Aditya Dutt Pandey

How we work · 2026

Published · 4 min read

The scarcest thing we own is a database connection

On tunyt the resource that has hurt us most is database connections, more than CPU, memory or instances. Why Postgres makes them scarce, the one equation behind every rule we have, and four rules that come out of it.

People talk about scaling a backend in terms of CPU, memory and instances. On tunyt the resource that has hurt us most is database connections. Once I started looking at the whole system through that, a lot of arguments turned into arithmetic.

Why connections are scarce

Postgres gives every connection its own server process. That's good for isolation and makes connections heavy. Each one costs memory on the database server, and past a certain number Postgres spends more effort managing connections than doing your work.

So our pool is small on purpose, and every API instance and every worker shares one fixed budget. When the budget runs out, requests wait in line no matter how fast your code is.

The one equation

connections in use ≈ requests per second × seconds each request holds a connection

That's Little's law. You can lower the rate, or lower how long each request holds a connection. Every rule below is about the second one.

Never hold a connection while waiting on someone else

A transaction holds a connection from the moment it opens until it commits. Call an outside service inside it and you hold that connection for the whole round trip. For us that means payments, SMS, email and LLM providers.

Run the numbers. If a payment call normally takes 300 ms, that's already a long hold. On a day the provider takes three seconds, every request doing this holds a connection ten times longer, so you need ten times the connections for the same traffic. The pool fills, and requests that have nothing to do with payments queue too. Now someone else's latency decides our uptime.

// Don't: the connection is held for the provider's whole latency.
await db.$transaction(async (tx) => {
  const order = await tx.order.create({ data });
  await payments.capture(order.id); // slow, and outside our control
  await tx.order.update({ where: { id: order.id }, data: { paid: true } });
});

// Do: write intent, call out, then record the result.
const order = await db.order.create({ data: { ...data, status: "PENDING" } });
const result = await payments.capture(order.id);
await db.order.update({ where: { id: order.id }, data: { status: result.ok ? "PAID" : "FAILED" } });

This costs something. The second version adds a PENDING state, and now you have to handle orders stuck in it. I'll take that every time: a stuck order is a problem for one person, a full pool is a problem for everyone.

Every list query has a ceiling

An unbounded findMany was marked P0 in our audit, our highest severity. It wasn't slow on the day we found it. The danger is the day the table grows: it pulls every row into memory in one go and holds a connection the whole time. Tables grow fastest on your busiest days, so it goes off at the worst moment.

Now every list endpoint paginates and every internal fetch has an explicit take. The number depends on the query, but there's always a number.

Ask once, not N times

Our payment flow needs several tickets at once. Looking them up one by one in a loop is N round trips and N turns on the pool for something the database can answer in one. So it asks once.

// One query for every id, then a Map to put them back in order.
const tickets = await db.ticket.findMany({ where: { id: { in: ids } } });
const byId = new Map(tickets.map((t) => [t.id, t]));
const ordered = ids.map((id) => byId.get(id));

The Map matters. IN doesn't promise rows back in the order you asked, and code that assumes it does will pass every test and be wrong in production.

An index ships with its query

Any new filter needs an index before it ships. If the index is missing, it goes in the same pull request. A query without one holds its connection for as long as the scan takes, which puts you straight back in the equation.

On high-write tables we use CREATE INDEX CONCURRENTLY. It's slower and can't run inside a transaction, but it doesn't block writes while it builds.

Why we write this down

None of these rules is clever, and most backend engineers know them. We write them down because a team of five building a mobile app, a web app and the AI infrastructure at the same time can't remember everything. More hardware never fixed it. Wasting fewer connections did.

By , CTO of tunyt

← All writing