Design an OTP and MFA Service
Understanding the Problem
🔢 What is an OTP and MFA service? A one-time password (OTP) is a short code, usually six digits, that proves a user has something: a phone that can receive an SMS, an inbox, or an authenticator app holding a shared secret. Multi-factor authentication (MFA) asks for one of these on top of a password, so a stolen password alone isn't enough. An OTP service generates, delivers and verifies these codes, and an MFA service manages which factors each user has enrolled.
The same machinery covers several requests: "verify a phone number at signup," "log in with a code instead of a password," "confirm this payment," and "add Google Authenticator as a second factor." They differ only in the purpose the code is bound to and the channel it travels over, so we'll design one service.
Functional Requirements
Core Requirements
- Users should be able to request a one-time code by SMS or email and enter it to complete a login or confirm an action.
- Users should be able to enroll an authenticator app and use its time-based codes as a second factor.
- Codes should expire after a few minutes, and the system should limit wrong guesses and how often codes can be resent.
Below the line (out of scope)
- The rest of login (passwords, sessions, tokens). See /system-design/auth-service.
- Push-notification approval and hardware security keys. We'll mention where they fit.
- Building an SMS gateway. We use third-party providers.
Non-Functional Requirements
Ask about scale and risk. Let's say 50 million users and 20 million code verifications a day.
Core Requirements
- The system must make guessing a code impractical: the chance of an attacker getting into an account by guessing should stay well under one in a million per day.
- Verifying a code should take under 100 ms, and an SMS or email should arrive within about 10 seconds for most users.
- Codes must be single-use, short-lived, and bound to the purpose they were issued for.
- The service should be highly available. If it's down, users with MFA can't log in at all.
- The system should keep SMS costs and fraud under control.
Below the line (out of scope)
- Per-country telecom regulations (sender ID registration and the like).
The traffic is small. 20M verifications a day is about 230 per second, with peaks perhaps 10x higher, which one Redis cluster and a small stateless fleet handle easily. The math that matters is about guessing. A six-digit code has 1,000,000 possible values. If we allow 5 wrong tries per code, a single code gives an attacker a 5 in a million chance. But if they can request a fresh code every few minutes, say 10 per hour, they get 50 guesses an hour, about 1,200 a day, and roughly a 0.12% chance per day of getting in. Across a million targeted accounts, that's over a thousand takeovers a day.
The per-code attempt limit sounds like the defense, but the real limit has to be on the account: total wrong guesses across all codes in a time window. That's the number your interviewer is listening for.
The other number is money. An SMS costs on the order of a cent in the US and much more to some countries. Attackers exploit this by triggering codes to premium-rate numbers they get paid for (SMS pumping), so "send a code" is an endpoint that costs us real money every time someone calls it.