Practice: Design a Coupon Code Service
Coupon service inspired by Amazon and Swiggy promo codes: a code can be redeemed only as many times as allowed, checks are cached, and usage counts are updated in the background.
Build this system yourself on the canvas below. Add components, connect them, and run traffic simulations to see where your design holds up and where it breaks.
Functional requirements
- Check whether a code is valid for an order: exists, not expired, applies to the items, and the user is eligible
- Redeem a code only as many times as it allows: a single-use code is redeemed at most once, even when two checkouts try at the same moment
- Keep a per-user and per-code usage count to enforce limits such as one use per customer
- Reject an expired or exhausted code immediately
- Create, pause and expire codes for the business, with usage reports
- Limit how fast one client can try codes, to stop guessing attacks
Non-functional requirements
- Code check < 50ms p99, served from the code cache
- Redeem accepted < 100ms p99
- A single-use code is never redeemed twice, even under retries and races (atomic check-and-decrement in the database)
- Handle 10K requests per second at peak: about 8K checks and 2K redemptions per second
- An expired or paused code is rejected within 10 seconds of the change
- 99.99% durability: a redemption is never lost; scale to 1M redemptions per minute in a flash sale
- 99.99% availability for checks, because checkout depends on them