Practice: Design an OTP Service
One-time password service inspired by login codes from banks and apps: codes expire quickly, wrong guesses are limited, and delivery by SMS or email happens in the background.
Build this system yourself on the canvas below. Add components, connect them, and run traffic simulations to see where your design holds up and where it breaks.
Functional requirements
- Generate a random one-time code for a user on request and tie it to that user and purpose (login, payment, reset)
- Deliver the code by SMS or email in the background so the request never waits on the provider
- Verify a code a user types in: it must match, be unexpired, and not have been used before
- Expire a code after 5 minutes and make it single-use, so a replay is rejected
- Allow at most 5 wrong tries per code, then lock the code and make the user request a new one
- Limit how many codes one phone number or email can request per hour, to stop SMS-pumping abuse
Non-functional requirements
- Code request accepted < 100ms p99; delivery to the phone within 10 seconds for 99% of codes
- Verify < 100ms p99, served from the code cache so the database is not on the hot path
- Handle 4K requests per second at peak: about 1.2K code requests and 2.8K verifications
- A code can never be verified twice, even when two verify calls arrive at the same moment (atomic check-and-consume)
- Codes are stored hashed and never written to logs
- A delivery provider outage must not lose requests: they wait in the queue and retry
- 99.99% availability for verify, since users are blocked from logging in without it