| Users | Requests per Second | Token Issuance | Database Load | Network Traffic | Latency |
|---|---|---|---|---|---|
| 100 users | ~10-20 RPS | Low token requests | Single DB instance handles well | Low bandwidth usage | Low latency |
| 10,000 users | ~1,000-2,000 RPS | Moderate token requests, refreshes | DB load increases, possible CPU spikes | Moderate bandwidth usage | Latency may increase slightly |
| 1,000,000 users | ~100,000 RPS | High token issuance and validation | DB bottleneck likely, connection limits hit | High bandwidth, possible network congestion | Latency noticeable without caching |
| 100,000,000 users | ~10,000,000 RPS | Very high token traffic, refresh storms | Single DB impossible, sharding needed | Very high bandwidth, CDN and edge caching needed | Latency critical, must optimize globally |
OAuth 2.0 flow in HLD - Scalability & System Analysis
The first bottleneck in OAuth 2.0 flow scaling is the database that stores tokens, authorization codes, and user sessions. As user count and requests grow, the DB faces high read/write load, connection limits, and latency issues. Token validation and refresh operations increase DB pressure quickly.
- Database Scaling: Use read replicas to offload read queries, connection pooling to manage DB connections, and sharding to distribute data across multiple DB instances.
- Caching: Cache access tokens and user session data in fast in-memory stores like Redis to reduce DB hits.
- Horizontal Scaling: Add more authorization servers behind load balancers to handle increased request volume.
- Token Design: Use JWT tokens to reduce DB lookups by embedding claims in tokens, validated cryptographically.
- CDN and Edge: For public OAuth endpoints, use CDN to reduce latency and bandwidth usage globally.
- Rate Limiting: Implement rate limiting to prevent abuse and reduce load spikes.
- At 1M users with 100K RPS, DB must handle ~100K token validations/refreshes per second.
- Assuming 1 token validation = 1 DB read, 1 refresh = 1 write + 1 read, DB QPS can exceed 200K.
- Single PostgreSQL instance handles ~5K QPS, so need ~40+ replicas or sharding.
- Network bandwidth: 100K RPS * 1KB token size = ~100MB/s (~800Mbps), requires 1Gbps+ network.
- Memory for caching tokens: 1M active tokens * 1KB = ~1GB RAM minimum.
When discussing OAuth 2.0 scalability, start by explaining the flow and components. Identify the database as the first bottleneck due to token storage and validation. Then discuss caching and horizontal scaling of authorization servers. Mention token design improvements like JWT to reduce DB load. Finally, cover network and rate limiting strategies. Structure your answer from small scale to large scale with clear bottlenecks and solutions.
Your database handles 1000 QPS for token validation. Traffic grows 10x to 10,000 QPS. What do you do first and why?
Practice
Solution
Step 1: Understand OAuth 2.0 Authorization Code flow
This flow lets apps get permission to access user data without needing the user's password directly.Step 2: Identify the main goal
The goal is to keep user credentials safe while allowing apps to act on behalf of the user.Final Answer:
To allow apps to access user data securely without sharing passwords -> Option CQuick Check:
OAuth 2.0 Authorization Code flow = Secure app access without passwords [OK]
- Confusing OAuth with password storage
- Thinking OAuth encrypts all user data
- Assuming OAuth replaces authentication methods
Solution
Step 1: Recall the OAuth 2.0 Authorization Code flow steps
User logs in and authorizes the app, which then receives an authorization code.Step 2: Understand token exchange
The app exchanges the authorization code for an access token to access user data.Final Answer:
User logs in -> App gets authorization code -> App exchanges code for access token -> Option DQuick Check:
Authorization Code flow sequence = Login, code, token [OK]
- Thinking app gets token before authorization code
- Confusing password sending with OAuth flow
- Mixing order of login and token receipt
POST /token
Headers: Content-Type: application/x-www-form-urlencoded
Body: grant_type=authorization_code&code=abc123&redirect_uri=https://app.com/callback&client_id=client123&client_secret=secretXYZ
Response:
{
"access_token": "token456",
"token_type": "Bearer",
"expires_in": 3600
}Solution
Step 1: Analyze the token request
The app sends a POST request with grant_type authorization_code and includes the code and client credentials.Step 2: Interpret the response
The response contains an access_token 'token456', token_type 'Bearer', and expires_in 3600 seconds, meaning the token is valid for one hour.Final Answer:
An access token 'token456' valid for 3600 seconds -> Option AQuick Check:
Token response includes access_token and expiry [OK]
- Expecting refresh token instead of access token
- Missing client_secret causes error
- Confusing confirmation message with token response
Solution
Step 1: Identify common OAuth token exchange errors
One frequent error is a mismatch in redirect URI between authorization and token requests.Step 2: Understand impact of redirect URI mismatch
If the redirect URI differs, the authorization server rejects the token request, so no access token is returned.Final Answer:
The redirect URI used in the token request does not match the one registered -> Option BQuick Check:
Redirect URI mismatch blocks token issuance [OK]
- Blaming user password errors for token issues
- Ignoring client_id in authorization request
- Assuming token expires before it is issued
Solution
Step 1: Understand security needs for mobile and web apps
Mobile apps cannot keep client secrets safe, so PKCE (Proof Key for Code Exchange) enhances security for Authorization Code flow.Step 2: Choose best practices for token storage and flow
Web apps can use standard Authorization Code flow with client secret. Implicit flow is deprecated due to security risks. Storing tokens in local storage or URLs exposes them to attacks.Final Answer:
Use Authorization Code flow with PKCE for the mobile app and standard Authorization Code flow for the web app -> Option AQuick Check:
PKCE secures mobile OAuth; standard flow suits web apps [OK]
- Using Implicit flow despite security risks
- Storing tokens insecurely in local storage
- Passing tokens in URL parameters
Ready to prove it?
Pass the test and this pattern moves unknown
