Quick revision
The short answer
We keep a reservations table with id, sku, quantity, order id, status and expires_at, and compute available stock as on hand minus active reservations, usually through a reserved counter on the stock row. A reservation is created in one transaction with an atomic conditional update on the stock row, confirmed when payment succeeds, and released on failure or cancellation as a saga compensation. A scheduled job using SKIP LOCKED, or a delayed message, expires reservations after 15 minutes; reserve and release are idempotent per order line, and stock changes are published as events for search and product pages.
What to remember
In this answer, we modelled on hand, reserved and available stock, created reservations with an idempotent insert and an atomic conditional update in one transaction, confirmed or released them through a checkout saga where release is the compensation, expired abandoned reservations with a SKIP LOCKED sweep or delayed messages, published stock changes through an outbox, and looked at splitting or moving a hot SKU's counter.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We keep a reservations table with id, sku, quantity, order id, status and expires_at, and compute available stock as on hand minus active reservations, usually through a reserved counter on the stock row. A reservation is created in one transaction with an atomic conditional update on the stock row, confirmed when payment succeeds, and released on failure or cancellation as a saga compensation. A scheduled job using SKIP LOCKED, or a delayed message, expires reservations after 15 minutes; reserve and release are idempotent per order line, and stock changes are published as events for search and product pages.
Why this question is asked
Holding stock during checkout is how online stores avoid selling the same last item to two people who are both entering card details. But a reservation that is never released is just as bad as no reservation: it hides stock that could be sold. Interviewers ask this question because it combines a concurrency problem, a distributed workflow across order, payment and inventory services, and time-based clean-up. In this answer, we will model on hand, reserved and available stock, create reservations atomically and idempotently, confirm or release them through the checkout saga, expire abandoned ones, publish stock changes, and handle a hot product.
1On Hand, Reserved and Available
Three numbers describe a product's stock. On hand is what is physically in the warehouse. Reserved is what is held for checkouts in progress. Available, the number we may still promise to new customers, is on hand minus reserved. If we have 10 units and three customers are paying for one each, available is 7.
Each reservation is its own row, so we can see who holds what and until when. Summing active reservations on every read would be slow, so the stock row also keeps a reserved counter, updated in the same transaction as the reservation rows. The two must always agree, and the reconciliation job described later checks that they do.
Notice the check constraint reserved <= on_hand. It turns any bug that would reserve more than we have into a database error instead of an oversell.
2Creating a Reservation Atomically and Idempotently
When a customer starts checkout, the order service calls reserve for each line. Two rules apply. It must be atomic, so two customers cannot both take the last unit, and it must be idempotent, because the order service will retry if a call times out, and a retry must not reserve twice.
We do both in one transaction. First we insert the reservation row; the unique constraint on (order_id, line_no) means a retry inserts nothing, and we simply return the existing reservation. Then we run a conditional update on the stock row that only succeeds if enough stock is available. If it updates zero rows, we throw, the transaction rolls back, and the reservation row disappears with it.
The conditional update is the same idea as an atomic decrement: the check and the change happen in one statement under the row lock, so concurrent reservations for the last unit cannot both succeed.
3Confirming or Releasing Through the Checkout Saga
Checkout spans several services: order, inventory and payment. There is no single database transaction across them, so we use a saga, a sequence of local transactions where each failure step has a compensating action that undoes an earlier step.
The order service drives it. After the reservation succeeds, it asks payment to charge the card. On success, it calls confirm, which turns the reservation CONFIRMED and moves the quantity out of stock for good: on_hand and reserved both drop by the quantity. On payment failure or cancellation, it calls release, the compensation, which sets the reservation RELEASED and reduces reserved, making the units available again. Both operations change state only if the reservation is still ACTIVE, which makes them idempotent and safe to retry.
7 messages in order. Play them, or step through them one at a time.
On success the order service would call confirm instead; both only act on ACTIVE reservations.
4Expiring Abandoned Reservations
Many customers simply close the tab, so neither confirm nor release ever arrives. A scheduled job runs every minute and releases ACTIVE reservations whose expires_at has passed. Several instances can run it at once safely by claiming rows with FOR UPDATE SKIP LOCKED in batches, so each instance gets different reservations.
A delayed message, sent at reservation time and delivered after 15 minutes, is an alternative that avoids polling, but we keep the sweep anyway as a safety net in case a message is lost.
There is one race to decide on: payment succeeds at 10:15:02, two seconds after the reservation expired. Confirm finds the reservation EXPIRED rather than ACTIVE. We then try to reserve again; if stock is still there, the order goes through, and if not, we refund and apologise. Giving payment a little extra time, by expiring at 15 minutes but letting the payment step finish within 16, keeps this rare.
5Publishing Stock Changes
Search results and product pages show whether an item is in stock, and they read from their own stores. So every change to available stock produces an event, such as StockAvailabilityChanged with sku and available quantity, written to an outbox table in the same transaction and published to Kafka by a relay. The outbox guarantees we never publish a change that rolled back, and never lose one that committed.
During a sale, a popular SKU changes many times per second, and search does not need every change. Consumers can coalesce events per SKU and update every second or two, and most product pages only need to know in stock, low stock or sold out.
6Follow-up questions
How would you handle a hot SKU that receives thousands of reservations per second?
A single stock row becomes the bottleneck because every reservation locks it. One fix is to split the SKU's stock into several bucket rows, for example 20 rows of 50 units, and reserve from a random bucket, falling back to others when it is empty; contention drops by about the number of buckets. Another is to hold the hot counter in Redis and decrement it with an atomic Lua script, writing reservations to the database asynchronously, which is fast but needs careful reconciliation. For extreme drops, a queue per SKU that admits customers in order is also common, and it gives a fairer experience.
Why not reserve stock when the item is added to the cart?
Carts are often abandoned or left open for days, so reserving at cart time would hide large amounts of stock from real buyers. Reserving at checkout, with a short expiry, holds stock only while someone is actually trying to pay.
How do you make sure the reserved counter matches the reservation rows?
Updates to both happen in the same transaction, and a periodic reconciliation compares the counter with the sum of ACTIVE reservations per SKU, alerting and correcting on any difference.
7Conclusion
In this answer, we modelled on hand, reserved and available stock, created reservations with an idempotent insert and an atomic conditional update in one transaction, confirmed or released them through a checkout saga where release is the compensation, expired abandoned reservations with a SKIP LOCKED sweep or delayed messages, published stock changes through an outbox, and looked at splitting or moving a hot SKU's counter.
Open with
I would keep a stock row per SKU with on hand and a reserved counter, plus a reservations table with id, SKU, quantity, order id and line, status and expires_at, with a unique constraint on order and line.
Then, if they want more
Available is on hand minus reserved. Reserve runs in one transaction: insert the reservation with ON CONFLICT DO NOTHING, so a retry returns the existing reservation, then a conditional update that adds to reserved only where on hand minus reserved is enough. Zero rows means out of stock and the whole transaction rolls back.
Checkout is a saga driven by the order service: after payment succeeds it calls confirm, which reduces on hand and reserved; on failure or cancellation it calls release as the compensation. Both only act on ACTIVE reservations, so they are idempotent.
For abandoned checkouts, a job every minute expires ACTIVE reservations past expires_at, claimed with FOR UPDATE SKIP LOCKED so several instances can share the work, and I might add delayed messages too. If payment succeeds just after expiry, we try to re-reserve or refund. Stock changes go through an outbox to Kafka so search and product pages stay current.
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 official documentation for the version you use. 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 "Design an inventory reservation service for an online store where items are reserved...": https://scrivio-interview-library.netlify.app/architecture/backend-system-design/857fb01dcc/