Explain SQL transaction isolation levels and choose one for an inventory reservation workflow.
Flipkart · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, SQL, System Design
Community answers
Posted by wasAsked editorial collection
Inventory deduction needs a transaction that locks or conditionally updates the specific tenant row, then verifies the affected row count. A single atomic update often avoids read-then-write races.
Example:
UPDATE Inventory
SET Quantity = Quantity - @Amount
WHERE RestaurantId=@RestaurantId AND OutletId=@OutletId
AND ProductId=@ProductId AND Quantity >= @Amount;
If zero rows were affected, report insufficient stock or missing scope. Keep transactions short and access rows in a consistent order to reduce deadlocks. Snapshot isolation improves many reads but does not replace write conflict handling. Add an idempotency key for retried orders and a stock-movement ledger so balances are auditable and repairable.
Backbencher Developer · · 0 likes
Isolation levels control what one transaction can see of other concurrent transactions, trading consistency against concurrency.
Anomalies they prevent:
- Dirty read: reading uncommitted data from another transaction.
- Non-repeatable read: reading the same row twice and getting different values.
- Phantom read: re-running a query and seeing new rows.
- Lost update: two transactions read the same value and both write, so one update disappears.
Levels (SQL Server naming, similar in other databases):
- Read Uncommitted: allows dirty reads. Rarely appropriate.
- Read Committed (default): no dirty reads, but non-repeatable reads and phantoms are possible. With READ_COMMITTED_SNAPSHOT on, readers see the last committed version without blocking writers.
- Repeatable Read: rows you read stay locked until commit, but phantoms can appear.
- Serializable: behaves as if transactions ran one at a time; range locks prevent phantoms. Most correct, least concurrent, deadlock-prone.
- Snapshot: each transaction sees a consistent snapshot; write conflicts raise an update-conflict error instead of blocking.
Inventory reservation workflow
The danger is overselling: two customers reserve the last unit at the same time. Raising the isolation level is not the only fix — the safest design is an atomic conditional update:
UPDATE Inventory
SET Available = Available - @qty, Reserved = Reserved + @qty
WHERE ProductId = @id AND Available >= @qty;
If the affected row count is 0, the item is out of stock. This works correctly under Read Committed because the row is locked during the update, and it keeps transactions short.
Alternatives:
- Optimistic concurrency with a rowversion column and retry on conflict.
- Read then update with UPDLOCK, ROWLOCK hints inside a transaction.
- Serializable or Snapshot only when the reservation logic spans several rows or tables that must stay consistent, accepting more retries or deadlocks.
Also add reservation expiry (release stock if payment does not complete in N minutes) and idempotency keys so retries do not reserve twice.