Timeouts and retries without the jargon

Here is our try at one short picture of timeouts and retries which a new colleague could read over a single coffee.

The short version

A timeout is the longest time a client will wait for an answer before it stops waiting; a retry is a second attempt following a timeout, while a retry policy says how often retries are allowed and how long to wait in between.

How it works

Retrying a payment may create a duplicate; an idempotency key allows the server to spot the repeat and return the original result. Deadlines ought to nest: a top-level call has the largest deadline, and each layer beneath it gets a smaller budget, so that work nobody is waiting for any more gets cancelled promptly.

import random
import time

def retry_call(fn, attempts=6, base=0.5, cap=5):
    for i in range(attempts):
        try:
            return fn(timeout=2)
        except TimeoutError:
            if i == attempts - 1:
                raise
            time.sleep(min(cap, base * 2 ** i) * random.uniform(0.5, 1.5))

What we do with it

Retries help against brief blips but multiply load during long outages, so retries need a cap.