🚨 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 (0)