HARP logo

Retry

In distributed systems, calls to remote resources and services can fail for reasons that are short-lived, such as a brief network glitch, a momentary timeout, or a service that is busy for an instant. Many of these faults clear on their own within a short window.

The retry pattern handles these transient failures by automatically attempting a failed request again, with the goal of eventually succeeding. A single retry, or a small number of spaced retries, often turns a request that would have surfaced as an error into one that quietly succeeds.

HARP does not retry

A request that fails on a URL of a remote is not sent to another URL: the caller gets the error, and the next request goes to the next URL of the pool, round-robin.

What HARP does, at the proxy layer and outside your application code, is protect the calls that follow:

  • A remote has one pool of URLs, and requests go through it round-robin. When the active pool drops below min_pool_size (1 by default), the fallback URLs join it: there is no switch to a secondary pool.
  • Once you select a liveness algorithm, the circuit breaker takes a failing URL out of the pool. A URL taken out comes back after a probe succeeds; without a probe, when you bring it back from the dashboard or HARP restarts. See Health monitoring.

On a timeout, the HTTP cache decides what the caller sees. With the cache on (the default), no client timeout applies. With the cache off, a timeout before the response headers gives the caller a 504; one in the middle of the body leaves the caller with the upstream status and a truncated body. Either way the breaker counts a network_error. See Time limiting.

The HTTP client's connection retry

The only retry in HARP is the HTTP client transport's, set by http_client.transport.retries. It is off by default (0). The excerpt below allows two retries; the HTTP client documentation (opens in a new tab) lists the other settings.

toml
[http_client.transport.arguments]
retries = 2

When retries is above 0, the client retries only a failed connection setup (ConnectError or ConnectTimeout), to the same URL: a DNS failure, a refused connection, a connect timeout, or a TLS handshake failure, a bad certificate included. The first retry is immediate, then the delays double from 0.5 s (0.5 s, 1 s, 2 s, and so on). A failure after the request is sent is not retried.

Each attempt waits up to the connect timeout again: with timeout = 10 and retries = 2, a blackholed upstream takes about 30 s to fail. With the HTTP cache on, the system's connect timeout applies to each attempt.

A failure absorbed by one of those retries raises nothing, so it never reaches the circuit breaker: it does not count toward failure_threshold. When every retry fails, the error is counted once.

Retrying safely

This section is about retries in your application, the ones HARP leaves to the caller. If you retry, a few rules keep it safe.

Retry blindly only idempotent requests. Sending an idempotent request twice has the same effect as sending it once: GET, HEAD, OPTIONS, PUT and DELETE are, by definition (RFC 9110, section 9.2.2 (opens in a new tab)). POST is not. After a timeout you do not know whether the server processed it, so sending it again can duplicate its side effect, such as a second order or a second payment.

Listen to busy responses. 429 Too Many Requests (RFC 6585, section 4 (opens in a new tab)) and 503 Service Unavailable (RFC 9110, section 15.6.4 (opens in a new tab)) say the server is overloaded and a later attempt of an idempotent request may succeed. When the response carries a Retry-After header (section 10.2.3 (opens in a new tab)), wait that long before the next attempt. A 503 from HARP without Retry-After can come from a request the upstream already received: the connection dropped after it was sent. Do not retry a POST on it.

Space out the others with exponential backoff and jitter. Without Retry-After, double the wait after each failure (1 s, 2 s, 4 s...), up to a maximum wait and a maximum number of attempts. Then add jitter: randomise each wait, so that clients that failed together do not all come back together.

Backoff alone: retries arrive togetherClient AClient BClient C0 s1 s3 s7 sBackoff with jitter: retries spread outClient AClient BClient C0 s1 s3 s7 s

Three clients fail together at 0 s (cross). Without jitter they all return at 1, 3 and 7 s; with jitter each waits a different time. Times are illustrative.