๐จ The morning it broke
It was the first morning of our marketing sale. We had launched our mobile app shortly before, and it had been running fine. Then the orders started pouring in. A truckload of them.
The first sign was an SOS from a teammate: browsing was slow. I opened our observability dashboards and the alerts were already firing. Lots of red. Not the festive kind. Database connections were pinned at their limit and new requests were being turned away. The website, the admin panel and the app were all failing with the same error: Database connection failed.
I was the support engineer on duty, and I was on my own. My honest first thought was: oh my. ๐ณ Customers were complaining, on the busiest morning we'd had.
What helped most wasn't technical. The rest of the team stayed calm. They spoke to customers kindly while I worked, which let me focus on the database instead of the noise. Shout-out to them.
What the app changed
Our database had handled the website for a long time without trouble. On a normal day, each request borrowed a connection from the backend's pool, ran its queries and gave it back quickly. Sale-day traffic plus the new app broke that rhythm in two ways.
- Connections were held longer. The app's background sync and polling kept a steady stream of requests coming, and some code paths didn't give their connection back as soon as they should have.
- It sent queries in bursts. Each time someone opened the app, it fetched their profile, notifications and telemetry all at once. Multiply that by a sale's worth of shoppers.
Our database plan (1 GiB of RAM) allowed only 22 connections. On a normal day that was enough. On sale day, with the new app, it ran out.
Fixing it, and making it worse
I started with the obvious moves. I scaled the database cluster to clear out stale connections and put PgBouncer, a connection pooler, in front of it. Then I upgraded the database to a bigger plan with 2 vCPUs and 4 GiB of RAM. DigitalOcean sets the connection limit by memory, so it jumped from 22 to 97.
With all that new room, I raised the app's connection limit to 90. It felt like the right call. More room, more connections, more speed. Right?
It wasn't. Database CPU shot to nearly 100%. Pages that had been slow were now crawling. My fix had made things worse. ๐ซ
Here's why. Think of a traffic jam: letting more cars onto the road doesn't get anyone home faster. A database with 2 CPUs can only truly run 2 queries at the same moment. Every other active query waits its turn, and the more queries are fighting for those 2 cores, the more time goes on juggling them. Some of ours were also hitting unindexed columns, which means scanning whole tables. Ninety of those at once pinned the CPU.
That was the lesson of the morning: a connection limit is not a target. It tells you how many connections the database will accept, not how many it can serve well. The real limit is the CPU behind it.
The fix that held
So I went the other way. I cut the app's limit to 20 and made sure PgBouncer was running in transaction mode. Fifteen minutes later, everything was back to normal. ๐ฎโ๐จ From the first refused connection to full recovery took about 75 minutes.
Three changes did the work together:
- All traffic goes through PgBouncer. The backend no longer connects to the database directly. The pooler shares a small set of real connections among many clients. Think nightclub bouncer: only so many inside at once, and nobody hogs the dance floor.
- Transaction mode. A connection goes back to the pool the moment a transaction finishes, not when the client disconnects, so the app's long sessions stop holding connections hostage.
- A pool sized to the CPU. Twenty works on 2 CPUs because most connections are idle at any given moment, waiting on the network or the app. A common starting point for truly active queries is about two per CPU core. If the backend scales to two containers, each gets 10, so the total stays at 20.
One catch with transaction mode: anything tied to a database session doesn't carry over between transactions. That includes session-level SET commands, advisory locks, LISTEN/NOTIFY and, on older PgBouncer versions, prepared statements. Check your ORM or driver is configured for transaction pooling before you switch.
Database CPU now stays under 45% at peak, and average API response time is under 150 ms.
How I knew it worked
I watched two numbers on DigitalOcean's database metrics dashboard. Connections told me what was failing. CPU told me whether a fix was working.
I didn't call it resolved straight away. I kept the database on the 2-CPU tier for 48 hours and watched both graphs through a full traffic cycle first.
What I'd do differently
Looking back, this was a launch problem as much as a database problem. We planned the app's features and its release, but not what its traffic would do to a database sized for the website.
- Load-test a new client before launch. A mobile app talks to the backend differently from a website. Test the pattern, not just the endpoints.
- Put a pooler in front of the database from day one. It costs little and removes a whole class of outage.
- Size the pool to the CPU, not the connection limit. Start small, measure, then raise it.
- Release connections in code, every time, even when a query fails.
- Find the slow queries. Missing indexes make every query hold its connection longer.
A launch isn't finished when the app ships. It's finished when the system behind it survives its first busy morning.
For the engineers: what I check now
Alert on the early signal, not the failure: connections above 80% of the limit, and database CPU above 70% for five minutes. And when something feels off, this shows what connections are actually doing:
SELECT state, count(*)
FROM pg_stat_activity
WHERE datname = current_database()
GROUP BY state;
The state to worry about is idle in transaction: a connection stuck inside an open transaction, holding a slot and often locks too. Behind PgBouncer, plain idle is normal; those are the pool's spare connections. In PgBouncer itself, SHOW POOLS; shows how many clients are waiting for a connection (cl_waiting).
The timeline
| Minutes in | What happened |
|---|---|
| 0 | Connections start climbing as sale traffic arrives. |
| 75 | Connections hit the limit of 22. The database starts refusing new ones. |
| 90 | Scaled the cluster to clear stale connections; started PgBouncer. |
| 105 | Upgraded to 2 vCPUs and 4 GiB of RAM; limit rises to 97. |
| 120 | Raised the app's limit to 90. CPU jumps to nearly 100%. |
| 135 | Cut the app's limit to 20; confirmed transaction mode. |
| 150 | Website, app and admin panel back to normal. |




Top comments (10)
Really honest write-up, especially the part where raising the limit to 90 made things worse. Most incident posts skip the step that didn't work, and that's usually the most useful part. Two small things that have helped us in similar situations: setting
idle_in_transaction_session_timeout(something like 30s) on the app's database role, so a code path that forgets to release a connection gets cut off by Postgres instead of holding a slot until someone notices; and looking at the app-open burst itself. If opening the app fires profile, notifications and telemetry at the same time, combining the first two into one endpoint and sending telemetry a few seconds later (or batching it) flattens those spikes quite a bit. Also worth writing down the pool math for autoscaling: per-container pool x max containers should stay under what PgBouncer is set to hand out, otherwise the next traffic spike quietly brings back the same problem.Thank you, I really appreciate this! These are great tips, especially the idle-in-transaction timeout and batching the app-open calls. I'll be adding them to my checklist. ๐
Good callout on profiling before rewriting. Swapping languages without fixing memory layout, cache locality, or unnecessary heap allocations just moves the bottleneck to a different stack trace.
Flamegraphs and syscall counts tell the truth faster than language benchmarks ever will.
Thanks for reading!
The traffic-jam analogy fits. Past roughly 2-4x the core count, extra active connections mostly add context switching, not throughput. We usually size the app-side pool near that number and let PgBouncer in transaction mode absorb the rest. Did you check pg_stat_activity or pg_stat_statements for which unindexed queries were the worst offenders? Fixing those first often frees more CPU than any pool tuning.
Thanks, great point! That's our next step: we're running EXPLAIN ANALYZE on the heaviest endpoints, and pg_stat_statements is a good shout for finding the worst offenders first. ๐
One thing worth flagging for anyone copying the transaction-mode fix: it quietly changes what a "session" means. Anything that leans on session state, like SET statements, advisory locks, LISTEN/NOTIFY or server-side prepared statements, can end up on a different backend connection for the next transaction. Older PgBouncer versions are the classic source of "prepared statement does not exist" errors right after the switch; 1.21+ can track them if you set max_prepared_statements. Did your ORM need any config change when everything moved behind the pooler?
Thanks for adding this, it's an important detail! In our case the ORM worked behind the pooler without changes, but it's definitely worth checking before switching. ๐
a connection limit is not a target is the line that stays with me. cutting the pool to 20 and letting pgbouncer do the work is the fix most people resist.
Thank you! It definitely felt backwards in the moment, cutting connections to make things faster. Glad that line stuck with you.