Incident · July 2026
Published · 4 min read
The obvious fix would have worked. That's why I didn't take it.
In July 2026 one dashboard with fourteen queries saturated tunyt's Postgres. Adding an index would probably have made the graph go green. I put budgets around the whole database instead. The seven decisions, the maths behind them, and why I'd make the slower call again.
In July 2026 tunyt's database hit its ceiling, and the obvious fix was right there: find the slowest query, add an index, watch the graph drop.
It probably would have worked for a while. That's why I didn't take it. It would have worked just well enough that we'd never ask why one page could take the whole database down.
What saturation looked like
Every connection in the pool was busy, and every request that needed one waited behind them. Pages that had nothing to do with the cause got slow too, because they were queueing for the same connections.
When unrelated pages slow down together, a shared resource has run out. One slow query doesn't do that.
I didn't chase the query
Speed up the slowest query and another one becomes the slowest. Traffic grows, the pool saturates again, and you add another index. Every round feels like progress.
So I asked a different question: why could one part of the product take the whole database down with it?
A little maths
Every connection pool follows one law:
connections in use ≈ requests per second × seconds each request holds a connection
That's Little's law, the most useful equation I know for backend work. A page that loads 20 times a second and holds a connection for a quarter of a second keeps about 5 connections busy, permanently.
Now make that page run fourteen queries at once.
What was happening
Our organiser dashboard needed fourteen pieces of data, and the first version fetched all fourteen in parallel. One page load took fourteen connections.
The results were cached, so most loads should never have reached Postgres. But caches expire. When ours did, every request in that window missed and ran all fourteen queries itself. We saw two identical waves of the same queries hit the database together. That's a cache stampede.
Meanwhile the analytics worker was running its own jobs on the same database, five at a time.
No single query was the problem. Nothing in the system said how much of the database any one part was allowed to use.
A budget instead of a bigger database
The other easy answer was a bigger database with more connections. Look at the equation again: demand grows with traffic. A bigger pool moves the ceiling, and the next fan-out or stampede fills it too. Postgres connections are whole server processes, so you can't keep adding them.
So I set one number for how many connections the whole deployment may hold and split it. The API gets one slice and the workers get a smaller one. A busy worker can't starve the API, and the API can't starve the workers. At worst, "the database is down" becomes "analytics are a bit slow".
A ceiling on fan-out
No more Promise.all over an open-ended list of queries. How many run at once is capped, whatever a page happens to want.
// Run fn over items with at most `limit` in flight at once.
async function mapLimited<T, R>(items: T[], limit: number, fn: (item: T) => Promise<R>) {
const out = new Array<R>(items.length);
let next = 0;
const lanes = Array.from({ length: Math.min(limit, items.length) }, async () => {
while (next < items.length) {
const i = next++;
out[i] = await fn(items[i]);
}
});
await Promise.all(lanes);
return out;
}
One request rebuilds, the rest wait
On a cache miss the first request takes a short Redis lock and rebuilds the value. Everyone else keeps getting the last good copy. There's also a guard inside each process so a server doesn't race itself for the lock.
// Simplified. The real one also stamps an expiry so stale values can be detected.
async function getDashboard(eventId: string) {
const key = `cache:dashboard:${eventId}`;
const cached = await redis.get(key);
if (cached && !isStale(cached)) return JSON.parse(cached);
// Only one request rebuilds. The rest serve the last good copy.
const won = await redis.set(`${key}:lock`, "1", "PX", 10_000, "NX");
if (!won && cached) return JSON.parse(cached);
const fresh = await buildDashboard(eventId);
await redis.set(key, JSON.stringify(fresh));
return fresh;
}
Some people see data a few seconds old while it rebuilds. For an analytics dashboard that's an easy trade. Nobody decides differently because a chart is five seconds behind.
The small ones
Stop recomputing the past. A finished day of analytics never changes, so completed days come from a daily rollup table. Only today is computed live.
Background work loses. The analytics worker went from five concurrent jobs to three. Analytics got slower on purpose, so background work can't win a fight with the people using the product.
Indexes that don't block writes. When we add an index to a busy table, we build it with CREATE INDEX CONCURRENTLY. It's slower, but it doesn't lock writes while it runs.
Why I'd do it again
The index would probably have turned the graph green and left the hole where it was. The next stampede or the next fourteen-query dashboard would have found it.
Every one of these decisions is now a written rule the team checks before shipping a list endpoint, a cache or a worker, and our coding agents read the same rules. The budgets live in one table, and changing a number needs a reason in the pull request. We paid for some of those numbers with this incident.
By Aditya Dutt Pandey, CTO of tunyt