Prevent Overselling Limited Stock With Optimistic Concurrency

Last updated: November 29, 2026

Quick Overview

Many buyers try to reserve the last units of a limited item at once. Implement a reservation function using version-based optimistic concurrency and explain when you would switch to pessimistic row locks.

Rakuten
Software Engineering Fundamentals
Software Engineer
Rakuten
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


Many buyers try to reserve the last units of a limited item at once. Implement a reservation function using version-based optimistic concurrency and explain when you would switch to pessimistic row locks.

Timed deals on Ichiba produce bursts of concurrent purchase attempts on the same few SKUs, and overselling means cancelling paid orders and angry customers. The interviewer checks race condition reasoning, compare-and-set, retry design and database locking trade-offs.

What the Interviewer Expects
  • Identify the read-modify-write race
  • Implement compare-and-set with a version column
  • Retry on conflict with a bound and backoff
  • Translate the approach to SQL
  • Compare optimistic and pessimistic locking under contention
Key Topics to Cover
concurrency
optimistic locking
compare-and-set
database transactions
race conditions
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • Write the SQL for both the optimistic and the SELECT FOR UPDATE versions.
  • Under extreme contention retries explode. What would you do instead?
  • How do you release reservations that never turn into orders?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements

reserve(sku, qty) must never let stock go negative, even with hundreds of concurrent callers. It returns True if reserved and False if out of stock. In production, stock lives in a database row; her...

High-Level Design

The naive version reads stock, checks it, and writes stock - qty. Two buyers can both read 1 and both write 0, selling two units. Optimistic concurrency adds a version: read (stock, version), then...


Submit Your Answer
Markdown supported

Related Questions