SYSTEM DESIGN · DESIGN PROBLEMS · PLATFORM SERVICES · MEDIUM · 43 OF 51

Design an Authentication System: Passwords, Sessions vs JWT, Refresh Tokens, MFA and SSO

Design login for a large consumer app: sign-up and login, slow password hashing and what it costs in CPU, opaque sessions versus signed tokens, short access tokens with rotating refresh tokens and reuse detection, a safe forgot-password flow, MFA with TOTP and passkeys, social login and enterprise SSO with OIDC, signing key rotation, brute-force defence and audit.

Try it on the practice board Drag the blocks, wire them up, and check your design against the reference.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
1 / 19

Press play to hear the narration, or step through at your own pace.

Notes

Listen

Listen. Users sign up with an email and password, log in and out, and can sign out of every device. They stay signed in for thirty days. They can reset a forgotten password, turn on multi-factor with an authenticator app or a passkey, and log in with Google, Apple or their company's single sign-on. Deciding what a user may do, which is authorization, is a separate problem; we only prove who they are.

The qualities. We never store anything that can be turned back into a password quickly, even if the whole database leaks. A stolen session must be revocable within minutes. Every API call checks a token, so that check should take under a millisecond, ideally without a database hop. We must resist credential stuffing and guessing. And availability is critical: if login is down, the whole product is down.

Functional
  • sign up with email + password
  • log in, log out, log out of
  • all devices
  • stay signed in for 30 days
  • forgot password by email
  • MFA: authenticator app,
  • passkeys, SMS as a fallback
  • log in with Google or Apple
  • enterprise SSO for work accounts
  • out of scope: permissions (authz)
Non-functional
  • never store a usable password
  • stolen DB must not reveal
  • passwords quickly
  • revoke a session within minutes
  • token check on every API call:
  • < 1 ms, no DB hop if possible
  • resist credential stuffing
  • and brute force
  • login p99 < 500 ms
  • 99.99%: no login = no product

Ask scale

Scale. A hundred million users, twenty million daily. Most stay signed in, so real logins are about five million a day, sixty a second, six hundred at peak. Here is the interesting number: a good password hash deliberately costs about a hundred milliseconds of CPU, so six hundred logins a second need sixty cores. An attack at ten times that would melt us, so rate limits run before hashing. Token checks are far busier: a hundred fifty thousand a second.

Back of the envelope
100M users, 20M daily active
logins: 5M/day ≈ 60/s, peak 600/s
hash cost ≈ 100 ms of one core
  600/s × 0.1 s = 60 cores at peak
  attack ×10 → limit before hashing

API calls: 20M × 200 = 4B/day
  ≈ 46k/s, peak ≈ 150k/s checks
refresh: 15 min tokens
  ≈ 80M/day ≈ 900/s

Define API

The API is small. Sign up and log in with an email and password. Login either returns tokens, a four oh one, or an MFA challenge that the client completes with a code. Refresh swaps a refresh token for a new pair. Logout can end one session or all of them. Forgot and reset handle lost passwords. Every endpoint is rate limited per IP and per account, because each one is an attack surface.

API
POST /v1/signup  { email, password }
POST /v1/login   { email, password }
  → 200 tokens | 401 | mfa_required
POST /v1/mfa/verify { challenge, code }
POST /v1/token/refresh
  { refresh_token } → new pair
POST /v1/logout   { all_devices? }
POST /v1/password/forgot { email }
POST /v1/password/reset
  { token, new_password }

Data model

Data model. Users live in SQL with a unique email, a verified flag, the password hash and the hashing parameters used. MFA secrets are encrypted, recovery codes hashed. Social and SSO logins map a provider and subject to a user. Refresh and reset tokens are stored only as hashes, like passwords, so a database leak does not hand out live sessions. Login events feed risk checks and the audit trail.

Data model
users(id, email UNIQUE, verified,
  pw_hash, pw_params, status)
mfa(user_id, type, secret_enc,
  recovery_hashes)
identities(provider, subject,
  user_id)   UNIQUE(provider,subject)
refresh_tokens(hash, family_id,
  user_id, device, expires, used)
reset_tokens(hash, user_id, exp)
login_events(user, ip, ok, at)

High-level design

Trace a login. The request reaches the auth service, which loads the user's password hash and verifies it. If MFA is on, it returns a challenge first. On success it issues two things: a short access token, valid fifteen minutes and signed with a key from the key manager, and a long-lived random refresh token whose hash is stored in the session store. On the web both go in secure, HTTP-only cookies; on mobile, in the keychain.

Now every other request. The gateway checks the access token's signature with a cached public key, plus its expiry, issuer and audience. That takes microseconds and needs no call to the auth service, which is why a hundred fifty thousand checks a second are cheap. The gateway forwards the verified user id to product services, which simply trust it. The auth service only sees logins and refreshes.

Deep dive: passwords

Deep dive one: passwords. Never encrypt them and never use a fast hash like SHA-256. Use a slow, memory-hard function such as argon2id, tuned to about a hundred milliseconds, with a unique random salt per user, so identical passwords produce different hashes and an attacker must crack each one separately. Store the parameters beside the hash, and when you raise them, rehash at the user's next successful login. Reject passwords found in breach lists.

Storing passwords
  • argon2id (or bcrypt, scrypt):
  • slow and memory-hard on purpose
  • unique random salt per user:
  • same password, different hash
  • optional pepper: secret key in
  • the key manager, not the DB
  • store params with the hash;
  • rehash on login when raised
  • check new passwords against
  • known breached lists

Deep dive: sessions

Deep dive two, the classic question: sessions or JWTs? An opaque session id is a random key into a store: revoking it is instant, but every request needs a lookup. A signed token carries the user id and expiry, so anyone with the public key can verify it with no lookup, but once issued it stays valid until it expires. The usual answer is both: a short fifteen-minute signed access token, and an opaque refresh token in the database that we can delete at any time.

Sessions vs signed tokens
  • opaque session id:
  • + revoke = delete one row
  • - a store lookup per request
  • signed token (JWT):
  • + verify locally, no lookup
  • - valid until it expires,
  • cannot be recalled
  • hybrid (our choice):
  • 15 min signed access token
  • + opaque refresh token in DB
  • revocation lag ≤ 15 min

Deep dive: refresh

Refresh tokens are long-lived, so they get extra care. Each refresh returns a new pair and marks the old refresh token as used, within a family that started at login. If a used token ever comes back, two parties hold copies, so one is a thief: revoke the entire family and make the user log in again. Logging out of all devices, or changing the password, deletes every family. Allow a few seconds of grace for genuine network retries.

Rotation and reuse detection
refresh R1 → issue A2 + R2,
  mark R1 used (same family)
R1 presented again later?
  someone copied it
  → revoke the whole family
  → force login, alert user
logout all devices:
  delete every family for user
password change → same
small grace window for retries

Deep dive: reset

Deep dive three: forgot password. Always give the same reply, whether or not the email exists, so attackers cannot discover accounts. The link carries thirty-two random bytes; we store only its hash, it expires in thirty minutes and works once. After a successful reset, revoke every session and email the user a notice, in case it was not them. If MFA is on, still ask for it, or the reset link becomes a way around the second factor.

Forgot-password flow
  • always reply "if the account
  • exists, we sent a link"
  • (no user enumeration)
  • token: 32 random bytes, store
  • only its hash, expires 30 min
  • single use; new one voids old
  • after reset: revoke all
  • sessions, email a notice
  • MFA still required after reset
  • rate limit per email and IP

Deep dive: MFA

Deep dive four: multi-factor. An authenticator app shares a secret with us at setup; both sides compute a code from that secret and the current thirty-second window, and we accept one step of clock drift but never the same code twice. Passkeys are stronger: a key pair on the device, bound to our domain, so a phishing site cannot use it. SMS is the weakest because of SIM swaps. Give hashed single-use recovery codes, and ask again for risky actions like changing email.

Second factors
  • TOTP app: shared secret,
  • code = HMAC(secret, time/30s)
  • accept ±1 step, block reuse
  • passkeys (WebAuthn): device key
  • pair, bound to our domain,
  • phishing resistant
  • SMS: SIM swap risk, fallback only
  • recovery codes: 10, hashed,
  • single use
  • step-up MFA for risky actions

Deep dive: OAuth and SSO

Deep dive five: social login and SSO, using OpenID Connect. The app sends the user to the provider, gets back a short code, and our server swaps that code for an ID token, using PKCE so a stolen code is useless. Check the state and nonce values, then verify the token's signature, issuer, audience and expiry. Key users by provider and subject, and only link to an existing account when the email is verified. For work accounts, route by email domain to the company's identity provider.

OIDC checklist
  • authorization code flow + PKCE
  • check state (CSRF) and nonce
  • verify ID token: signature,
  • issuer, audience, expiry
  • key users by (provider, sub),
  • not by email alone
  • link to an existing account
  • only if email is verified
  • SSO: route by email domain
  • to the company's IdP

Deep dive: keys

Deep dive six: keys. Sign tokens with an asymmetric key, so only the auth service can create them while every gateway can verify them. The private key stays in a key management service or hardware module. Each token names its key id, and the public keys are published at a well-known endpoint that verifiers cache. To rotate, publish the new key first, start signing with it a little later, and retire the old one once its last tokens expire. Other secrets belong in a vault, never in git.

Keys and secrets
  • sign tokens with asymmetric
  • keys (ES256): only auth signs,
  • everyone else verifies
  • private key in KMS or HSM
  • token header kid = key id
  • publish public keys (JWKS);
  • verifiers cache for 1 hour
  • rotate every 90 days: publish
  • new key first, sign with it
  • later, retire old after 15 min
  • app secrets in a vault, not git

Deep dive: attacks

Finally, attacks. Credential stuffing replays leaked passwords from other sites at huge volume. Rate limit per IP, per account and per network range, before the expensive hash runs. Add growing delays and a CAPTCHA rather than a hard lockout, which an attacker could use to lock real users out. A login from a new device or country triggers MFA and an email. Return the same error for a wrong email or wrong password, and audit every sensitive event.

Brute force and stuffing
  • limit per IP, per account and
  • per IP range, before hashing
  • growing delays, then CAPTCHA,
  • not a hard lockout (DoS risk)
  • flag new device or country:
  • require MFA, email the user
  • same error for bad email and
  • bad password
  • audit every login, reset and
  • MFA change; alert on spikes

Trade-offs

Review the trade-offs. Short signed access tokens make every check free, but a revoked user keeps access for up to fifteen minutes; for urgent cases, push a small denylist of token ids to the gateways. A slow hash protects users after a breach but costs real CPU. If the auth service goes down, existing tokens keep working, which buys time. Monitor login success rate, four oh one spikes, refresh token reuse and reset volume.

Trade-offs and failures
  • 15 min access token: fast, but
  • revocation waits up to 15 min
  • (urgent: short denylist)
  • slow hash: safer DB, more CPU
  • auth service down: existing
  • tokens keep working 15 min
  • session store down: no refresh,
  • replicate it across zones
  • monitor: login success rate,
  • 401s, refresh reuse, resets

Interview summary

In the interview: hash passwords with argon2id and a salt, and do the CPU math. Issue a short signed access token, verified at the gateway with no lookup, and an opaque, hashed, rotating refresh token whose reuse revokes the family. Resets use random, hashed, single-use links with the same reply every time. Prefer passkeys and authenticator codes over SMS, use OIDC with PKCE for social login, and keep signing keys in a key manager.

Say it like this
  • argon2id + salt, ~100 ms
  • → 60 cores at 600 logins/s
  • access token 15 min, signed,
  • verified at the gateway
  • refresh token opaque, hashed,
  • rotated, reuse kills family
  • reset: random, hashed, 30 min,
  • one use, same reply always
  • MFA: passkeys > TOTP > SMS
  • OIDC code + PKCE, KMS keys

Interview questions

How should passwords be stored?

Hash them with a slow, memory-hard function such as argon2id or bcrypt, with a unique random salt per user and the parameters stored alongside, so a leaked database cannot be cracked quickly. Never encrypt them or use a fast general-purpose hash.

Sessions or JWTs?

Opaque sessions are easy to revoke but need a lookup per request; signed tokens verify locally but stay valid until expiry. A common design combines a short-lived signed access token with an opaque, revocable refresh token stored server side.

How do you revoke a JWT before it expires?

Keep access tokens short, such as 15 minutes, and revoke the refresh token so no new ones are issued. For urgent cases, push a small denylist of token ids or a per-user minimum issued-at time to the verifiers.

What is refresh token rotation?

Each refresh returns a new refresh token and invalidates the old one within the same family. If an old token is presented again, it was copied, so the whole family is revoked and the user must log in again.

How do you design a safe forgot-password flow?

Return the same response whether the email exists, send a link with a long random token stored only as a hash, make it expire within about 30 minutes and work once, and revoke all sessions after the reset.

Which second factor is best?

Passkeys based on WebAuthn are strongest because they are bound to the site and resist phishing; authenticator app codes are good; SMS is weakest due to SIM swapping and should be a fallback with recovery codes as backup.

How does login with Google work securely?

Use the OpenID Connect authorization code flow with PKCE, check the state and nonce, exchange the code server side, and verify the ID token signature, issuer, audience and expiry before mapping the provider and subject to a user.

How do you defend against credential stuffing?

Rate limit by IP, account and network before hashing, add increasing delays and CAPTCHAs instead of hard lockouts, check passwords against breach lists, require MFA on new devices, and alert on unusual login failure spikes.

How do you rotate token signing keys without logging everyone out?

Give each key an id carried in the token header, publish the new public key before signing with it, keep the old public key published until all tokens it signed have expired, and keep private keys in a key management service.