Design an inventory system that reserves an item when a user adds it to a shopping cart. The system must prevent overselling under concurrent requests.
Focus on the database: explain transaction boundaries, locking, race conditions, and deadlocks. Then explain how you would handle an increase to ten times the average transactions per second. Clarify reservation lifetime, cart changes, and checkout behavior rather than assuming an unspecified policy.
### What a Strong Answer Covers
- An inventory invariant that every stock-changing operation preserves.
- Atomic reservation creation and idempotent request handling.
- Safe interaction among checkout, cancellation, expiration, and stock updates.
- Lock order, short transactions, and recovery from aborted transactions.
- Scaling choices that preserve the same stock guarantee, including hot-item contention.
### Follow-up Questions
- What happens when checkout and the expiration worker act on the same reservation at the same time?
- Why would adding application servers or read replicas fail to solve contention on one popular item's stock row?
Overview: Design cart-time inventory reservations that prevent overselling, coordinate checkout and expiration, avoid races, and handle tenfold transaction growth.
Design an inventory system that reserves an item when a user adds it to a shopping cart. The system must prevent overselling under concurrent requests.
Focus on the database: explain transaction boundaries, locking, race conditions, and deadlocks. Then explain how you would handle an increase to ten times the average transactions per second. Clarify reservation lifetime, cart changes, and checkout behavior rather than assuming an unspecified policy.
What a Strong Answer Covers Guidance
An inventory invariant that every stock-changing operation preserves.
Atomic reservation creation and idempotent request handling.
Safe interaction among checkout, cancellation, expiration, and stock updates.
Lock order, short transactions, and recovery from aborted transactions.
Scaling choices that preserve the same stock guarantee, including hot-item contention.
Follow-up Questions Guidance
What happens when checkout and the expiration worker act on the same reservation at the same time?
Why would adding application servers or read replicas fail to solve contention on one popular item's stock row?