Skip to Content

What’s the right async pool config for test vs prod?

Category: Database & RAG

Answer

Tests should use NullPool (no shared connections) so each session is fresh and isolated. Production needs pool_size + pool_pre_ping + pool_recycle so dropped connections are detected before they break a request. Async engines also need echo=False in prod to keep logs readable.

Concrete examples from the fca project context

Example 1

test env: NullPool, echo=False. dev: small pool, echo=True. prod: pool_size=20, pool_pre_ping=True, pool_recycle=3600.

Example 2

Alembic migration CLI uses a SEPARATE synchronous engine to avoid event-loop mishaps.

Example 3

Lazy engine creation via engine_kwargs = {**defaults} and branching on settings.environment keeps one Settings class.

Data flow / flow chart

Settings.environment -> engine_kwargs:
  test  -> NullPool, echo=False
  prod  -> pool_size=20, pool_pre_ping=True, pool_recycle=3600

Takeaway

Different environments want different pools. Branch on Settings.environment, not on whether you feel lucky.