SD-35 · Back to the write-up
Design a Login and Authentication
Slow hashes on their own servers, opaque sessions in a fast store, one check at the gateway, and limits plus a second factor against stolen passwords.
Follow a request
Log in on a new device
Load a page
A stuffing wave
Reset a password
Log in with a social account
A session shard fails
Users + outside
Edge
Services
Data
Identity providerSocial login
BrowserSession cookie
Email + SMSOutside providers
Edge + WAFRate limits
Risk workersEvents, async
Auth serviceHashes, MFA
Auth gatewayChecks once
Web servicesTrust the gateway
Users DBAccounts, hashes
Session store6 shards
The numbers, at a glance
1 real ≈ 150/s20 bots ≈ 3,000/s
750KEvery service
500KStore limit
150KGateway once
0.1% logins150,000 req/s
A login, in order
Password login on a known device. On a risky login, the service returns mfa_required and the session is created only after the code.
Browser
Edge
Auth gateway
Auth service
Users DB
Session store
1 POST /login
2 under limits
3 verify
4 user by email
5 hash, factors
6 SET sess:{hash}
7 OK
8 200 + sid
9 Set-Cookie
10 logged in
RequestResponseAsync, does not block
How a session is represented
The decision this question is really about.
Cookie: JWT, exp +30 d
Services check signature
Logout → still valid
+ No lookups.− Valid until it expires, even after logout.
Access token, 5 minRefresh token
Lookup on refresh only
Logout → up to 5 min
+ Few lookups.− Revocation waits up to 5 minutes.
Cookie: sid (random)
Gateway: GET sess:{hash}internal token, 60 s
Logout → gone in 1 s
+ Revoke at once; cookie reveals nothing.− One fast lookup per request.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Browser
Web app
Users + sessions DB
Browser
Auth serviceWeb services
Users DBSession store
Browser
Edge + WAF
Auth gateway
Auth serviceWeb services
Users DBSession store
BrowserIdentity providers
Edge + WAF
Auth gateway
Auth service + MFAWeb servicesRisk workers
Users DBSession storeEmail + SMS
Was this walkthrough useful?Your vote tells us what to improve next.