SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 35
Design a login and authentication system
A consumer app with web and mobile clients needs accounts. People sign up with an email and password, expect to stay signed in on their phone for weeks, and want to sign out of every device when a laptop is stolen. About 30 backend services need to know who is calling on every request.
Requirements
- Sign up with email and password, with email verification.
- Log in and receive credentials that every backend service can check on each API call.
- Stay signed in on a device for up to 60 days without re-entering a password; users can revoke one session or all sessions.
- Optional TOTP multi-factor authentication, and password reset by email link.
Constraints and assumptions
- 50M accounts, 5M daily active users; peak 2,000 logins/s and 200,000 authenticated API requests/s across all services.
- Revocation (sign out everywhere, password change, admin lock) must take effect on every service within 60 seconds.
- Checking credentials on an API request may add at most 2 ms p99 inside a service.
- Expect credential stuffing: bursts of 10,000 login attempts/s from rotating IPs using leaked email and password pairs.
- If the user database leaks, passwords must not be practically recoverable.
Out of scope for the first sketch
Acting as an identity provider for third-party apps · Authorization rules and permissions · Enterprise SSO (SAML, OIDC federation) · Passkeys (a later follow-up could add them)
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a login and authentication system
Finish your first sketch before answering these. Cross out freely.
- What exactly do you store for a password, and how expensive should checking one be?
- A user taps "sign out of all devices". Walk through what happens to a phone that still holds a valid access token.
- Login traffic jumps to 10,000 attempts/s from 50,000 IPs, each trying one or two accounts. How do you stop it without locking real users out?
- A refresh token is stolen. The attacker and the real app both keep using it. Can you notice, and what do you do?
- Design the password reset link so that an old, forwarded or leaked link cannot take over the account.
Compare afterwards: worked approaches and reading at sketchbook /questions/authentication/review