Enforce a Limited-Quantity Coupon Cap With a Conditional SQL Update
Last updated: November 29, 2026
Quick Overview
A promotional coupon is limited to its first N redemptions, but under load it gets redeemed more times than allowed because the service reads the remaining count, checks it in code, then writes it back. Rewrite redemption so the database enforces the cap and one use per customer atomically.
Amazon
November 29, 20260
0
0 solved
A promotional coupon is limited to its first N redemptions, but under load it gets redeemed more times than allowed because the service reads the remaining count, checks it in code, then writes it back. Rewrite redemption so the database enforces the cap and one use per customer atomically.
Amazon promotions such as limited first-N coupons attract bursts of simultaneous redemptions, which exposes read-modify-write races immediately and costs real money. The interviewer checks that you push the invariant into a single conditional statement inside a transaction, rather than adding application locks that do not work across hosts.
What the Interviewer Expects
- Identifies the read, check, write race across concurrent requests
- Uses an UPDATE with the condition in the WHERE clause and checks rows affected
- Wraps the decrement and redemption insert in one transaction
- Uses a unique key to block repeat use by the same customer
- Adds a CHECK constraint as a final safety net
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you do the same with DynamoDB?
- The single coupon row becomes a hot spot; how would you scale it?
- How do you return a use to the pool if the order is cancelled?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Each coupon has a fixed number of uses; never exceed it.
- One redemption per customer per coupon.
- Return
REDEEMED,EXHAUSTED, orALREADY_REDEEMED....
High-Level Design
Replace read-then-write with UPDATE ... SET remaining = remaining - 1 WHERE coupon_code = ? AND remaining > 0. The database evaluates the condition and the write atomically on the row, so two reques...