⚙️ Fix a Rate Limiter Resetting Its Window Too Early
The built-in System.Threading.RateLimiting middleware’s FixedWindowLimiter tracks its window entirely in the memory of whichever process instance handled the request, keyed per partition. That’s fine for a single instance, but behind a load balancer distributing traffic across several replicas, each replica keeps its own independent window and its own independent count — so a client that gets routed to a different instance on its next request effectively gets a brand new, unused rate limit window there, letting it exceed the limit you configured simply by virtue of which instance happened to answer.
🔎 The Problem
builder.Services.AddRateLimiter(options =>
{
options.AddFixedWindowLimiter("api", opt =>
{
opt.PermitLimit = 100;
opt.Window = TimeSpan.FromMinutes(1);
});
});
// In-memory per-instance state: 3 replicas behind a load balancer each
// enforce their OWN 100-requests-per-minute window independently, so a
// client round-robined across all three can realistically send ~300
// requests/minute total before any single instance would reject one
✅ Fix: A Rate Limiter Resetting Its Window Too Early
- Use a distributed rate limiter backed by a shared store (Redis-backed implementations are the common choice) whenever the app runs as more than one instance, so the window and count are shared rather than per-replica.
- If sticky sessions are already part of your architecture for other reasons, confirm the load balancer is actually routing a given client consistently to the same instance — don’t rely on it as a rate-limiting strategy on its own.
- Add a test that simulates multiple instances hitting the same limiter configuration independently, so the gap between “the limit works in a single-instance dev environment” and production is caught before it ships.
🚨 Why This Is Dangerous
- A rate limit that silently multiplies by your replica count under load is exactly the kind of protection that looks like it’s working in every manual test and then fails precisely when the system is under enough real traffic to need it.
A rate limiter is only as global as the state it reads from — in-memory means local, and local means every replica gets its own private allowance.




