Overview
The API Gateway implements rate limiting using ASP.NET Core’s built-in rate limiting middleware to protect backend services from excessive requests and potential abuse.Current Configuration
Rate limiting is currently applied only to the Ordering Service route.Rate Limiter Implementation
Configured inProgram.cs:
Rate Limiter Policy Details
Policy Name:fixed
Algorithm: Fixed Window
Configuration:
- Window Duration: 10 seconds
- Permit Limit: 5 requests per window
- Requests Allowed: 5 requests every 10 seconds
- Rate: 0.5 requests/second average
How Fixed Window Works
- Requests are counted in fixed 10-second windows
- Counter resets at the start of each window
- First 5 requests succeed, subsequent requests are rejected until next window
- Simple and predictable behavior
Applying Rate Limiting to Routes
Rate limiting is enabled per route using theRateLimiterPolicy property:
Protected Routes
| Route | Rate Limited | Policy | Reason |
|---|---|---|---|
/catalog-service/* | No | - | Read-heavy operations, high throughput needed |
/basket-service/* | No | - | Frequent user interactions expected |
/ordering-service/* | Yes | fixed | Write operations, critical business logic |
Why Only Ordering Service?
The Ordering Service has rate limiting because:- Write Operations: Creates orders, processes payments, updates inventory
- Business Critical: Order creation affects multiple downstream systems
- Resource Intensive: Database writes, external service calls, event publishing
- Fraud Prevention: Prevents rapid-fire order submission attacks
- Resource Protection: Limits load on ordering database and infrastructure
Rate Limit Responses
Success Response
When within rate limit:Rate Limited Response
When limit exceeded:429 Too Many Requests
Retry-After Header: Indicates seconds until the next window (remaining time in current window)
Testing Rate Limiting
Using curl
- Requests 1-5:
200 OK - Request 6:
429 Too Many Requests
Using PowerShell
Load Testing
Rate Limiting Algorithms
ASP.NET Core supports multiple rate limiting algorithms:1. Fixed Window (Current)
2. Sliding Window
3. Token Bucket
4. Concurrency
Advanced Configuration
Per-User Rate Limiting
Per-IP Rate Limiting
Custom Rejection Response
Monitoring and Metrics
Key Metrics to Track
- Total Requests: Number of requests per route
- Rate Limited Requests: Count of 429 responses
- Rate Limit Percentage:
(429s / Total Requests) * 100 - Window Utilization: Average permits used per window
Logging Rate Limit Events
Recommendations
Current Setup
The current configuration (5 requests per 10 seconds) is very restrictive and likely intended for development/testing.Production Recommendations
Ordering Service
Catalog Service (if needed)
Basket Service (if needed)
Best Practices
- Monitor First: Track request patterns before applying limits
- Start Permissive: Begin with higher limits and tighten as needed
- User-Specific: Implement per-user limits for better fairness
- Gradual Degradation: Use queuing instead of immediate rejection
- Clear Communication: Provide meaningful error messages
- Document Limits: Communicate rate limits to API consumers
Adding Rate Limiting to Other Routes
To add rate limiting to catalog or basket services:- Define a new policy in
Program.cs:
- Apply to route in
appsettings.json:
Related Documentation
- YARP Configuration - Complete proxy configuration
- Routing - Route matching and transformations
- Overview - API Gateway architecture
