Design a Coupon Generation and Redemption Service
Design campaign codes and reliable coupon application when many shoppers redeem concurrently.
Problem statement
For this exercise, assume a merchant creates a coupon campaign with a validity window, a percentage discount, a total redemption limit, and an optional per-customer limit of one. The service generates unique codes for the campaign.
At checkout, a shopper supplies one code and the service decides whether it can be applied to that cart, returning a discount quote. A successful checkout confirms one redemption; an abandoned or failed checkout must not consume a redemption permanently. Design the generation, validation, and application service.
Explain how you would scale it and remove single points of failure while preventing a campaign from exceeding its limit. The checkout system owns payment and order state; the coupon service receives an idempotent checkout confirmation or cancellation.
What to build
- Create a bounded campaign and generate codes that are unique within it.
- Validate a code against the campaign and cart, then return a discount quote or a clear rejection.
- Confirm one redemption for a completed checkout and release an unconfirmed reservation after cancellation or expiry.
At scale
- Assume 100,000 active campaigns and 10 million generated codes; code lookup must avoid a full scan.
- Assume a flash sale brings 20,000 validation or reservation attempts per second for one campaign; concurrent use must not exceed its limit.
- Assume a reservation expires after five minutes unless checkout confirms it; retries may arrive after a network timeout.