Design an Authentication Service
Understanding the Problem
🔐 What is an authentication service? An authentication service answers one question for other systems: who is this user? It runs the login page, stores or delegates credentials, and hands out tokens that applications trust as proof of identity. Products like Okta, Auth0, Microsoft Entra ID or AWS Cognito do this as a service for many companies at once, so one login works across all of a company's apps (single sign-on) and employees can log in with their corporate identity provider (federation).
You might hear this asked as "design SSO," "design a login platform," or "design Okta." They're the same problem. SSO is a feature of a central authentication service, and federation is that service trusting another one. We'll cover all three.
Functional Requirements
Authentication is easy to confuse with authorization. Draw the line with your interviewer early: we're proving who the user is, not deciding what they can do. That's a separate system.
Core Requirements
- Users should be able to sign up and log in to an application with a username and password.
- Users who are logged in to one application should be able to open another application from the same company without logging in again (single sign-on).
- Applications should be able to verify, on every request, which user is calling, and sessions should be revocable (logout, password change, admin kill).
- Users from an enterprise customer should be able to log in with their company's own identity provider (federation), and consumers with a social login like Google.
Below the line (out of scope)
- Multi-factor authentication. It's a step inside login; see /system-design/otp-mfa. We'll show where it plugs in.
- Authorization (roles, permissions, ACLs).
- User provisioning (SCIM), admin consoles and audit-log search.
Non-Functional Requirements
Ask for scale. We'll design for a multi-tenant service with 10,000 tenants, 500 million end users, and 100 million logins per day.
Core Requirements
- The system should be highly available, 99.99% or better. If login is down, every app that depends on it is down.
- Token verification should add close to zero latency to application requests, at an aggregate of around 1 million requests per second across all customer apps.
- Login should complete in under 500 ms at p99, excluding the user's typing and any MFA.
- The system must store credentials safely and resist credential stuffing and brute-force attacks.
- Revocation should take effect within about a minute.
Below the line (out of scope)
- Compliance programs (SOC 2, FedRAMP) beyond doing the engineering right.
- Fine-grained per-tenant data residency, beyond keeping each tenant in a home region.
Now the math. 100M logins a day is about 1,200 logins per second on average. Logins cluster at the start of the workday and around big consumer events, so plan for 10x peaks, around 12,000 per second. Each password login runs a deliberately slow hash. Argon2id or bcrypt tuned to take on the order of 100 ms of CPU is typical, so 12,000 logins per second needs about 1,200 CPU cores just for hashing, and with Argon2id at 64 MB per hash, about 75 GB of memory in flight across the fleet.
That number is the most useful thing you can say about this problem. Password checks are expensive on purpose, so an attacker sending a million guesses a second can't be allowed to reach the hash. Rate limiting and bot detection belong in front of the expensive step, not after it.
The other number is the million requests per second of token checks. If each one were a call to our service, our service would need to be bigger and more available than all our customers' apps combined. So token checks must not call us at all, which pushes us toward signed tokens.