Sub topic 15
When the limiter's store is down: fail open or fail closed
This chapter assumes you know the token bucket and fixed window algorithms, and that you understand a shared Redis store holds limiter state for many application servers. If you need a refresher on that setup, see the earlier chapters on distributed state and atomic updates.
The problem
In a distributed limiter, every request makes a call to the state store. That call can fail. Redis may restart, the network may drop packets, or a failover may leave the store unreachable for a few seconds. The limiter must still return a decision for each request, so you must choose a default for when it cannot decide.
- Fail open means the limiter allows the request when the store is unavailable.
- Fail closed means the limiter rejects the request when the store is unavailable.
Choosing a policy
The right default depends on what the limit protects.
| Purpose of the limit | Usual choice | Reason |
|---|---|---|
| General API fairness | Fail open | A limiter outage should not take down a healthy service. |
| Login or password attempts | Fail closed | Without limits, attackers can guess passwords freely. |
| Expensive or paid downstream calls | Fail closed | An unlimited burst could cost money or overload a fragile dependency. |
| Read-heavy public endpoints | Fail open | Availability matters more than strict fairness. |
Most services fail open for general traffic. The limiter is a protective layer, and a protective layer that causes outages is worse than one that briefly protects less. Fail closed only where the cost of unlimited traffic is higher than the cost of rejecting good users.
Return a different status for fail closed
When you fail closed, do not send HTTP 429. That status tells the client it sent too many requests, which is false. Use 503 Service Unavailable, ideally with a short Retry-After, so clients and dashboards can tell a limiter outage from real over-limit traffic.
Implementation
Always set a short timeout on the store call. A slow store is often worse than a dead one, because requests pile up waiting. The sketch below uses the redis Python package and the Lua script from the previous chapter.
import redis
client = redis.Redis(host="localhost", port=6379, socket_timeout=0.05)
def check_limit(script, key, capacity, rate, fail_open=True):
try:
allowed = script(keys=[key], args=[capacity, rate, 1])
return bool(allowed), "ok"
except (redis.ConnectionError, redis.TimeoutError):
# Store unreachable: apply the configured failure policy.
return fail_open, "store_error"The second return value lets the caller log and count each store error. It also lets the caller pick 503 instead of 429 when the limiter fails closed.
Reducing the risk
- Local fallback. When the store fails, switch to a per-process in-memory limiter such as the
TokenBucketclass. Each server enforces its own share, so the global limit is only approximate, but abuse stays bounded. - Circuit breaker. After several consecutive failures, skip the store for a short period instead of waiting on a timeout for every request. Probe the store again after that period.
- Alerts. Alert on the rate of store errors. A limiter that fails open silently can go unnoticed for days.
Exercise
Your API has a public product-search endpoint and a password-reset endpoint. Redis, which holds the limiter state, goes down. Which failure policy would you pick for each endpoint, and what status code would you return when rejecting requests?
Show answer
Fail open for product search, because availability matters more than strict fairness there. Fail closed for password reset, because unlimited attempts invite abuse. Rejections caused by the outage should use 503 with a short Retry-After, not 429, since the client did not exceed a limit.