Microservices Rate Limiting
Every service has a limit on how many requests it can handle each second. A sudden flood from one client, a buggy script, or a malicious attack can use all that capacity and starve everyone else. Rate limiting caps how often a caller may knock on the door and keeps the service healthy for all.
Layman Explanation
A popular bakery sells fresh bread. If one customer ordered all the loaves, other customers would leave empty-handed. A sign reading "five loaves per person" keeps the shelves fair. Rate limiting is that sign for APIs.
Why Rate Limiting Matters
- Protects services from overload and crashes.
- Stops abusive or runaway clients.
- Shares capacity fairly among customers.
- Controls cost for services that pay per call to other providers.
- Supports pricing plans such as free and premium tiers.
Common Algorithms
Fixed Window
The system counts requests inside a calendar window, such as one minute. A client allowed 100 requests per minute gets blocked after the hundredth until the next minute starts. Burst traffic at a window edge can double the effective rate.
Sliding Window
The window slides with time, so the count always covers the last 60 seconds. Edge bursts disappear and fairness improves.
Token Bucket
A bucket holds tokens. Each request removes one token. Tokens refill at a steady speed. A full bucket allows a short burst, and an empty bucket forces the client to wait.
Refill: +2 tokens per second
|
v
+-------------+
| o o o o o | capacity: 5 tokens
+------+------+
|
each request takes 1 token
|
v
Request allowed (bucket empty = request rejected)
Leaky Bucket
Requests enter a queue and leave at a fixed speed, like water dripping from a small hole. The output stays smooth no matter how bursty the input looks.
Where to Enforce Limits
Client --> API Gateway (global limit per client key)
|
+--> Orders service (limit per user)
+--> Search service (limit per IP)
The gateway blocks obvious abuse before it reaches any service. Individual services add finer limits for their own expensive operations.
Choosing What to Count
| Key | Best For |
|---|---|
| API key | Partner and plan-based limits |
| User id | Fair use among logged-in users |
| IP address | Public endpoints without login |
| Endpoint | Protecting costly operations |
Telling Clients What Happened
A rejected request returns status code 429, which means Too Many Requests. Helpful headers explain the rules.
HTTP/1.1 429 Too Many Requests Retry-After: 30 X-RateLimit-Limit: 100 X-RateLimit-Remaining: 0
Well-behaved clients read Retry-After, pause, and try again with growing delays.
Distributed Counting
Many gateway instances share one limit, so they need a shared counter. A fast in-memory store such as Redis keeps the counts. Atomic increment commands prevent two instances from miscounting at the same moment.
Good Practices
- Publish limits in the API documentation.
- Allow small bursts for natural usage spikes.
- Monitor rejected requests to tune limits.
- Whitelist trusted internal services when needed.
Rate Limiting vs Throttling
Rate limiting rejects requests above the allowed number. Throttling slows requests down and processes them later. Some systems combine both: small overages wait in a queue, and large floods receive status code 429 at once. Pick the behavior that suits the user experience you want.
Key Takeaways
Rate limiting guards capacity and fairness. Pick an algorithm that matches your traffic, enforce limits at the gateway, and communicate clearly through status codes and headers.
