Design an Inventory Reservation System

System Design
Medium
Amazon
72.3K views

Design a service to temporarily reserve limited inventory (e.g., seats, products) during a high-traffic checkout process. Focus on time-based expiration and rollback.

Why Interviewers Ask This

Interviewers at Amazon ask this to evaluate your ability to design distributed systems that handle high concurrency and data consistency. They specifically want to see how you manage race conditions when multiple users attempt to reserve the same limited inventory simultaneously, and how you implement time-based expiration without blocking the checkout flow.

How to Answer This Question

1. Clarify requirements: Define scale (requests per second), latency targets, and specific failure scenarios like network partitions or payment timeouts. 2. Model the core entity: Define the Inventory Item with fields for total stock, reserved count, and a unique reservation ID with an expiration timestamp. 3. Design the API: Create endpoints for 'reserve' and 'release', ensuring idempotency to prevent double-charging if retries occur. 4. Address concurrency: Propose using Redis with atomic operations (like INCR/DECR) or database optimistic locking to handle simultaneous requests safely. 5. Implement expiration: Suggest using Redis keys with TTL or a background cron job to automatically release expired reservations back to available stock. 6. Handle rollbacks: Detail how to trigger a rollback if the user abandons the cart after the timeout window, ensuring stock is never permanently lost due to ghost reservations.

Key Points to Cover

  • Demonstrating understanding of race conditions in high-concurrency environments
  • Proposing atomic operations (like Lua scripts or optimistic locking) to ensure data integrity
  • Leveraging Redis TTLs for automatic, efficient expiration of temporary reservations
  • Designing a clear rollback strategy to restore inventory upon timeout or failure
  • Balancing performance with durability through async writes and caching layers

Sample Answer

To design this system, I would first clarify that we need to support flash sales where thousands of users hit the checkout button simultaneously. The core challenge is preventing overselling while minimizing latency. I propose a hybrid approach using a fast in-memory store like Redis for the reservation logic and a durable database for final state. For the reservation API, the client sends a request with a product ID and quantity. We generate a unique reservation token. Before decrementing stock, we check if sufficient inventory exists. To handle concurrency, we use Redis's Lua scripting to atomically decrement the reserved count only if it doesn't exceed the total. This prevents race conditions better than simple read-modify-write cycles. Crucially, every reservation must have a Time-To-Live (TTL). When a user initiates checkout, we set a 10-minute TTL on the reservation key in Redis. If the user completes payment within this window, we confirm the order and delete the key. If they don't, Redis automatically expires the key, triggering a callback or event to increment the available stock back to its original level. For durability, we write the reservation attempt to an asynchronous queue. A consumer service processes the queue to update the primary database, ensuring we have an audit trail even if the cache fails. Finally, we add a fallback mechanism where a scheduled job scans for stale reservations older than the TTL but not yet released, acting as a safety net for any edge cases.

Common Mistakes to Avoid

  • Ignoring race conditions by suggesting simple database updates without locks or atomic transactions
  • Failing to define a specific expiration mechanism, leading to permanent inventory loss
  • Over-engineering the solution by using complex databases instead of appropriate in-memory caches for temporary states
  • Not addressing what happens when the payment gateway times out or the user navigates away unexpectedly

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 150 System Design questionsBrowse all 73 Amazon questions