Rate Limiting
Rate Limiting Algorithms
| Algorithm | Description |
|---|---|
| Fixed Window | X requests per minute |
| Sliding Window | Smooth rate over time |
| Token Bucket | Allow bursts, refill tokens |
| Leaky Bucket | Process at fixed rate |
Rate Limit Headers
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1609459200
Retry-After: 60
Implementation
@RateLimiter(name = "apiLimiter", fallbackMethod = "rateLimitFallback")
@GetMapping("/api/products")
public ResponseEntity<List<Product>> getProducts() { ... }
public ResponseEntity<?> rateLimitFallback(Exception ex) {
return ResponseEntity.status(429)
.header("Retry-After", "60")
.body("Rate limit exceeded");
}
Rate Limiting Strategies
Algorithms
- Fixed Window: Simple, bursty
- Sliding Window Log: Accurate, memory-heavy
- Sliding Window Counter: Balanced
- Token Bucket: Flexible, allows bursts
Response Headers
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1640995200
Implementation
- Use Redis for distributed limiting
- Implement per-user limits
- Return 429 status code
- Include retry-after header
Key Points
- Understanding Rate Limiting is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Rate Limiting in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Rate Limiting implementation
// Key aspects: validation, error handling, logging, testing
public class RateLimiting {
// Production-ready implementation
}Identify and handle edge cases for Rate Limiting. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Rate Limiting. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. HTTP status for rate limiting?
2. Token bucket allows?
3. What is the primary purpose of Rate Limiting?
4. What is a common mistake when implementing Rate Limiting?
Flashcards
Question
Rate limit status code?
Click to reveal answer
Answer
429 Too Many Requests
Question
Token bucket behavior?
Click to reveal answer
Answer
Allows bursts, tokens refill over time
Question
What is Rate Limiting?
Click to reveal answer
Answer
Rate Limiting is a key concept in backend development.
Question
When to use Rate Limiting?
Click to reveal answer
Answer
Use Rate Limiting when building production systems that require reliability, scalability, and maintainability.
Question
Rate Limiting best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Rate limiting prevents abuse and ensures availability
- 2.Return 429 with Retry-After header
- 3.Choose algorithm based on needs
- 4.Per-user and per-IP limiting
Interview Tips
- •Implement rate limiting
- •Know rate limiting algorithms
Cheat Sheet
Rate Limiting
- Status: 429 Too Many Requests
- Headers: X-RateLimit-Limit, Remaining, Reset
- Algorithms: Fixed Window, Sliding, Token Bucket, Leaky
- Retry-After: seconds until retry