Backend Engineer: FastAPI Lifespan & Scoped Dependency Injection Patterns for High-Concurrency APIs
One of the most frequent causes of connection pool exhaustion and memory leaks in production FastAPI services is instantiating database engines, HTTPX clients, or Vertex AI SDK sessions inside per-request dependencies instead of managing their lifecycle at the ASGI application level. As a Backend Engineer and AI Application Engineer, I architect deterministic two-tier dependency injection hierarchies in FastAPI: process-wide singletons bound to the ASGI lifespan context manager, and request-scoped units of work injected via yield dependencies.
1. Process-Wide Resource Pools via ASGI lifespan
Creating an AsyncEngine or httpx.AsyncClient on every HTTP request bypasses TCP connection reuse and triggers TLS handshake storms. Instead, we initialize shared connection pools once inside FastAPI's async lifespan context manager and attach typed registries to app.state:
from contextlib import asynccontextmanager
from typing import AsyncIterator, TypedDict
import httpx
from fastapi import FastAPI, Request, Depends
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
async_sessionmaker,
create_async_engine,
)
class AppState(TypedDict):
db_engine: AsyncEngine
session_factory: async_sessionmaker[AsyncSession]
http_client: httpx.AsyncClient
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[AppState]:
engine = create_async_engine(
"postgresql+asyncpg://app:secret@db.internal:5432/prod",
pool_size=20,
max_overflow=10,
pool_pre_ping=True,
)
session_factory = async_sessionmaker(
bind=engine,
expire_on_commit=False,
autoflush=False,
)
http_client = httpx.AsyncClient(
timeout=httpx.Timeout(10.0, connect=3.0),
limits=httpx.Limits(max_keepalive_connections=50, max_connections=100),
)
yield {
"db_engine": engine,
"session_factory": session_factory,
"http_client": http_client,
}
await http_client.aclose()
await engine.dispose()
app = FastAPI(lifespan=lifespan)
2. Request-Scoped Transactional Session & Deterministic Test Overrides
Per-request route handlers should never call session.commit() ad-hoc across dozens of service functions. By wrapping session_factory() in a generator dependency that commits cleanly on exit and rolls back automatically on unhandled exceptions, every API endpoint becomes an atomic Unit of Work:
async def get_db_session(request: Request) -> AsyncIterator[AsyncSession]:
factory: async_sessionmaker[AsyncSession] = request.state.session_factory
async with factory() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
In pytest integration suites, overriding app.dependency_overrides[get_db_session] with a savepoint-wrapped test transaction allows hundreds of integration tests to run against a real PostgreSQL 16 instance in seconds without truncating tables between tests. Combine this pattern with our Backend Engineer high-concurrency PostgreSQL & Python architecture and AI Application Engineer agent services.
Need a senior Backend Engineer to audit and scale your FastAPI or SQLAlchemy 2.0 services? Book a 1-Day Engineering Sprint.