"Too many connections" errors from Postgres after scaling the app out. How do you fix it?
IntermediateScenarioScaling and performance
Answer
Situation read → Each pod opens its own pool. 50 pods × 20 connections = 1,000, beyond max_connections.
What I'd do →
- Confirm the math:
SELECT count(*), state FROM pg_stat_activity GROUP BY state;
SHOW max_connections;
- Immediately: reduce the per-pod pool size, or cap HPA
maxReplicas. - Proper fix: put a pooler in front, RDS Proxy on AWS or PgBouncer in transaction mode, so thousands of client connections share a small number of server connections.
- Kill idle-in-transaction sessions and set
idle_in_transaction_session_timeout. - Add an alert on connections at 80% of max.
Why → Postgres connections are processes with real memory cost; raising max_connections endlessly hurts the DB.
Interview tip → Do the multiplication out loud; it shows you understand the root cause.
More on Scaling and performance
Traffic is expected to 10x for a sale next week. How do you prepare the platform?IntermediateThe HPA is configured but pods don't scale even though the app is slow. Why?BeginnerPods are Pending during a traffic spike because the cluster is out of capacity. What do you do now and long term?IntermediateThe database CPU is at 95% and the API is timing out. What's your approach?IntermediateLatency is fine on average but p99 is terrible. Where do you look?AdvancedOne pod in a deployment is much slower than the others. How do you investigate?Beginner