stamina: Production-grade Retries Made Easy#

Release 24.2.0 (What’s new?)

Transient failures are common in distributed systems. To make your systems resilient, you need to retry failed operations. But bad retries can make things much worse.

stamina is an opinionated wrapper around the great-but-unopinionated Tenacity package. Its goal is to be as ergonomic as possible while doing the right thing by default and minimizing the potential for misuse. It is the result of years of copy-pasting the same configuration over and over again:

  • Retry only on certain exceptions.

  • Exponential backoff with jitter between retries.

  • Limit the number of retries and total time.

  • Automatic async support – including Trio.

  • Preserve type hints of the decorated callable.

  • Flexible instrumentation with Prometheus, structlog, and standard library’s logging support out-of-the-box.

  • Easy global deactivation for testing.

For example:

import httpx

import stamina

@stamina.retry(on=httpx.HTTPError, attempts=3)
def do_it(code: int) -> httpx.Response:
    resp = httpx.get(f"{code}")

    return resp

Check out Motivation if you need more convincing, or jump into our Tutorial to get started right away!