Every check your app runs against external infrastructure (a rate limiter’s Redis backend, an auth token lookup, a cache, a fraud scoring service) can fail because the infrastructure is unreachable, as opposed to returning a “no”. When that happens, the code behind the check can let the request through or reject it. There’s no option where the check works anyway, so the decision should be made explicitly, in code, before it happens in production at 2am.
Fail open means the request proceeds when the check can’t run. Fail closed means it’s rejected. People sometimes call one of these the safe default, but which is safer depends on what the check protects. The question is which is worse: letting through a request the check would have blocked, or blocking one it would have allowed.
Fail open: a rate limiter
flash-sale-system is a ticket-reservation service built to survive a stampede of concurrent buyers without overselling inventory. Every reservation request passes through a rate limiter backed by Redis:
async def check_rate_limit(request: Request):
client_ip = request.client.host if request.client else "unknown"
key = f"rate_limit:{client_ip}"
try:
current = await r_async.incr(key)
if current == 1:
await r_async.expire(key, RATE_LIMIT_DURATION)
if current > RATE_LIMIT_MAX:
raise HTTPException(status_code=429, detail="Too many requests. Slow down!")
except redis.ConnectionError:
# Fail open if Redis is down? Or Fail closed?
# For a demo, let's fail open but log it.
print("Rate limit check skipped (Redis down)")
If Redis is unreachable, the except block logs a line and lets the request continue to the reservation logic. That fail-open choice is right for this check because the rate limiter isn’t what keeps the system correct. The no-overselling guarantee comes from a separate Lua script that runs atomically inside Redis and decrements the ticket counter in one operation. The rate limiter only throttles abuse. If Redis goes down entirely, the reservation endpoint has bigger problems anyway, since the same instance holds the inventory counter. If only the rate limiter’s calls are flaking (timeouts, a network blip), failing closed would turn away real buyers during a flash sale because a secondary defense hiccuped. Blocking legitimate purchases is worse than briefly losing the abuse throttle.
So fail open makes sense when the check is a secondary defense and denying the request costs more than letting it through.
Fail closed: an auth check that has nothing to fall back to
The same codebase has a check that does the opposite, with no comment debating it. get_current_user decodes the JWT on every authenticated request:
async def get_current_user(token: str = Depends(oauth2_scheme)) -> str:
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username = payload.get("sub")
if username is None or not isinstance(username, str):
raise HTTPException(status_code=401, detail="Invalid credentials")
return username
except JWTError as exc:
raise HTTPException(status_code=401, detail="Invalid credentials") from exc
No decode failure falls through to “let them in anyway”. An expired token, a bad signature or a malformed payload all end in a 401. That’s fail closed, and it’s correct because “the auth check is broken” and “the request isn’t authenticated” have to be treated the same. A rate limiter that can’t run has a fallback state: everyone is treated as under the limit. An auth check has none. Its whole job is to tell the real user from everyone else, and skipping that when it can’t be verified grants access to anyone who asks.
Deciding
If this check is unavailable, what does a false negative (letting a bad request through) cost compared with a false positive (blocking a good one)? For a rate limiter guarding a ticket sale, a false negative is cheap: some extra load reaches reservation logic that has its own correctness guarantee. For auth, a false negative defeats the purpose of having auth. Framed that way, the code doesn’t need a comment like “fail open if Redis is down? or fail closed?”, because what the check protects already answers it.