# Scaling and connection pooling

> How many database connections the engine uses, how to size it, and when to add a pooler.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/architecture/scaling-and-pooling/

For agents: Connection budget: each engine instance keeps a pool per project database (default max 2 when serverless, 10 when long-running) plus one registry connection; idle connections close after 30 s; worst case = instances × (projects active in each × pool size + 1). On Lambda, cap instances with reserved concurrency. db.t4g.micro ≈ 80 usable connections, small ≈ 190, medium ≈ 400. Add PgBouncer in transaction mode or RDS Proxy when many projects or high concurrency; the engine only uses SET LOCAL inside transactions, so transaction pooling is safe. Long-running mode (container, Fargate) shares one pool across requests and scales by CPU.

The engine is fast; the database's connection limit is what you size for.

## How many connections

Each engine instance keeps:

- a pool **per project database**: at most 2 connections when it runs as serverless functions, 10 when it runs as a long-lived server; idle connections close after 30 seconds;
- one connection to the **registry** when there are many projects (Sypnia Cloud), none when the projects come from a file.

Worst case: `instances × (active projects per instance × pool size + 1)`.

| Database               | Usable connections (approx.) | Serverless engine, 1 project |
| ---------------------- | ---------------------------- | ---------------------------- |
| `db.t4g.micro` (1 GB)  | 80                           | up to \~25 instances         |
| `db.t4g.small` (2 GB)  | 190                          | up to \~60 instances         |
| `db.t4g.medium` (4 GB) | 400                          | up to \~130 instances        |

On Lambda, **reserved concurrency** is the dial: it caps instances, so it caps connections. The template sets 5; raise it together with the database.

## Adding a pooler

When many projects share one server, or one project needs high concurrency, put a pooler between the engine and Postgres:

- **PgBouncer in transaction mode**, on a small instance or container. The engine only uses `set local` and `set_config(..., true)` inside transactions, so transaction pooling is safe.
- **RDS Proxy** works too; it is billed by the database's vCPUs, so it pays off on larger instances.

## Long-running mode

The same binary runs as a server (`sypnia-data serve`) in a container (ECS, Fargate, Kubernetes, a VM). One process shares one pool across all requests, so connections stay low and steady; scale by CPU. Sypnia Cloud moves to this mode as it grows.

## What else scales

- **CPU**: the costliest thing the engine does is argon2id at sign-in (about 100 ms of one core, on purpose). Everything else is a few milliseconds.
- **The schema cache** is per instance and refreshed on DDL; very large schemas (thousands of tables) take a second to load on a cold instance.
