Interview Prep/Real-World Interview/Scaling and performance

"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 →

  1. Confirm the math:
SELECT count(*), state FROM pg_stat_activity GROUP BY state;
SHOW max_connections;
  1. Immediately: reduce the per-pod pool size, or cap HPA maxReplicas.
  2. 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.
  3. Kill idle-in-transaction sessions and set idle_in_transaction_session_timeout.
  4. 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.

All Real-World Interview questions Roadmap: Interview readiness

More on Scaling and performance