Design e-commerce inventory availability and order reservation without overselling under concurrent requests. Define on-hand, reserved, and available invariants; atomic multi-item placement; idempotent retries; expiration; and optimistic versus pessimistic control for hot SKUs.
## Design Inventory Availability and Order Placement
Design the inventory portion of an e-commerce system with two core operations:
1. Query the currently available quantity for a product.
2. Place an order for one or more units without overselling under concurrent requests.
Focus on data contention, transactional boundaries, and the tradeoffs between optimistic and pessimistic concurrency control.
### Constraints & Assumptions
- Inventory is tracked per product SKU at one logical fulfillment location.
- A placement request has an idempotency key and either reserves all requested units or none.
- Concurrent orders may target the same popular SKU.
- Canceled or expired orders release their reservation.
- Payment processing is outside scope, but reservation state must support a later confirm or cancel operation.
### Clarifying Questions to Ask
- May availability reads be slightly stale, or must they reflect every committed reservation?
- Is an order for multiple SKUs atomic across all items?
- How long does a reservation live before expiration?
- What is the expected contention level for the hottest products?
- Should a failed placement return the exact unavailable SKU and current quantity?
### Hints
- Distinguish on-hand, reserved, and available inventory.
- Make the capacity check and reservation write one atomic decision.
- Compare retry behavior under optimistic control with lock wait behavior under pessimistic control.
### What a Strong Answer Covers
- Inventory and reservation/order records with clear invariants.
- Concrete query, place, confirm, cancel, and expire contracts.
- Atomic no-oversell logic and idempotent retries.
- Optimistic versus pessimistic behavior under low and high contention.
- Multi-SKU deadlock/order concerns, expiration, observability, and failure recovery.
### Follow-up Questions
- How would you prevent deadlocks when an order contains several SKUs?
- When would a database row lock outperform optimistic retries?
- How would you reconcile inventory after a crash between reservation and event publication?
- What availability should the query show while a reservation is pending payment?
Quick Answer: Design e-commerce inventory availability and order reservation without overselling under concurrent requests. Define on-hand, reserved, and available invariants; atomic multi-item placement; idempotent retries; expiration; and optimistic versus pessimistic control for hot SKUs.