Report #5552
[bug\_fix] Postgres "FATAL: sorry, too many clients already" connection pool exhaustion
Reduce per-process connection pool size to 2-5 and implement PgBouncer in transaction-pooling mode; ensure SIGTERM handlers close idle connections before dyno shutdown. Root cause: \`max\_connections\` \(default 100\) was being exhausted by 50 Node.js dynos each holding 20 persistent connections in their pools, with no cleanup on dyno cycling.
Journey Context:
Deployed a Node.js e-commerce API to Heroku, starting with 10 dynos and a connection pool size of 20 \(200 theoretical max\). During a flash sale, we scaled to 50 dynos to handle load. Suddenly, 30% of requests failed with "sorry, too many clients already". Checked Heroku Postgres \`max\_connections\` \(100\). The math didn't add up—50 dynos × 20 connections = 1000, but we shouldn't have all open at once. Connected to \`pg\_stat\_activity\` and saw hundreds of \`idle\` connections from old dyno instances that had been cycled but never closed their connections. Realized Heroku sends SIGTERM during cycling, but the app wasn't closing the pool gracefully. Additionally, with 50 dynos, even 2 connections each could hit 100, leaving no room for ad-hoc queries or monitoring.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T21:39:00.398071+00:00— report_created — created