Rate limiting and HTTP caching
Token bucket and sliding window, 429 with Retry-After, ETags and conditional requests, and what a CDN can cache.
Two mechanisms with the same goal: do less work per request, and less work overall. Caching stops you computing an answer somebody already has. Rate limiting stops one client consuming the capacity of all the others. Both are HTTP features, so a client that speaks HTTP gets them without learning anything about your service.
Conditional requests: the 304 that sends no body
An ETag is an opaque identifier for a version of a response. Send one, and a client can ask whether it is still current.
$ curl -i /catalog
HTTP/1.1 200
ETag: "05e99d767478ea6048d9ae8d185ce5188"
Content-Length: 46The client keeps that tag. Next time, it asks:
$ curl -i /catalog -H 'If-None-Match: "05e99d767478ea6048d9ae8d185ce5188"'
HTTP/1.1 304
ETag: "05e99d767478ea6048d9ae8d185ce5188"304 Not Modified, and no body at all. The client uses what it already has.
Be clear about what this does and does not save. Spring's ShallowEtagHeaderFilter, which produced the tags above, hashes the rendered response — so your controller still ran, the database was still queried, and only the bytes on the wire were saved. That is worth having on a slow connection and worth nothing on your server's load.
To save the work as well, the ETag has to be something you can compute cheaply, before doing the expensive part — a version column, a last_modified timestamp, a hash stored beside the row. Then:
String etag = "\"" + catalog.getVersion() + "\"";
if (etag.equals(request.getHeader("If-None-Match")))
return ResponseEntity.status(304).eTag(etag).build();Now the 304 costs one cheap read instead of a full render. Last-Modified with If-Modified-Since is the same idea at second granularity; ETags are more precise and are the better default.
Cache-Control says who may keep it, and for how long
ETag answers "has it changed?". Cache-Control answers "may you avoid asking at all?".
| Directive | Meaning |
|---|---|
no-store | never write this down, anywhere |
no-cache | you may keep it, but revalidate before using it |
private | only the end client, never a shared proxy or CDN |
public | shared caches may keep it |
max-age=60 | fresh for sixty seconds; use it without asking |
must-revalidate | once stale, do not serve it without checking |
The one that matters most in a backend is private. A per-user response cached by a shared proxy is one user seeing another's data, and the default in some setups is more permissive than you expect. Anything personalised is private, and anything with a token in it is no-store.
Rate limiting, and the headers that make it usable
A limiter's job is to protect capacity, and the difference between a good one and a bad one is almost entirely in what it tells the client.
HTTP/1.1 200 RateLimit-Remaining: 2
HTTP/1.1 200 RateLimit-Remaining: 1
HTTP/1.1 200 RateLimit-Remaining: 0
HTTP/1.1 429 Retry-After: 37 RateLimit-Remaining: 0A client can see its budget shrinking and slow down before being refused. When it is refused, Retry-After: 37 tells it exactly how long to wait — without that header a client guesses, and the guess is usually "immediately", which is how a rate limit turns into a retry storm.
Return a Problem Details body with it, as the error lesson established, so the refusal has a type the client can branch on rather than a bare status.
Four algorithms, and the one to reach for
| Algorithm | How it behaves |
|---|---|
| Fixed window | count per clock minute. Simple; allows a double burst across the boundary — 3 at 11:59:59 and 3 at 12:00:00 |
| Sliding window | count over the last 60 seconds, whenever "now" is. Smooth, more state |
| Token bucket | tokens refill at a steady rate; a request spends one. Allows a burst up to the bucket size, then enforces the average |
| Leaky bucket | requests drain at a fixed rate; no bursts at all |
Token bucket is usually right for an API, because real clients are bursty in a legitimate way — a page loads and fires six requests — and what you actually want to prevent is a sustained rate, not a brief clump.
The decision that matters more than the algorithm is what you key on. Per API key or user id is the useful unit. Per IP address punishes an office behind one NAT and does nothing to an attacker with a thousand addresses. Most production limiters key on the authenticated principal and fall back to IP only for unauthenticated endpoints.
Where they meet
The two mechanisms compose, and the combination is what makes a public API survivable:
- A cacheable,
public,max-age'd response never reaches your service at all, so it consumes no rate-limit budget and no capacity. - A conditional request that returns 304 is cheap for you and should usually cost the client less budget than a full response — some APIs do not count them at all.
- A rate-limited client that respects
Retry-Afterrecovers; one that does not becomes the load you were protecting against.
And the ordering matters in the filter chain: rate limiting goes before authentication is expensive but after you can identify the caller — which in practice means after a cheap API-key lookup and before anything that touches a database.
Under the hood: the token-bucket arithmetic, and what a strong ETag comparison actually requires
Token bucket is genuinely an algorithm, not just a metaphor, and the arithmetic is small enough to hold in your head. State is two numbers: tokens (how much budget is available right now) and lastRefill (when it was last topped up). A request does three things: compute elapsed time since lastRefill; refill by elapsed * refillRate, capped at the bucket's capacity (tokens = min(capacity, tokens + elapsed * rate)); then, if tokens >= 1, subtract one and allow, otherwise reject. The cap at capacity is what bounds the burst — a client that has been idle can spend up to a full bucket instantly, but never more, and the sustained rate it can keep spending at is exactly refillRate, no faster, no matter how it paces its requests. That is the precise mechanism behind "allows a burst, then enforces the average" from the table above.
A sliding window counter (the cheap approximation most production limiters actually run, rather than a true sliding log that stores every request's timestamp) keeps two counters — the current fixed window's count and the previous window's count — and estimates the "true" sliding count as currentCount + previousCount * overlapFraction, where overlapFraction is how much of the previous window still falls inside the trailing 60-second span. It is O(1) memory per key, unlike a sliding log's O(requests), at the cost of being an estimate rather than exact — the estimate assumes requests are evenly spread within each window, which is close enough for rate limiting and never exact for it.
A strong ETag comparison, per RFC 7232 §2.3, requires byte-for-byte identity of the representation — not "semantically the same", literally the same bytes. A weak validator (prefixed W/"...") relaxes that to "semantically equivalent, might differ in bytes I don't consider significant", and is what most frameworks generate by default (Spring's ShallowEtagHeaderFilter produces a strong tag from a hash of the actual bytes, so two responses that differ only in whitespace get different tags — worth knowing if you're wondering why a supposedly-unchanged response still misses the cache). If-None-Match on a GET is explicitly allowed to use the weaker comparison; a conditional PUT (checking a resource hasn't moved since you read it) must use the strong one, because there the byte-for-byte guarantee is the entire point.
Try it yourself
Trace a token bucket through a real burst
double tokens = 3; // bucket capacity
double capacity = 3;
double refillPerTick = 1;
int[] requestTicks = {0, 0, 0, 0, 1}; // four requests at once, one a tick later
int currentTick = 0;
for (int t : requestTicks) {
if (t != currentTick) {
int elapsed = t - currentTick;
tokens = Math.min(capacity, tokens + elapsed * refillPerTick);
currentTick = t;
}
boolean allowed = tokens >= 1;
if (allowed) tokens -= 1;
System.out.println("tick=" + t + " allowed=" + allowed + " remaining=" + (int) tokens);
}Before running it: four requests arrive in the same instant against a bucket of capacity 3. Work out by hand how many are allowed, and what changes one tick later.
Misconceptions
- "Token bucket and leaky bucket are two names for the same idea." They allow opposite things. Token bucket lets a burst through immediately, up to the bucket size, and only enforces the average rate over time. Leaky bucket smooths every request to a fixed output rate regardless of how they arrived — no burst is ever allowed, which is why it suits shaping traffic downstream (protecting a fixed-capacity resource) more than it suits being fair to bursty legitimate clients.
- "A sliding window counter is an exact sliding window." It is a weighted estimate of one, trading exactness for O(1) memory — it assumes even distribution of requests inside each window, which is usually close enough to be useful and is never literally the same computation as a true sliding log that timestamps every request individually.
Sources
- RFC 9111, HTTP Caching —
Cache-Control,Vary, and freshness/validation semantics (obsoletes RFC 7234). - RFC 7232, HTTP/1.1: Conditional Requests —
ETag,If-None-Match, and strong versus weak validators. - RFC 6585, Additional HTTP Status Codes — defines
429 Too Many RequestsandRetry-After's role with it.