Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A reservation service does SELECT ... FOR UPDATE on a seat row, then calls a payment provider over HTTP, then updates the seat and commits. Under load, connections run out and p99 latency goes to 30 seconds. Explain the problem and redesign the flow.

The seat row lock and a database connection are held for the whole payment call, so when the provider slows down, every request for that seat queues on the lock and every request in the service queues for a connection, which drives p99 to the pool timeout of 30 seconds. The redesign splits the flow into two short transactions around the HTTP call: first mark the seat HELD with an expiry and commit, then call payment outside any transaction, then confirm with a conditional UPDATE that succeeds only if our hold is still valid.

Why this question is asked

Putting a remote call inside a database transaction is one of the most common causes of cascading outages in Spring services, because @Transactional makes it look harmless. Interviewers ask this scenario to see whether we can connect locks, connection pools and latency, and whether we can redesign the flow without losing correctness. In this answer, we will see why the current flow collapses under load, redesign it as reserve, pay and confirm, add expiry, idempotency and timeouts, and handle the awkward case where payment succeeds after the hold expired.

1What the Current Flow Holds and for How Long

The service does SELECT ... FOR UPDATE on the seat, which takes an exclusive row lock until commit. It then calls the payment provider, which normally takes 300 milliseconds but sometimes 10 seconds or more. Only after that does it update the seat and commit. For that whole time, two scarce things are held: the row lock on the seat and one connection from the HikariCP pool.

Under load the two effects feed each other. Other buyers of the same seat wait on the lock, each holding its own connection while it waits. Requests for completely different seats need connections too, find the pool empty, and wait up to connectionTimeout, which defaults to 30 seconds in HikariCP. That is the p99 the question describes: the latency is not the database being slow, it is requests queueing for resources held by slow HTTP calls.

Java
@Transactional
public Booking book(long seatId, Card card) {
Seat seat = seats.findForUpdate(seatId); // row lock + connection taken
if (!seat.isFree()) throw new SeatTakenException();
payment.charge(card, seat.getPrice()); // HTTP, 300 ms to 10+ s, lock still held
seat.markSold();
return bookings.save(new Booking(seat)); // commit releases lock and connection
}
// pool of 20 connections, 20 slow payments in flight -> every other request waits up to 30 s

2The Redesign: Reserve, Pay, Confirm

The fix is to keep each database transaction short and never wait on the network inside one. We split the booking into three steps. Step one is a short transaction that marks the seat HELD, with a hold id and an expiry time a few minutes ahead, and commits; it takes milliseconds. Step two is the payment call, outside any transaction, so no lock and no connection is held while we wait. Step three is another short transaction that turns the hold into a sale, but only if the hold is still ours and still valid.

SQL
-- step 1: reserve (short transaction)
UPDATE seat SET status = 'HELD', hold_id = :holdId, hold_expires_at = now() + interval '5 minutes'
WHERE id = :seatId AND (status = 'FREE' OR (status = 'HELD' AND hold_expires_at < now()));
-- 1 row: we hold it; 0 rows: seat taken, answer at once
-- step 2: payment.charge(card, price, idempotencyKey = holdId) -- no transaction open
-- step 3: confirm (short transaction)
UPDATE seat SET status = 'SOLD', hold_id = NULL
WHERE id = :seatId AND status = 'HELD' AND hold_id = :holdId;
-- 1 row: sold; 0 rows: our hold expired or was released
Step throughReserve, pay outside the transaction, confirm
Booking serviceDatabasePayment provider
UPDATE seat SET HELD, hold_id, expiry; COMMIT
1 row: hold taken (ms)
charge(card, price, key = hold_id)
approved (300 ms to 10 s, no lock held)
UPDATE seat SET SOLD WHERE hold_id = ? AND HELD; COMMIT
1 row: sold, or 0 rows: hold lost

6 messages in order. Play them, or step through them one at a time.

Connections and locks are held only for the two short updates.

Notice that both writes are conditional updates. They lock the row only for the length of one statement and their WHERE clause does the check, so there is no read-then-write gap. The status column and hold id now carry the reservation across time, instead of a lock that ties up a connection.

3Expiry, Idempotency and Clean-Up

A hold must not live forever if the user walks away or the service crashes between steps. The expiry time makes the seat available again; the reserve statement already treats an expired hold as free, and a background job can periodically reset expired holds to FREE so that seat maps show the truth.

The payment call needs an idempotency key, and the hold id is a natural choice. If the call times out and we retry, the provider recognises the key and returns the original result instead of charging twice. We should also record the payment attempt in our own table, in a short transaction before the call, so a recovery job can find payments whose outcome we never learned and ask the provider.

4Timeouts So Failures Stay Small

The last layer is to fail fast instead of queueing. The payment client gets a connect timeout of about 1 second and a read timeout slightly above the provider's normal worst case, for example 5 seconds. On the database side, lock_timeout of a second or two makes a rare wait on the seat row fail quickly, and a HikariCP connectionTimeout of 1 to 2 seconds turns pool exhaustion into fast errors that a load balancer and client can handle, rather than 30-second hangs. A circuit breaker around the payment client stops sending calls when the provider is clearly down, so holds expire cleanly instead of piling up.

5Follow-up questions

How would you handle the payment succeeding after the hold expired?

The confirm update returns 0 rows, so we know the seat is no longer ours. First we try to re-acquire it with the same conditional reserve-and-sell update: if nobody else has taken the seat, we sell it and the customer never notices. If someone else holds or bought it, we refund the payment automatically using the provider's refund API with its own idempotency key, tell the customer, and perhaps offer a similar seat. The case must be logged and counted, because a rising rate means the hold time is too short for real payment times.

Why not just raise the connection pool size?

A bigger pool delays the collapse but does not remove it, because slow payments can still fill any pool, and each extra connection costs memory on the database server. It also does nothing for buyers of the same seat, who still queue on the row lock. Removing the HTTP call from the transaction fixes both.

Could optimistic locking replace the hold?

A @Version check at confirm time would detect that someone else sold the seat, but only after we charged the card, so we would refund often on popular seats. The hold reserves the seat before payment, which is what customers expect from a booking flow.

6Conclusion

In this answer, we saw that holding a row lock and a pooled connection across an HTTP call turns slow payments into lock queues and pool exhaustion, which is where the 30-second p99 comes from. The redesign reserves the seat with a short conditional update and an expiry, calls payment outside any transaction with an idempotency key, and confirms with a conditional update that checks the hold. Expired holds are released by a background job, timeouts and a circuit breaker keep failures fast, and a late payment is either re-attached to the seat or refunded.

What to say in the interviewAbout 80 seconds aloud

Open with

The problem is that the FOR UPDATE lock and the database connection are both held for the duration of the payment HTTP call. When the provider slows down, buyers of the same seat queue on the row lock, and everyone else queues for a pool connection; Hikari's default connection timeout is thirty seconds, which is exactly the p99 we see.

Then, if they want more

I'd split the flow. First a short transaction that does a conditional update setting the seat to HELD with a hold id and an expiry, say five minutes, only if it's free or the old hold has expired, and commits.

Then the payment call with no transaction open, using the hold id as the idempotency key so retries can't double charge. Then a second short transaction that sets the seat to SOLD where status is HELD and hold_id matches.

If that updates zero rows, the hold expired, so I try to re-take the seat, and if it's gone I refund automatically. A background job releases expired holds.

I'd also add tight timeouts: a few seconds on the payment client with a circuit breaker, a lock_timeout of a second or two, and a short Hikari connection timeout, so failures stay fast and local.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A reservation service does SELECT ... FOR UPDATE on a seat row, then calls a payment...": https://scrivio-interview-library.netlify.app/databases/database-locking/ac5fb055ca/