Try
↑ ↓ move↵ openesc close
In this lesson · topic 6 of 7
  1. 1. When a retry makes it worse
  2. 2. Backoff, jitter, budgets and breakers
  3. 3. Break a dependency and compare six retry policies
  4. 4. Jitter spreads retries, only budgets and breakers cap them
  5. 5. Hedging the slow tail
  6. 6. Deadlines down the chain, keys across the retry
  7. 7. Sizing the retry safety net

Topic 6

Deadlines down the chain, keys across the retry

6 min read

Two ideas make retries safe in a real system. One is about time, and the other is about repeats.

Deadlines

A user waits 2 seconds. The request goes through a gateway, then service A, then service B, then a database. If each hop starts its own 2-second timeout, three things go wrong.

The gateway gives up at 2 s and answers the user with an error. A is still waiting, up to 2 s from when it called. B is still waiting for its own 2 s. The database query started at 1.9 s keeps running until 3.9 s. The user left long ago, and the work carries on for up to two seconds at every layer below.

The fix is to send a deadline, an absolute time or the time remaining, and let each hop subtract what it has used. gRPC does this, for example, by sending the remaining time in a request header and applying it to calls made while handling that request.

A worked path, with 2,000 ms at the start:

  • The gateway spends 100 ms and sends 1,900 ms to A.
  • A spends 300 ms and sends 1,600 ms to B.
  • B knows its slowest useful query takes 200 ms. With 1,600 ms left it proceeds.

Now suppose B arrives with 120 ms left. It can fail at once, without touching the database, because even a normal answer would arrive after the caller has gone. That early failure is a form of load shedding, and it costs nearly nothing.

Predict first

A caller has a 2 s deadline. Service A retries a failing call to B three times, each with its own fresh 1 s timeout, and all fail by timeout. When does service A stop trying?

Two rules follow. Never let a retry start with less time left than a normal answer needs. And never extend the deadline from below.

Idempotency keys

A write times out. The client does not know if the server did it. Retrying a charge may bill the card twice. Not retrying may drop a real order. The way out is to make the repeat harmless.

The client creates a unique key for each logical operation, once, for example a random identifier made before the first attempt, and sends it on every attempt. The server does this:

  1. Look up the key. If a stored result exists, return it without running the operation.
  2. If not, run the operation and store the result under the key, in the same transaction as the effect.
  3. Keep the key for a while, long enough to cover every retry the client might make, such as a day.

The same transaction is the important part. If the effect and the stored key can disagree, a crash between them reintroduces the duplicate.

Three edge cases decide whether the design holds.

  • Concurrent duplicates. Two attempts arrive while the first is still running. The second must wait for it or be refused with a conflict, not run again.
  • Key reuse with a different body. Same key, different amount. This is a client bug, and the server should refuse it and not return the first result.
  • Expiry. A key deleted after an hour does nothing for a client that retries after two.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms