Skip to content

Timeouts and retries

A service that calls another one does not control how long the answer takes. If it sets no limit, a dependency that has stopped answering takes the caller down with it: every call waits, and every waiting call holds a thread, a connection and a user. Little’s Law says how many: the number of calls waiting at once is the rate of calls times the time each one waits. At 30 calls a second and a wait of 3 s, that is 90 calls held at once. With no limit, the wait has no end and neither has the number.

A timeout is that limit: the longest a caller will wait for an answer before it stops waiting and treats the call as failed. In Polly, the .NET resilience library, a timeout strategy cancels the execution if it does not complete in time, and its documentation stresses that this is cooperative: the code being called must respect the cancellation token, or it carries on after the timeout has fired.

How long should it be? Amazon’s article on the subject describes choosing an acceptable rate of false timeouts, 0.1% in its example, and then reading the matching percentile of the downstream service’s latency, p99.9 in that example. A timeout below that percentile cuts off calls that would have worked. A timeout far above it holds resources for nothing. The percentiles lesson shows why a percentile, and not the average, is the number to read.

Mental model. A timeout is the caller’s decision to stop waiting, not a fact about the dependency. It trades a call that might still have succeeded for the resources that waiting would have held.

When the timeout runs out the caller knows one thing: no answer came in time. The request may never have arrived, may have been done with the answer lost, or may still be running. The duplicate requests lesson starts from there, and it is the reason this module keeps returning to the same rule: a call is only safe to repeat if repeating it does no harm.

The lab sends 30 user requests a second to a service that makes one attempt at a dependency for each. The dependency is down from 5 s to 13 s, and the lab opens at 10 s, in the middle of the outage. There are no retries yet.

One attempt for each request, and an outage of the dependency. Every request that arrives while it is down fails. This is the baseline to compare the other scenarios with.

Traffic
30 per second
Arrival

Steady spreads the requests evenly. In bursts, a second of requests arrives all at once, as when many clients act on the same schedule.

The dependency
5 s
8 s

While the dependency is down, every attempt fails.

0%

Share of attempts that fail on their own, even when the dependency is up.

60 per second

Attempts the dependency can serve in any one second. It turns the rest away.

A failure looks like

An error comes back after 0.1 s. With no answer, the caller waits the whole timeout before it can retry.

The callers
Layers that retry
1

Each layer calls the next one and retries that call. The last layer calls the dependency.

0 (attempts: 1)
1 s

How long a caller waits for an answer from the dependency.

Backoff

The wait before a retry. None retries at once, fixed waits 0.5 s every time, and exponential starts at 0.5 s and doubles each time, up to 4 s.

Jitter

Jitter makes each wait random. Full picks anywhere from zero to the delay, equal picks from half the delay to the delay. With no backoff there is no wait to randomise.

Protection

Each layer may retry only a share of the calls it makes, after a reserve of ten retries.

10% of calls

Closed, calls go through. After enough failures in a row it opens and calls fail at once. After a pause it is half-open and lets one probe through.

5 failures in a row
3 s
Timing
Time: 10 s

The lab runs in real time. Step moves on by one tenth of a second, Skip by five seconds.

What is happening

The dependency is down from 5 s to 13 s. Every attempt made now fails.

There are no retries, so one user request is one attempt at the dependency.

In the last second, user requests: 30; attempts at the dependency: 30; served: 0. The dependency can serve 60 a second.

User requests
300150 succeeded, 147 gave up, 3 still trying
Attempts at the dependency
3001 for each user request, the most for one: 1
At most for one request
1attempts at the dependency: 1
Attempts, last second
30served: 0, capacity: 60, busiest tenth of a second: 3
Circuit breaker
Offswitched off
Retries refused
0no retry budget
Attempts at the dependency in each tenth of a second
035Outage010 seconds
  • Attempts (line)
  • Served (filled)
  • Outage (hatched)
Second by second, newest first (the chart in numbers). Now: 10 s.
SecondsNew requestsAttemptsServedBusiest tenth of a secondDependency
9 to 10303003Down
8 to 9303003Down
7 to 8303003Down
6 to 7303003Down
5 to 6303003Down
4 to 53030303Up
3 to 43030303Up
2 to 33030303Up
Attempts at each layer since the start
CallAttempts so farFor each user requestAt most for one request
Layer 1 to the dependency30011

Circuit breaker log

The circuit breaker is switched off.

At 10 s, user requests so far: 300 (150 succeeded, 147 gave up, 3 still trying). Attempts at the dependency: 300, 1 for each request. Circuit breaker: Off.

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • The lab advances in steps of 0.1 s. An attempt that succeeds, or that is answered with an error, takes one step, and a retry is sent on the step after the failure at the earliest, so no backoff means 0.1 s. A seeded random draw decides each failure rate and each jitter, so the same settings give the same run.
  • Each layer calls the next one and retries that call. The layers above wait for the answer of the layer below, and only the attempt at the dependency has a timeout. Real services usually have a timeout at every layer, which leaves work running below after a caller has given up and can make the amplification worse than shown.
  • The dependency fails in three ways: an outage window, a failure rate, and a capacity (it serves a fixed number of attempts in any one second and turns the rest away). A failure is either an error at once or silence until the timeout; real failures are a mix of both. A turned-away attempt costs the dependency nothing.

Simplifications

  • A retry budget gives each layer a bucket that fills with a share of the calls it makes and loses one for each retry, starting with ten retries in reserve. One circuit breaker guards the calls to the dependency: it opens after a number of failures in a row, lets one probe through after a pause, and closes when the probe succeeds. Real libraries differ in how they count failures and how many probes they allow.
  • No real client library and no connection-level effect is modelled: no connection set-up, no connection pool, no queueing in the network, no load balancer choosing another instance. The numbers illustrate the mechanism; they are not measurements of a product.

Parameter values are illustrative. They are not benchmarks of any real product.

User requests shows 300: 150 succeeded, 147 gave up and 3 are still trying. Attempts at the dependency is 300 as well, one for each request. The 150 requests that arrived from 0 s to 5 s succeeded, and the ones that arrived since the outage began failed. In this lab a failure comes back at once, as an error.

A real dependency that is overloaded or cut off often says nothing at all, and then the caller finds out only when its timeout runs out. Press Restart. Set A failure looks like to “No answer (a timeout)” and Outage lasts to 20 s, so that the dependency stays down, and leave Timeout at 1 s. Press Skip 5 s, which takes the lab to 15 s. User requests says 150 succeeded, 270 gave up and 30 still trying.

Those 30 are the calls held while they wait for their timeout. Now press Restart, make the same two changes, set Timeout to 2 s and press Skip 5 s: 150 succeeded, 240 gave up and 60 still trying. With Timeout at 3 s it is 150 succeeded, 210 gave up and 90 still trying. The rule from Little’s Law holds: 30 requests a second times 1 s, 2 s or 3 s is 30, 60 or 90 requests in flight. All of them are waiting for an answer that is not coming, and each one holds what a request holds. A shorter timeout frees them sooner. What the lab cannot show is the price of that: a dependency that is only slow would have answered some of them, and a timeout that is too short throws those answers away.

A retry is a bet that the failure was brief. If calls fail now and then for reasons that have nothing to do with each other, a dropped packet or a restarting instance, the bet is good. When 20% of attempts fail independently, all four attempts of a request fail with probability 0.2 × 0.2 × 0.2 × 0.2, which is 0.0016, or 0.16%. Three retries turn a failure rate of 20% into one of 0.16%.

An outage is the opposite case. The failures are not independent, because they all have the same cause, and a retry meets the same cause as the first attempt. It then adds a call. This lab retries every failed attempt at once, up to three more times.

A failed attempt is tried again at once, up to three more times. While the dependency is down it receives four attempts for every request, which is the traffic it can least afford.

Traffic
30 per second
Arrival

Steady spreads the requests evenly. In bursts, a second of requests arrives all at once, as when many clients act on the same schedule.

The dependency
5 s
8 s

While the dependency is down, every attempt fails.

0%

Share of attempts that fail on their own, even when the dependency is up.

60 per second

Attempts the dependency can serve in any one second. It turns the rest away.

A failure looks like

An error comes back after 0.1 s. With no answer, the caller waits the whole timeout before it can retry.

The callers
Layers that retry
1

Each layer calls the next one and retries that call. The last layer calls the dependency.

3 (attempts: 4)
1 s

How long a caller waits for an answer from the dependency.

Backoff

The wait before a retry. None retries at once, fixed waits 0.5 s every time, and exponential starts at 0.5 s and doubles each time, up to 4 s.

Jitter

Jitter makes each wait random. Full picks anywhere from zero to the delay, equal picks from half the delay to the delay. With no backoff there is no wait to randomise.

Protection

Each layer may retry only a share of the calls it makes, after a reserve of ten retries.

10% of calls

Closed, calls go through. After enough failures in a row it opens and calls fail at once. After a pause it is half-open and lets one probe through.

5 failures in a row
3 s
Timing
Time: 10 s

The lab runs in real time. Step moves on by one tenth of a second, Skip by five seconds.

What is happening

The dependency is down from 5 s to 13 s. Every attempt made now fails.

The caller makes up to 4 attempts, so one user request can become up to 4 attempts at the dependency.

In the last second, user requests: 30; attempts at the dependency: 120; served: 0. The dependency can serve 60 a second.

With no backoff, a failed attempt is retried at once.

User requests
300150 succeeded, 129 gave up, 21 still trying
Attempts at the dependency
7142.4 for each user request, the most for one: 4
At most for one request
4attempts at the dependency: 4
Attempts, last second
120served: 0, capacity: 60, busiest tenth of a second: 12
Circuit breaker
Offswitched off
Retries refused
0no retry budget
Attempts at the dependency in each tenth of a second
01020Outage010 seconds
  • Attempts (line)
  • Served (filled)
  • Outage (hatched)
Second by second, newest first (the chart in numbers). Now: 10 s.
SecondsNew requestsAttemptsServedBusiest tenth of a secondDependency
9 to 1030120012Down
8 to 930120012Down
7 to 830120012Down
6 to 730120012Down
5 to 63084012Down
4 to 53030303Up
3 to 43030303Up
2 to 33030303Up
Attempts at each layer since the start
CallAttempts so farFor each user requestAt most for one request
Layer 1 to the dependency7142.44

Circuit breaker log

The circuit breaker is switched off.

At 10 s, user requests so far: 300 (150 succeeded, 129 gave up, 21 still trying). Attempts at the dependency: 714, 2.4 for each request. Circuit breaker: Off.

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • The lab advances in steps of 0.1 s. An attempt that succeeds, or that is answered with an error, takes one step, and a retry is sent on the step after the failure at the earliest, so no backoff means 0.1 s. A seeded random draw decides each failure rate and each jitter, so the same settings give the same run.
  • Each layer calls the next one and retries that call. The layers above wait for the answer of the layer below, and only the attempt at the dependency has a timeout. Real services usually have a timeout at every layer, which leaves work running below after a caller has given up and can make the amplification worse than shown.
  • The dependency fails in three ways: an outage window, a failure rate, and a capacity (it serves a fixed number of attempts in any one second and turns the rest away). A failure is either an error at once or silence until the timeout; real failures are a mix of both. A turned-away attempt costs the dependency nothing.

Simplifications

  • A retry budget gives each layer a bucket that fills with a share of the calls it makes and loses one for each retry, starting with ten retries in reserve. One circuit breaker guards the calls to the dependency: it opens after a number of failures in a row, lets one probe through after a pause, and closes when the probe succeeds. Real libraries differ in how they count failures and how many probes they allow.
  • No real client library and no connection-level effect is modelled: no connection set-up, no connection pool, no queueing in the network, no load balancer choosing another instance. The numbers illustrate the mechanism; they are not measurements of a product.

Parameter values are illustrative. They are not benchmarks of any real product.

At 10 s the dependency is down, and Attempts at the dependency is 714 for 300 user requests, 2.4 for each, with at most 4 for one request. Attempts, last second shows 120, while the users sent 30 requests in that second: while the dependency is down it receives four times the traffic. Press Skip 5 s, to 15 s, where the dependency is back. Without retries, the first lab made 450 attempts by then and 207 requests had succeeded. With these retries, there are 1,152 attempts and 225 requests have succeeded: 702 more attempts bought 18 more answers.

A retry earns its cost only when three things are true.

  • The failure is transient. A timeout, a 503 or a dropped connection may go away. A 400 or a rejected payment will not, and repeating it only repeats the answer.
  • The operation is safe to repeat. A retry is a duplicate request. Reads are safe, and a write needs the protection of the idempotency module.
  • There is a limit. Google’s SRE book says to limit retries per request and never to retry a request indefinitely. The next lessons turn that limit into a wait, a budget and a breaker.

Queues retry too. A message queue puts a message back after its visibility timeout, and the messaging module shows how a limit on attempts and a dead-letter queue stop a message from circulating for ever.

What happens if every layer has its own timeout?

Section titled “What happens if every layer has its own timeout?”

Take a service that waits at most 3 s for each attempt and makes up to 3 attempts. In the worst case one call takes 3 × 3, which is 9 s, before it gives up. If the user’s browser gave up after 5 s, the service is still working at 9 s for someone who left four seconds ago. The timeouts of the layers add up, and a layer that does not know how much time its caller has left cannot choose a sensible limit. The RPC lesson shows the answer used by gRPC: a deadline that each hop passes on, as the time that is left.

The work does not stop when the caller leaves. A server that has accepted a request usually finishes it, and its capacity goes to an answer nobody will read. The labs in this module do not show that. The cache lab in when the cache fails does: a request that times out is lost to its user, and the database still does the work. Cascading failures shows how that wasted work, and the retries that follow it, keep an overloaded system from recovering.

  • A timeout is the caller’s limit on waiting. Choose it from a percentile of the dependency’s latency, and remember that it does not say whether the work was done.
  • A silent dependency holds the request rate times the timeout in calls at once: in the lab 30 × 1 s, 2 s and 3 s is 30, 60 and 90.
  • A retry cures brief, independent failures: four attempts at a 20% failure rate fail together 0.16% of the time.
  • In an outage a retry only adds load: at 15 s, 702 more attempts than the baseline bought 18 more answers.
  • Retry only transient failures of operations that are safe to repeat, and always with a limit.

Next: wait between retries, with backoff and jitter.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.