# How Sovaria works
Source: https://sovaria.in/docs/architecture

The path a request takes — gateway, identity, access policies, the product that owns it and your database — and what each Sovaria product (Auth, Database, Documents, Storage, Functions, Realtime…) is responsible for.

Sovaria is a hosted service: a set of products behind one gateway. Your app talks only to the gateway, at your project URL; the gateway works out which project and who is calling, applies limits, and hands the request to the product that owns it. Each one checks your access policies before touching your data. Everything reaches Sovaria through the edge and is admitted by the gateway; no service is reachable on its own.

(Diagram: A request from your app to your data)

## What happens to one request

1. **Edge and gateway checks** — TLS ends at the edge. The gateway screens the request for injection and smuggling patterns, applies per-IP flood limits, adds security headers and works out the project from the host name (`<ref>.sovarias.in` or your custom domain).
2. **Who is calling** — A session token or API key is verified by **Auth** and turned into a verified identity (user, roles, organization, project, environment). Anything the client claims about itself is ignored.
3. **Limits** — Origin rules, rate limits and plan limits are applied. Over a limit, the request stops here with `403` or `429` and a reason.
4. **Routing** — The request goes to the product that owns the path. Requests are spread over healthy instances, and an instance that keeps failing stops receiving them.
5. **Access policies** — Your **access policies** decide whether this caller may do this action on this resource ([How access is decided](https://sovaria.in/docs/access-model)).
6. **Your data** — Allowed queries run in your project's database with the caller set for row-level security; the result goes back the same way.
7. **Recorded** — Usage is counted for billing, the request is logged and traced (**Logs & Audit**), and changes to schema, policies and functions are recorded in [Version history: see, undo and restore changes](https://sovaria.in/docs/version-history).

Every response carries an `X-Request-ID`; errors also carry a `trace_id`. Search either in Logs & Audit to see the request's path through these steps.

## The products

| Product | Responsible for |
|---|---|
| **Gateway** | The single entry point: TLS, project lookup, request screening, CORS and origins, rate and plan limits, routing. |
| **Auth** | Identity: sign-up and sign-in, sessions and tokens, MFA and passkeys, organizations, members and roles, API keys, project settings, mobile apps and push ([Authentication overview](https://sovaria.in/docs/auth-overview)). |
| **Access policies** | Your rules: compiling and evaluating them, policy versions and their audit trail ([How access is decided](https://sovaria.in/docs/access-model)). |
| **Database** | Your project's Postgres: SQL queries, the REST table API, transactions, schema changes, vectors, compute sizes, pooling, backups ([Database](https://sovaria.in/docs/database)). |
| **Documents** | The document store: collections, queries, transactions and live listeners ([Documents](https://sovaria.in/docs/documents-overview)). |
| **Storage** | Buckets, uploads and downloads, signed links, the S3-compatible API ([Storage](https://sovaria.in/docs/storage-overview)). |
| **Functions** | Deploys, versions, invocations, triggers, schedules and workflows; your code runs in an isolated sandbox and reaches the internet only through an egress proxy ([Functions](https://sovaria.in/docs/functions-overview)). |
| **Realtime** | WebSocket channels, presence, message replay and encrypted channels with Sovaria-managed keys ([Realtime](https://sovaria.in/docs/realtime-overview)). |
| **Logs & Audit** | Request and function logs, traces, log drains and Simulation runs. |
| **Version history** | Commits, timelines, snapshots and restores of schema, policies and functions ([Version history: see, undo and restore changes](https://sovaria.in/docs/version-history)). |
| **Billing** | Plans, entitlements, usage metering, invoices and payments ([Billing, invoices and overage](https://sovaria.in/docs/billing)). |
| **Sovariq** | The AI agent that can explain and operate your project with your permissions. |

## Security boundaries

- Projects are isolated: each has its own database role and data; keys and sessions work only for their own project.
- Services trust only identities the gateway verified, never what a client sends.
- Secrets (function secrets, OAuth client secrets, SMTP passwords, push keys) are encrypted at rest and never returned after you save them.
- Functions run sandboxed, reach the internet only through the egress proxy and only the hosts you list.
- Values in queries are bound as parameters by the database, never spliced into SQL.

## When something fails

| Situation | You see | What to do |
|---|---|---|
| A Sovaria instance is unhealthy | Requests are routed to healthy instances; a few may get `502`/`503`. | Retry with backoff (the SDKs do). |
| Your database is starting or paused | `503 PROJECT_MAINTENANCE` | Retry shortly; the first request wakes a paused database. |
| Your database is asleep (idle) | `503 PROJECT_MAINTENANCE` with `reason: "asleep"` or `"waking"` and `Retry-After` | Retry after a few seconds (the SDKs do); the request already started it. |
| Another change to the same thing is running | `503 RESOURCE_BUSY` with `Retry-After` | Retry after the given seconds. |
| A limit is reached | `429` or `403 PLAN_*` | Back off, or raise the limit ([Plan limits](https://sovaria.in/docs/plan-limits)). |

More in [Reliability](https://sovaria.in/docs/reliability).
