Distributed Limiting, HTTP 429 and Load Shedding
Overview
Real systems run many gateway servers, so counting requests in one server's memory is not enough. This lesson covers sharing counters, telling clients they were limited, and how throttling and load shedding differ from rate limiting.
Detailed Notes
What is distributed rate limiting
It means enforcing one limit across many servers. If a client is allowed 100 requests per minute and hits 5 gateway servers, the five must agree on the total.
The problem it solves
With purely local counters, each of 5 servers allows 100, so the client really gets 500. With load balancing, requests spread unpredictably, so local counts are also uneven. We need shared state, or an accepted approximation.
How it works
Where to keep counters
- Local (per server): very fast, but the real limit is multiplied by the number of servers.
- Central store: all servers share counters in Redis, a very fast in-memory data store (the next chapters cover why in-memory is fast). Accurate, but adds a network call and a dependency.
- Local plus periodic sync: count locally, sync totals every second or so. Fast, with a small overshoot allowed.
Redis supports atomic operations (they complete fully, with no other command interleaving). That matters: without it, two servers could both read "1 token left" and both spend it. A simple fixed-window counter adds 1 to a per-key, per-minute number and rejects when it passes the limit.
Decide the failure mode if Redis is unreachable: fail open (allow traffic, favouring availability) or fail closed (block, favouring protection).
Telling clients: HTTP 429. An over-limit client should get 429 Too Many Requests, ideally with a Retry-After header saying how many seconds to wait. Many APIs also send headers showing the limit, remaining requests and reset time (names vary by provider).
Client behaviour: backoff with jitter. If thousands of clients retry after exactly 1 second, they collide again (a retry storm). Clients should use exponential backoff (wait 1s, 2s, 4s, 8s...) plus jitter (random variation) so retries spread out.
Rate limiting vs throttling vs load shedding
| Concept | Targets | What it does |
|---|---|---|
| Rate limiting | An individual client | Rejects requests beyond that client's allowance |
| Throttling | Client or system | Slows requests (delay, queue, lower rate) rather than rejecting |
| Load shedding | The whole system's survival | Drops low-priority requests when overloaded, whoever sent them |
Words are used loosely (AWS calls its rate limiting "throttling"), so focus on behaviour. Rate limiting is about fairness per client; load shedding is about keeping the system alive.
A simple example
A ticket office with several windows (servers) and a limit of 4 tickets per customer. If each window counts separately, someone can buy 4 at every window. A shared ledger fixes it, but every sale needs a trip to the ledger. When a huge crowd arrives, staff stop serving window-shoppers first so buyers get through: that is load shedding.
System design example
Three stateless gateway instances behind a load balancer run an atomic token-bucket check in a shared Redis per API key. Over limit: 429 with Retry-After: 12. If Redis is down, they fail open with a conservative local limit, since blocking all traffic is worse. Separately, when CPU or latency passes a threshold, the service sheds low-priority work (analytics calls, returning 503) to keep checkout working. Clients retry with backoff and jitter.
Summary
- Many servers need shared counters (Redis) or an accepted approximation.
- Atomic updates prevent two servers spending the same token.
- Return 429 with Retry-After; clients should back off with jitter.
- Rate limiting is per-client fairness; load shedding protects the whole system.
Architecture Diagram
Stateless gateways consult shared counters; over-limit clients get 429.
- 1A gateway receives a request and builds the key.
- 2It runs an atomic check-and-update in Redis.
- 3Allowed: it forwards the request.
- 4Over limit: it returns 429 with Retry-After.
Common Interview Questions
Chapter quiz
Rate Limiting and Throttling · 5 questions
1.What is the main goal of rate limiting?
2.Which HTTP status code means 'too many requests'?
3.Which algorithm allows controlled bursts using a refilling bucket of tokens?
4.What is the fixed window boundary problem?
5.Why do clients retry with exponential backoff and jitter?
0 of 5 answered
Finished this lesson?
0 of 91 lessons done · 0% of the course

