What is connection pooling and why does it matter when you scale?
IntermediateConceptDatabase connections in apps
Answer
Short answer
Opening a database connection is slow (TCP, TLS, authentication), so apps keep a pool of open connections and reuse them across requests. It matters when scaling because total connections = pods × pool size, and databases have a hard max_connections limit — scale out too far and the database refuses new connections.
Example math
- 25 pods × HikariCP
maximum-pool-size=20= 500 connections. - A db.t3.medium PostgreSQL allows roughly 400 → errors:
FATAL: sorry, too many clients already.
Pool settings per stack
| Stack | Setting |
|---|---|
| Spring (HikariCP) | spring.datasource.hikari.maximum-pool-size=10 |
Node pg |
new Pool({ max: 10 }) |
| Python SQLAlchemy | create_engine(url, pool_size=5, max_overflow=5) |
| .NET Npgsql | Maximum Pool Size=20 in the connection string |
Solutions at scale
- Size pools to what each pod actually needs (often 5–10 is plenty).
- Put a pooler in between: PgBouncer, RDS Proxy, ProxySQL (MySQL) — many app connections share a few DB connections.
- Account for HPA max replicas when sizing.
What to say in the interview
"Pools reuse expensive connections. When autoscaling, pods times pool size can exceed max_connections, so we right-size pools, include HPA maximums in the math, and use PgBouncer or RDS Proxy to multiplex connections."
More on Database connections in apps
How does an application connect to a database, and how should connection details be passed?BeginnerAfter deployment, the app can't connect to the database. How do you troubleshoot it?IntermediateWhat is a connection string? Explain its parts.BeginnerWhat are the default ports for common databases?BeginnerWhy should database credentials never be stored in code or Git?BeginnerHow do Kubernetes Secrets work, and are they secure?Intermediate