System Design Academy

Saving…

Lesson 1 of 10 in Module 50 of 54 lessons complete

Failure Is Normal: Timeouts, Retries & Backoff

Module 5, lesson 1Concept12 min

Definition

Timeout

In plain English
A timeout is the longest you'll wait for a response before giving up.
Think of it like
It is like hanging up after the phone rings for a minute.

Retry

In plain English
A retry is sending a failed request again in the hope it succeeds the next time.
Think of it like
It is like redialling when a call drops.

Exponential backoff

In plain English
Exponential backoff means waiting longer after each failed attempt, such as 1, 2, then 4 seconds.
Think of it like
It is like knocking, waiting, then waiting longer before each next knock.

Why it exists

Your refill service waits for an insurer that stopped responding. Waiting calls keep connections busy until other users cannot get through.

If every caller immediately tries again, the failing insurer receives even more work. Your recovery attempt can prolong the outage.

How it works

Jitter

In plain English
Jitter adds a small random amount to each wait so clients don't all retry at the same instant.
Think of it like
Like a crowd leaving a stadium through different gates at slightly different times.

Retry storm

In plain English
A retry storm is when many clients retry at once and the extra traffic keeps a struggling service down.
Think of it like
Like everyone redialling a jammed helpline at once.

Retry budget

In plain English
A retry budget caps retries to a small share of normal traffic, such as 10%.
Think of it like
Like a rule that you only redial three times an hour.
  1. Set a deadline

    Put a timeout on every network call and a total deadline around all attempts.

  2. Check safety

    Retry only operations safe to repeat, such as idempotent reads or charges protected by an idempotency key.

  3. Spread attempts

    Increase the wait after failures, then add jitter.

  4. Stop

    Cap attempts and total time, and refuse retries when the shared retry budget is exhausted.

A client times out, waits with jitter, receives 503, waits longer, then receives 200.

Backoff leaves recovery time between attempts.

A client times out, waits with jitter, receives 503, waits longer, then receives 200.

This simulation succeeds on attempt three; each attempt checks its time limit, and seed 7 makes jitter repeatable.
Go
package main

import (
	"context"
	"fmt"
	"math/rand"
	"time"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	rng := rand.New(rand.NewSource(7))
	flaky := func(ctx context.Context, attempt int) bool { return ctx.Err() == nil && attempt == 2 }
	for attempt := 0; attempt < 3; attempt++ {
		one, stop := context.WithTimeout(ctx, 20*time.Millisecond)
		success := flaky(one, attempt)
		stop()
		fmt.Println("attempt", attempt+1, "success", success)
		if success || attempt == 2 {
			return
		}
		wait := (10 * time.Millisecond << attempt) + time.Duration(rng.Intn(5))*time.Millisecond
		select {
		case <-ctx.Done():
			return
		case <-time.After(wait):
		}
	}
}
ResultRetry unchanged?
TimeoutOnly if safe to repeat; the first attempt may have completed
503 Service UnavailableFor safe operations within budget
429 Too Many RequestsWait for Retry-After, then check safety and budget
400 Bad RequestNo; correct the request
401 UnauthorizedNo; fix authentication first

Examples

Stop waiting forever

Insurer checks have a two-second timeout and up to three retries with backoff.

Three retries mean four total attempts. A total deadline can stop the sequence earlier.

A refill request calls the insurer and returns a temporary failure when the two-second timeout expires.

An insurer outage no longer holds a refill request forever.

A refill request calls the insurer and returns a temporary failure when the two-second timeout expires.

The insight

Every network call needs a timeout.

Trade-offs

Pros

  • Survives brief failures.
  • Frees resources from slow calls.
  • Spreads recovery traffic.

Cons

  • Adds load and waiting time.
  • Short timeouts reject healthy slow calls.

Use when

  • Every network call needs a timeout.
  • Temporary failures affect safe operations.

Avoid when

  • Repeating an action can duplicate its effect.
  • The request needs correction before it can succeed.

Quick check

Why add jitter to retry waits?
Which response should you NOT retry?

Takeaways

  • Put a timeout on every network call.
  • Retry only safe operations, with exponential backoff and jitter.
  • Cap retries so they don't become the outage.

Try the quick check above whenever you feel ready.

Previous: Delivery Guarantees & Idempotency