Identity and access patterns for distributed systems. OAuth 2.0 authorization code flow, JWT tokens, session management โ how to handle "who are you?" and "what can you do?" at scale.
Medium Medium-High FrequencyAuthentication (AuthN) verifies identity โ "who are you?" Authorization (AuthZ) checks permissions โ "what can you do?" OAuth 2.0 is a delegation protocol: a user authorizes an app to access their data on a provider, without sharing their password.
authorization_code.client_secret for tokens (server-to-server, never exposed to browser).access_token (1h TTL), refresh_token (long-lived), id_token (JWT with user info).Token contains claims, signed by server. Client sends JWT with each request. No session lookup needed โ verify signature locally. But: can't revoke until expiry. Use short-lived (15min) + refresh tokens.
Server stores session in Redis/memory, sends session ID as cookie. Easily revocable (delete session). But: requires shared state across servers. Better for traditional web apps.
OAuth 2.0 is authorization ("Spotify can read my Google profile") โ it doesn't authenticate the user. OpenID Connect (OIDC) adds authentication on top ("this is really user@gmail.com"). Most candidates confuse them โ knowing the difference shows depth.
JWT expiry length: Short (5-15 min) = more refresh calls but faster revocation on password change. Long (1-24h) = fewer refreshes but compromised tokens stay valid for hours. Best practice: 15-minute JWT + refresh token rotation + blacklist for emergency revocation.
Symmetric vs asymmetric signing: HS256 (symmetric) โ one shared secret, simpler but every service needs the secret. RS256 (asymmetric) โ private key signs, public key verifies. Microservices should use RS256 so services can verify tokens without holding the signing key.
Stateless JWT vs stateful sessions: JWT scales horizontally (no shared state) but can't be revoked mid-flight. Sessions are revocable but need shared storage (Redis). Most production systems use both: JWT for API auth, sessions for web app auth.
PKCE for mobile/SPA: Mobile apps can't store client_secret securely. PKCE (Proof Key for Code Exchange) replaces the secret with a dynamic challenge โ the modern best practice for public clients.
Auth patterns appear in nearly every system design interview. Show you understand the full picture, not just "use JWT."
Interview signal: Walk through the authorization code flow step by step โ draw the three parties (user, app, auth server) and the redirect dance. Mention PKCE for mobile apps unprompted.
id_token (JWT), extracts email/name, finds or creates a Spotify account, then issues its own JWT + refresh token./var/run/secrets. Inter-service auth uses these tokens + RBAC policies.| Metric | Value |
|---|---|
| JWT signature verification | <1 ms (local, no network) |
| Recommended JWT expiry | 5โ15 minutes |
| Refresh token lifetime | 30โ90 days (with rotation) |
| bcrypt password hash time | ~100โ300 ms (tunable) |
| Redis session lookup | ~0.5 ms |
| Token storage per user (Redis) | ~200 bytes |
| 240M social auth users ร 4 sessions/day | ~1B token refreshes/day |
| 240M tokens ร 200 bytes in Redis | ~48 GB (2 Redis nodes) |