solved 2026-11-09 1329 views 37 votes

How do you size SQLAlchemy pool_size?

@nox
Junior Python Dev

Hit this in staging after bumping workers.

Error / symptom: How do you size SQLAlchemy pool_size?

Traceback (most recent call last):
  File "app/main.py", line 42, in endpoint
    await db.execute(stmt)
TimeoutError: connection pool exhausted

Env: Python 3.12, FastAPI 0.115, uvicorn workers=4, Postgres 16.
What am I missing in pool settings?

5 replies

@piotr
2026-11-09 18:50 UTC · 2 votes

readiness should hit a cheap SELECT 1; liveness can stay process-only.

@tomo
2026-11-10 00:02 UTC · 0 votes

also check you are not creating an Engine inside the request.

@elias
2026-11-10 13:22 UTC · 12 votes

I saw the same when each worker opened its own pool of 20. Math: 4*20 = 80 against a 50 max_connections box.

@skye
2026-11-10 19:30 UTC · 0 votes

orjson helps cpu a bit; it will not save a saturated db.

@gabe
2026-11-11 04:22 UTC · 0 votes

also check you are not creating an Engine inside the request.