Skip to content

Failure modes

A rate limiter stands in front of every request. What it does when its store is slow or down matters more than it does for most code, because the store's trouble can become your whole API's trouble. This page is about what paceapi does then, and the four settings that change it.

The in-memory store has nothing to wait for and is never timed. This page is about a store on the network.

What happens with no settings at all

  • A slow store is given up on. A call to the store gets one second, including the time to connect. redis-py on its own would wait for as long as the server took.
  • A request the store could not count is let through. This is called failing open. It is the default because a rate limiter that takes your API down with it is worse than one that is briefly absent.
  • An error or two cost only themselves. The request that failed is let through, and the next one goes to the store as usual.
  • Three failures in a row put the store in doubt. From then on, one request at a time finds out whether the store works. The others go past it without waiting.
  • A store that is still failing is left alone, for longer each time. The first rest is a thirty-second of a second. Each further attempt that fails doubles it, up to one second. The first call that succeeds ends all of this, and every request goes to the store again.

So when Redis stops answering, the requests already waiting on it wait out the timeout. A few that were in line behind them wait out one more, or two or three more if the connection pool has room for only a call or two at a time. After that, one request at a time tries Redis, with a rest of up to a second in between, and the rest are not held up at all.

A call that is given up on may still have reached Redis and been counted there.

What is not held against the store

A timeout is there to catch a store that is slow. Two other things look the same from inside a request, and neither is the store's doing. If they counted as failures, a flood of requests could make the limiter give up on a healthy store and let the flood through.

  • Waiting in line. The Redis store has only so many calls in flight at once, and the rest of a burst waits in line. That wait is not timed. The timeout starts when a call's turn comes, so a burst is counted a little late, and never given up on.
  • A busy event loop. When something holds the event loop up, an answer that came in time sits unread. So the timeout is counted in thirty-two short ticks, and a tick counts once however late the loop gets round to it. A loop that stood still for a second costs a call one tick, not all of its time.

The other side of this: on an event loop that is busy all the time, giving up on a store that really has stopped answering takes longer than store_timeout says.

What this does not catch

  • A store that is slow, but answers in time. Nothing fails, so nothing is given up on, and the fallback is not used. Nothing is turned away for having waited in line either. If more requests keep arriving than the store can count, they wait longer and longer.
  • Only some calls failing. It takes three failures in a row to put a store in doubt. If one node of a cluster hangs while the others answer, the failures never come three in a row, and each request for that node waits out the timeout.
  • A request with a shorter deadline of its own. A request that is cancelled while it waits for the store says nothing about the store. If your requests run under a deadline shorter than store_timeout, a store that hangs is never given up on, because every request gives up first. Keep store_timeout the shorter of the two.

Keep limiting while the store is down

Failing open means no limits for as long as the outage lasts. Give the limiter a fallback and it counts there in the meantime:

def limiter_for(url: str) -> Limiter:
    return Limiter(
        RedisStore(from_url(url)),
        # While Redis is failing, count here. Each process then keeps its own
        # count, which is looser than one shared count and far tighter than none.
        fallback=MemoryStore(),
        # Wait a quarter of a second for Redis, not the default second.
        store_timeout=0.25,
        # While Redis stays down, try it again less and less often, down to
        # once every five seconds.
        store_cooldown=5,
    )

A memory fallback counts in each process separately. With four workers a caller can get up to four times the limit during an outage, which is looser than normal and far tighter than nothing.

When the first store is back, the next request to find out notices, and the limiter returns to it. What the fallback counted is left to expire. If the fallback fails as well, on_store_error decides.

The whole app is in examples/redis_fallback.py.

Turn requests away instead

For an endpoint where an uncounted request is worse than a refused one, set on_store_error="deny". Requests that cannot be counted get a 429 that asks the caller to try again in a second.

on_store_error="raise" raises an error, for code that wants to decide for itself. In a route that is a 500 unless you handle it. A request that tried the store gets the store's own error, which for a call that took too long is a TimeoutError. A request that went past a store in doubt gets a StoreUnavailableError, whose cause is what the store last failed with.

The settings

Setting Default What it does
store_timeout 1.0 Seconds one call to the store may take, once its turn has come. None waits.
store_cooldown 1.0 The longest a store that keeps failing is left alone between two attempts, in seconds. None tries it on every request, so each one waits out the timeout while the store hangs.
fallback none A second store to count in while the first is failing.
on_store_error "allow" What to do when nothing could count: "allow", "deny" or "raise".

Set store_timeout a little above what your store normally takes at its slowest. A quarter of a second is generous for a Redis in the same network. The slowest call is the one that has to open a connection first, which takes several round trips, and more with TLS. A call that is given up on while it connects leaves no connection behind, so a timeout shorter than that never gets one open.

With store_timeout=None a call can wait forever, and so can the request that is finding out about a store in doubt. When nothing has been heard from it for one store_cooldown, another request may try.

Knowing that it happened

With "allow" and "deny", a failure is logged as an error on the paceapi logger, with the store's exception, at most once a minute for as long as the trouble lasts. The line says what is happening to requests meanwhile, and a fallback that fails as well gets a line of its own. A recovery is logged once the first store has stayed up for a minute. With "raise", a failure that leaves a request uncounted is not logged, because the error is yours to handle. One that the fallback made up for is logged like any other.

Observers are told about every failure as it happens, through on_store_error. The event carries the error as the store raised it and what happened to the request: "allow", "deny", "raise", or "fallback" when it was counted in the fallback store. A request that goes past a store in doubt is not a new failure, and raises no event.