The booking API of a car rental platform I built worked perfectly in every manual test. Then an outside-in test fired twelve bookings at the same moment. The API stopped answering for 30 seconds, some requests came back as 500, and every other request on the server waited too. The log had one line that explained everything once I understood it:
SequelizeConnectionAcquireTimeoutError: Operation timeout
It reproduced in three runs out of four, which is the worst kind of bug: rare enough to ship, common enough to hit production on a busy evening.
The setup
The backend is NestJS with Sequelize on PostgreSQL. The connection pool was small, as it should be on a modest database plan:
pool: { max: 5, min: 0, acquire: 30000, idle: 10000 }
Creating a booking runs inside a transaction, because a booking touches several tables that must change together.
What actually happened
Inside that transaction, the code checked whether the car was still free. The availability query looked innocent:
await sequelize.transaction(async (tx) => {
const free = await this.availability.findFreeCars(range); // no { transaction: tx }
// ...create the booking with { transaction: tx }
});
Without { transaction: tx }, Sequelize does exactly what you asked: it runs the query on a new connection from the pool. So each booking:
- takes connection #1 for its transaction,
- asks the pool for connection #2 for the availability check,
- only releases #1 after #2 has done its work.
With one booking that is harmless. With five at once, each of them holds one connection and waits for a second. The pool has five. Nobody can finish, so nobody releases anything, and everyone waits until the 30-second acquire timeout gives up. It is a deadlock, just in the connection pool instead of in the database.
The fix: make transactions contagious
I could have hunted down every query and added { transaction: tx }. That fixes today's bug and invites the next one: the first new query someone writes without it brings the deadlock back.
Sequelize has a better option. With CLS (continuation-local storage) enabled, any query made inside sequelize.transaction(cb) joins that transaction automatically, even if the code never passes it. Sequelize only needs an object with get, set, run and bind, and Node's built-in AsyncLocalStorage can provide all four, so there's no extra dependency:
import { AsyncLocalStorage } from "node:async_hooks";
import { Sequelize } from "sequelize";
const store = new AsyncLocalStorage<Map<string, unknown>>();
Sequelize.useCLS({
get: (key: string) => store.getStore()?.get(key),
set: (key: string, value: unknown) => store.getStore()?.set(key, value),
run: (fn: (ctx: Map<string, unknown>) => unknown) => {
const ctx = new Map(store.getStore());
return store.run(ctx, () => fn(ctx));
},
bind: <T>(fn: T) => fn,
} as unknown as Parameters<typeof Sequelize.useCLS>[0]);
Now the availability check runs on the booking's own connection. Each booking needs exactly one connection, and twelve parallel bookings simply queue for the pool and finish.
The opposite case
Automatic joining has one trap. The background-jobs worker holds a long-lived transaction for a PostgreSQL advisory lock, so only one worker runs a job at a time. With CLS, every query the job made would join that lock transaction, which is not what you want for real work. So there is an explicit escape hatch:
/** Runs fn with no current transaction: its queries take their own connection. */
export function outsideTransaction<T>(fn: () => T): T {
return store.run(new Map(), fn);
}
The worker takes the lock, then does the job's work inside outsideTransaction.
What I took away
- A transaction holds a connection for its whole life. Anything that asks for a second connection while it is open can starve the pool under load.
- Make the safe path the default. CLS removes the need to remember
{ transaction: tx }on every call. - Concurrency bugs need concurrency tests. No amount of clicking through the UI would have found this. The test that fires twelve simultaneous bookings stays in the suite as a regression check.
- Small pools are fine. The bug wasn't that five was too few; doubling the pool would only have raised the number of bookings needed to trigger it.
- #Database
- #Concurrency