Design a Coupon System
Understanding the Problem
🎟️ What is a coupon system? A coupon system lets a business hand out discounts and control exactly who can use them, when, and how many times. A shopper types
BLACKFRIDAY20orWELCOME-7KQ9-M2XDat checkout and gets money off. Behind the code is a campaign with rules: a time window, eligible products, a minimum spend, and limits like "10,000 uses in total, once per customer, once per card."
You'll also see this asked as "design a promo code service," "design a discount engine," or "design a referral program." Referral codes are coupons with a twist: the code belongs to a user, and redeeming it rewards two people. We'll cover all of them.
Functional Requirements
Core Requirements
- Marketers should be able to create campaigns with a discount, a time window, eligibility rules and redemption limits (global, per user, per payment card), using either one shared public code or millions of unique single-use codes.
- Shoppers should be able to apply a code to their cart and see whether it's valid and what it takes off.
- The system should redeem codes when an order is placed, never exceeding any limit, and give the use back if the order fails or is cancelled.
- Users should be able to share a personal referral code; when a new user signs up with it and completes a first order, both get a reward.
Below the line (out of scope)
- Automatic promotions with no code (buy 2 get 1 free applied to everyone), though the rule engine handles them the same way.
- Loyalty points and gift cards. They're stored value, closer to a digital wallet.
- Marketing analytics on campaign performance, beyond emitting events.
Non-Functional Requirements
Ask about scale and, more importantly, about how strict the limits are. Is "10,000 uses" a hard cap the finance team will audit, or a soft target? We'll assume hard caps. For scale: 50M users, and a sale-day peak of 5,000 orders per second, 40% of them with a code.
Core Requirements
- The system must never exceed a campaign's limits, including under heavy concurrency on a single hot code.
- Validating a code in the cart should take under 50 ms at p99, so it doesn't slow down checkout.
- The system should handle about 2,000 redemptions and 20,000 validations per second at peak.
- Codes must be impossible to guess, and the system must resist abuse: one person with many accounts, stolen code lists, self-referrals.
- Redemptions should stay consistent with orders: a use is counted only for orders that went through.
Below the line (out of scope)
- Multi-region active-active redemption counters. Each campaign's counters live in one region.
- Tax treatment of discounts.
The back-of-the-envelope:
- Redemptions. 5,000 orders/s × 40% = 2,000 per second at peak.
- Validations. Shoppers apply, remove and re-apply codes, and every cart refresh re-validates, so assume about 10 validations per redemption: 20,000 per second.
- Codes. A large retailer might issue 100M unique codes a year (mailers, apology credits, partner promotions). At ~100 bytes per row with its index, that's ~10 GB a year. Small.
- The hot spot. Campaigns are not evenly used. On a sale day, one public code like
BLACKFRIDAYcan be on most of those 2,000 redemptions per second, and all of them increment the same global counter.
The data is small and the overall rate is moderate. The design is driven by two things: a single counter that thousands of requests want to bump at once, and limits on several dimensions (global, user, card) that all have to hold together.
A good clarifying question: "When a coupon is used on an order that's later refunded, does the use come back?" Different businesses answer differently, and the answer changes the redemption state machine. Asking it shows you're thinking about the lifecycle, not just the happy path.