Reliability and Cost Engineering › Retries, Redundancy, and the Independent-Failure Assumption

Michael BrenndoerferDecember 2, 20266 min read
Language AI HandbookAll chapters
Part 64 of 67Compound AI System Design
Chapter 490Reliability and Cost Engineering › Retries, Redundancy, and the Independent-Failure Assumption
Reading section 4 of 12 · Retries, Redundancy, and the Independent-Failure Assumption · 6 minSection 4 of 12 · 6 min

Section 4 of 12 · 6 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

If an operation has a 99.5% success probability independently on each attempt, three attempts give 1−0.0053=99.9999875%1-0.005^3=99.9999875\% success. During a shared outage, failure on one attempt can make the next failure more likely.

With kk independent attempts and per-attempt success probability pp, all attempts fail with probability (1−p)k(1-p)^k. The probability that at least one succeeds is its complement. In the second model, an outage with probability rr lasts for the entire retry sequence. Conditional on no outage, the attempts are independent with success probability pp, giving unconditional success (1−r)[1−(1−p)k](1-r)[1-(1-p)^k].

PythonIn [7]
CollapseExpand
def success_independent(p, k):
    return 1 - (1 - p) ** k


def success_correlated(p, k, r):
    # r = probability of correlated outage where EVERY attempt fails.
    # otherwise (1-r) of the time the attempts are iid with success p.
    return (1 - r) * success_independent(p, k)


p = 0.995
for k in (1, 2, 3):
    ind = success_independent(p, k)
    print(f"k={k} independent: {ind * 100:.7f}%")

print()
for r in (0.0, 0.001, 0.005):
    row = [success_correlated(p, k, r) * 100 for k in (1, 2, 3)]
    print(
        f"correlated outage r={r:g}: "
        + "  ".join(f"k={k}:{v:.7f}%" for k, v in zip((1, 2, 3), row))
    )
ConsoleOut [7]
CollapseExpand
k=1 independent: 99.5000000%
k=2 independent: 99.9975000%
k=3 independent: 99.9999875%

correlated outage r=0: k=1:99.5000000%  k=2:99.9975000%  k=3:99.9999875%
correlated outage r=0.001: k=1:99.4005000%  k=2:99.8975025%  k=3:99.8999875%
correlated outage r=0.005: k=1:99.0025000%  k=2:99.4975125%  k=3:99.4999876%

With r=0.001r=0.001, one attempt succeeds with probability 0.999⋅0.995=0.9940050.999\cdot0.995=0.994005. Three attempts raise that to about 99.9%, the outage-imposed ceiling. During the all-fail outage, a policy that uses all three attempts sends three times as many calls as the one-attempt policy. Across the entire window, most healthy requests still finish on the first attempt.

A retry can recover from a transient failure. During sustained overload, the added work can prevent the dependency from recovering. The retry policy also needs to respect the original request's deadline.

  • Distinguish failure classes. The example marks a few response codes as retryable. In production, check the dependency's contract, the request's remaining deadline, and whether repeating the operation is safe. A timeout alone cannot tell you whether a write took effect.
  • Cap attempts and add jitter. A cap limits calls per operation. Randomized delays reduce the chance that clients retry in sync. The half-fixed, half-random delay in the example is equal jitter.
  • Budget retries. Track total downstream attempts divided by original requests to see the load multiplier. Its excess over one is extra attempts per request when every request starts with one call.
  • Shed load when retries cannot help. When evidence indicates a dependency is unavailable, a circuit breaker can skip that dependency and fail fast or route to a viable fallback.

This retry simulator uses capped exponential backoff with jitter. It records latency and counts how many calls an original request generates.

PythonIn [8]
CollapseExpand
def classify(outcome_code):
    # Map a synthetic outcome to retry policy.
    if outcome_code == "ok":
        return "success", False
    if outcome_code in ("429", "500", "503", "timeout"):
        return "transient", True
    return "hard", False  # 400, 401, 422, policy refusal -> do not retry


def backoff_delay(attempt, rng, base=0.05, cap=2.0):
    # attempt is 1-indexed (after first try, attempt=1 for the first retry).
    raw = min(cap, base * (2 ** (attempt - 1)))
    return raw * (0.5 + rng.random() * 0.5)  # equal jitter


def simulate_request(server_transient, rng, max_attempts=3):
    """One logical request. Returns (success, total_latency, n_attempts)."""
    total_lat = 0.0
    attempts = 0
    while attempts < max_attempts:
        attempts += 1
        # Base call latency ~ lognormal; a "down" server returns 503 fast.
        if rng.random() < server_transient:
            code = "503"
            total_lat += rng.lognormal(np.log(0.02), 0.2)
        else:
            code = "ok"
            total_lat += rng.lognormal(np.log(0.4), 0.6)
        kind, retryable = classify(code)
        if kind == "success":
            return True, total_lat, attempts
        if not retryable:
            return False, total_lat, attempts
        if attempts < max_attempts:
            total_lat += backoff_delay(attempts, rng=rng)
    return False, total_lat, attempts


def run_batch(server_transient, max_attempts, n=20_000, seed=11):
    rng = np.random.default_rng(seed)
    succ, lats, atts = [], [], []
    for _ in range(n):
        s, l, a = simulate_request(
            server_transient, rng, max_attempts=max_attempts
        )
        succ.append(s)
        lats.append(l)
        atts.append(a)
    succ = np.array(succ)
    lats = np.array(lats)
    atts = np.array(atts)
    return dict(
        success=succ.mean(),
        p99=float(np.percentile(lats, 99)),
        mean_att=atts.mean(),
        max_att=int(atts.max()),
        amplification=atts.sum() / n,
    )

Compare a 1% per-attempt failure probability with a 40% probability. Both regimes draw independently on every attempt. The second models frequent failures, distinct from the shared outage in the previous table.

ConsoleOut [9]
CollapseExpand
--- healthy  (1% transient) ---
  no-retry : success=0.9899  p99=1.63s  attempts/req=1.00
  retry(3) : success=1.0000  p99=1.58s  attempts/req=1.01  max_att=3
  amplification on retry: 1.01x  (total attempts / original requests)

--- impaired (40% transient) ---
  no-retry : success=0.5964  p99=1.45s  attempts/req=1.00
  retry(3) : success=0.9381  p99=1.63s  attempts/req=1.56  max_att=3
  amplification on retry: 1.56x  (total attempts / original requests)

The next cell reruns the two three-attempt settings with the same seeds and plots their mean attempts per original request. Use the earlier table for p99 latency, which this figure does not show.

PythonIn [10]
CollapseExpand
regime_names = ["1% failure", "40% failure"]
measured = [run_batch(rate, max_attempts=3) for rate in (0.01, 0.40)]
amp_vals = [batch["amplification"] for batch in measured]
bar_x = np.arange(len(regime_names))
VisualizationOut [11]
CollapseExpand
Bars near 1.01 and 1.56 attempts per request for simulated failure probabilities of one and forty percent respectively.
Simulated attempts per request with up to three tries. Both settings use independent per-attempt failures. The dashed baseline is one call per original request.

With failure probability ff and at most three independent attempts, the second attempt occurs with probability ff and the third with probability f2f^2. The expected number of attempts is 1+f+f21+f+f^2: about 1.01 in the healthy setting and 1.56 at 40% failures. In the latter setting the theoretical success probability rises from 60% to 1−0.43=93.6%1-0.4^3=93.6\%. The printed p99 includes both outcomes and can change when fast failures are replaced by successful calls that do more work.

Retries are a load multiplier, not free availability

Three attempts can triple volume during an outage if every request exhausts its attempts. Monitor the attempts-per-request ratio and use the routing controls to stop sending work down a path that cannot recover within the deadline.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy