33. Design a Notification System
1
The interview prompt2
33.1 What do you ask first?3
33.2 How big is this, really?4
33.3 What are the doors?5
33.4 What do we store?6
33.5 What does the whole system look like?7
33.6 Why one queue per channel?8
33.7 How do retries and de-duplication work?9
33.8 How do user preferences work?10
33.9 What makes OTPs special?11
33.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab33. Design a Notification System
33.9 What makes OTPs special?
3 min read+10 XP
Figure: an OTP is short-lived, single-use and guarded at every step.
The OTP flow, with the chapter's code checking each rule:
- Generate 6 digits with a cryptographic random generator (not
random). - Store only a hash (an HMAC of phone + code with a server secret), with a 5-minute expiry. The code never appears in the database or logs.
- Single use: delete on success; reusing the code fails.
- Limit attempts: 5 wrong tries lock the code. With 5 tries on a 6-digit code, an attacker's chance is 1 in 200,000; limits per phone and per IP (Chapter 25) stop them requesting fresh codes endlessly. In the code, a 4th request within an hour is refused.
- Send fast and fail over: the dedicated queue, plus a second SMS provider if there's no delivery receipt in a few seconds.
- Drop late jobs: don't send a code that has already expired.
- Offer something better than SMS where possible. SMS OTPs are vulnerable to SIM swaps and interception; NIST's digital identity guidelines classify SMS/phone-network OTPs as a restricted authenticator. Offer authenticator apps or passkeys, and use SMS as a fallback.
Interviewer pushback:
- "Someone requests OTPs for random phone numbers to run up your SMS bill." → That's SMS pumping fraud: rate-limit per phone, per IP and per device, require a CAPTCHA after a few requests, block number ranges and countries you don't serve, and alert on unusual SMS volume by destination.
Memory code: "Random, hashed, short-lived, single-use, and sent through its own door."