Lesson 10 of 15
33. Design a Notification System

33.9 What makes OTPs special?

3 min read+10 XP

request, generate, store a hash, send through the fast lane, verify with limited attempts.

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."