| Users | Traffic Characteristics | API Server Load | Database Load | Network | Other Changes |
|---|---|---|---|---|---|
| 100 users | Low requests/sec (~10-50) | Single server handles easily | Single DB instance, low QPS | Minimal bandwidth | Basic logging, no caching needed |
| 10,000 users | Moderate requests/sec (~1,000-5,000) | Multiple API servers behind load balancer | DB handles ~1,000 QPS, may need read replicas | Moderate bandwidth, consider CDN for static | Introduce caching (Redis), rate limiting |
| 1,000,000 users | High requests/sec (~100,000+) | Horizontal scaling of API servers, autoscaling | DB bottleneck likely, sharding or partitioning needed | High bandwidth, CDN critical for static and some API responses | Advanced caching, API gateway, throttling, monitoring |
| 100,000,000 users | Very high requests/sec (millions) | Global distributed API servers, multi-region load balancing | Multiple DB clusters, geo-distributed, complex sharding | Very high bandwidth, multi-CDN strategy | Microservices, event-driven, circuit breakers, extensive monitoring |
REST API best practices in HLD - Scalability & System Analysis
At small to medium scale, the database is the first bottleneck. It struggles to handle increasing query volume and complex joins. API servers and network usually handle load better initially. Without caching, DB load grows linearly with users.
- Horizontal scaling: Add more API servers behind load balancers to handle more concurrent requests.
- Caching: Use Redis or Memcached to cache frequent API responses and reduce DB load.
- Database read replicas: Offload read queries to replicas to reduce primary DB load.
- Sharding/Partitioning: Split database by user ID or region to distribute load.
- CDN: Cache static assets and some API responses close to users to reduce latency and bandwidth.
- API Gateway: Manage rate limiting, authentication, and routing efficiently.
- Monitoring and throttling: Detect and control traffic spikes to protect backend.
- At 10,000 users, expect ~1,000 QPS. One DB instance can handle ~5,000 QPS, so still okay.
- At 1M users, ~100,000 QPS likely. Need ~20 DB replicas or sharded clusters.
- Bandwidth: 1 Gbps = 125 MB/s. For 100,000 QPS with 1 KB response, need ~100 MB/s bandwidth.
- API servers: Each handles ~5,000 concurrent connections. For 100,000 QPS, need ~20 servers.
- Caching reduces DB QPS by 50-90%, lowering infrastructure cost.
Structure your scalability discussion by first identifying the main components: API servers, database, network. Then estimate load at different user scales. Identify the first bottleneck (usually DB). Propose targeted solutions like caching, read replicas, and sharding. Discuss trade-offs and monitoring. Keep explanations simple and focused.
Question: Your database handles 1000 QPS. Traffic grows 10x. What do you do first?
Answer: Introduce caching to reduce DB queries and add read replicas to distribute read load. This relieves DB pressure before considering sharding or more complex solutions.
Practice
Solution
Step 1: Understand HTTP methods for REST
GET retrieves data, POST creates new resources, PUT updates existing resources, DELETE removes resources.Step 2: Identify method for updating
PUT is the standard method to update or replace an existing resource in REST APIs.Final Answer:
PUT -> Option AQuick Check:
Update = PUT [OK]
- Confusing POST with update instead of create
- Using GET for updates
- Using DELETE to update
Solution
Step 1: Understand common versioning patterns
Versioning is often done in the URL path to clearly separate API versions.Step 2: Identify correct version placement
Placing version immediately after the base API path, like /api/v1/resource, is standard practice.Final Answer:
/api/v1/resource -> Option CQuick Check:
Version in path = /api/v1/ [OK]
- Putting version after resource name
- Using query parameters for versioning
- Placing version before /api/
{
"status": 201,
"message": "Created"
}Solution
Step 1: Review HTTP status codes for success
200 means success for general requests, 201 means resource created successfully.Step 2: Match status code to creation
201 Created is the correct code to indicate a new resource was successfully created.Final Answer:
201 Created -> Option BQuick Check:
Creation success = 201 [OK]
- Confusing 200 with creation success
- Using 400 or 404 for success
- Ignoring status code meaning
POST /users/delete to delete a user. What is the best fix to follow REST API best practices?Solution
Step 1: Identify correct HTTP method for deletion
DELETE method is the standard for removing resources in REST APIs.Step 2: Correct endpoint structure
Use resource identifier in URL path like /users/{id} with DELETE method for clarity and REST compliance.Final Answer:
Change to DELETE /users/{id} -> Option DQuick Check:
Delete resource = DELETE /resource/{id} [OK]
- Using POST for delete actions
- Using GET to delete resources
- Adding delete action in URL path
Solution
Step 1: Understand filtering in REST APIs
Filtering is best done using query parameters in GET requests to keep APIs simple and cacheable.Step 2: Evaluate options for filtering
GET /products?category=electronics&min_price=100&max_price=500 uses GET with query parameters, which is standard and clear. Other options misuse POST or PUT or complex URL paths.Final Answer:
GET /products?category=electronics&min_price=100&max_price=500 -> Option AQuick Check:
Filter with GET query params [OK]
- Using POST or PUT for filtering
- Embedding filters in URL path segments
- Sending filters in request body for GET
Ready to prove it?
Pass the test and this pattern moves unknown
