Easy · Telecom and messaging
One-time code by text
Apps ask you to send a six-digit code to a phone. The code must arrive in seconds, a person must never get it twice, and nobody may use your system to spam a number. Texts go through a carrier partner with a hard speed limit and a real price per message. Practise this easy system design problem for free with a Google or GitHub account.
The brief
Apps ask you to send a six-digit code to a phone. The code must arrive in seconds, a person must never get it twice, and nobody may use your system to spam a number. Texts go through a carrier partner with a hard speed limit and a real price per message.
Think of the login code text that arrives in seconds.
Users, scale and requirements
- Who uses it
- 3 million people a month (approx.)
- Audience in plain numbers
- 3,000,000 people per month
- Normal peak
- ≈ 40 requests per second
- Request mix
- Notification: 20%; Write: 30%; Read: 50%
- Speed for 99 of 100 requests
- Write: ≈ 400 ms; Read: ≈ 400 ms
- Successful requests
- 99.9%
- Data safety
- At most 1 minute of writes may be lost.
- Monthly budget
- ≈ $94,000
- Data at rest
- ≈ 0.2 TB
- Where users are
- North America: 50%; Europe: 30%; Asia Pacific: 20%
- Home region
- North America
- Waiting deadlines
- Notification: 10 seconds; Write: 3,600 seconds; Read: 900 seconds
≈ marks an approximation, just as in the game. Waiting deadlines measure the age of queued work and are separate from interactive response time.
- The carrier partner accepts about 100 texts a second per sender in this scenario (approx.).
- Each text costs about a cent (approx.). Every code is a real bill.
- All numbers are rough approximations.
- Notification speed measures provider acceptance. Device receipt is outside this traffic model; check expiry, provider outcome and retries in the correctness trace before claiming delivery guarantees.
Questions that shape the design
How fast must the code arrive?
Within about 10 seconds of the request. Late codes are as bad as none.
Can the same request be sent twice?
Apps retry when they are unsure. Send each code only once.
What stops abuse?
Limit how many codes one phone number can get in a few minutes.
The traffic day
Consumer traffic, busiest in the evening. The clock covers 00:00 to 24:00 in the home region. Events add pressure on top of that normal traffic.
09:00 · Provider rate limit
An outside service lowers its configured request quota and answers 'too many requests' above the reduced limit.
Provider capacity falls to 80% of its configured quota. Duration: ≈ 3,600 seconds.
13:00 · Duplicate delivery
The queue redelivers messages. Without duplicate protection, side effects may be applied twice.
Affected share: 5%. Duration: ≈ 3,600 seconds.
What a strong design has to handle
- The carrier has a speed limit and a code that arrives late is useless. What smooths a burst without delaying everyone?
- A queue can delay a code when the carrier slows. Giving codes their own queue keeps marketing texts from waiting in the same line.
- Strict per-number limits stop abuse but may block a real person who retries a few times.
Make a prediction, run a full traffic day, then compare the result. The in-game editorial opens after a finished day and includes measured reference designs.
Concepts to practise
Related problems
- Hard
Bulk text campaigns
Businesses send a coupon or an alert to hundreds of thousands of phones at once. Carriers accept texts only at a fixed speed, every text costs real money, and a person who replied STOP must never be texted again. Delivery receipts come back by the thousand and must be counted.
- Hard
Concert ticket drop
Tickets for a huge tour go on sale at 10:00 sharp. Hundreds of thousands of fans arrive in the same minute, bots arrive earlier, and everyone refreshes when the page stalls. There are 40,000 seats. Sell each one once, charge each buyer once, and keep the site standing.
- Medium
Rate limiter for a public API
Developers call your public API from scripts and apps. A few misbehaving or malicious clients can crowd out everyone else. Build limits that are fair, fast and cheap, and that survive a retry storm.
- Medium
Notification service
Every other team asks you to send messages: order shipped, new login, a sale starting. You send them by push, email and text through outside providers who have their own limits. Send each message once, and never block the team that asked.