SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
  • Message Queues
  • Pub/Sub
  • Message Brokers
  • Delivery Semantics
  • Dead Letter Queues
  • Kafka Deep Dive
  • Retries with Backoff
  • RabbitMQ and SQS
  • Event Streaming vs Message Queues
  • Message Ordering
  • Task Queues, Workers and Scheduled Jobs
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/APIs and Communication/Asynchronous Communication

Retries with Backoff

Try again after a growing wait, with a little randomness, so retries help instead of hurt.

⏱ 12 min readIntermediateMust KnowInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Retrying with backoff means that when a call fails for a short-lived reason, you try again, but you wait longer each time and add a little randomness.

  • Many failures are temporary: a network blip, a restarting server, a busy service. Trying again often works.
  • Retrying at once, over and over, can hurt a service that is already struggling.
  • Exponential backoff (wait 1 s, 2 s, 4 s, 8 s...) gives the service room to recover. Jitter stops all clients from coming back at the same moment.

02Concept: What is it?

In a system of many machines, calls fail all the time. A packet is lost, a server is restarting, a database is slow, or a rate limit says "too many requests". A retry is simply trying the same call again. It turns many small, temporary failures into successes that the user never sees.

But retries have a dark side. If a service is down because it is overloaded, then thousands of clients retrying at once add even more load. The service cannot recover, and the failure lasts longer. This is called a retry storm or a thundering herd.

Three ways to wait before a retry
Fixed wait
Retry 1, 2, 3, 41 s, 1 s, 1 s, 1 s

Clients come back together, again and again.

Exponential
Retry 1, 2, 3, 41 s, 2 s, 4 s, 8 s

Calmer, but still in step with each other.

Exponential + jitter
Retry 1, 2, 3, 4random between 0 and the cap

Spread out. The load stays smooth.

All three start when a call fails. Only the last one spreads the clients out.

The formula

Exponential backoff with a cap and jitter (illustrative)
delay_n   = min(cap, base * 2^n)          # n = 0 for the first retry
wait      = random(0, delay_n)            # "full jitter"
# give up when attempts > max_attempts OR total time > deadline
🧠Only retry what can succeed later
Retry temporary errors: timeouts, connection resets, 503 and 429 (and honour a Retry-After header if one is sent). Do not retry errors that will never change, such as 400 (bad request) or 404.

03Real-Life Example

🍵Everyone Calling the Same Shop
A shop is closed for ten minutes because of a power cut. A hundred customers call every ten seconds to ask, "Are you open yet?". When the power returns, the phone rings non-stop and the staff cannot even answer. Now imagine each customer waits a bit longer after every call, and picks a slightly different moment. The phone rings gently, a few calls at a time, and the staff answer everyone.
The shop
Retries
Customers who all call every 10 s
Clients that retry at a fixed time
The shop closed for ten minutes
A service that is down for a short while
Staff buried when the power returns
A retry storm hitting the recovering service
Waiting longer after each call
Exponential backoff
Each person choosing a random moment
Jitter
Giving up after a few tries
Max attempts or a deadline
Ordering the same pizza twice by mistake
Retrying without idempotency
The life of a retried call
📞Callfails (503)
→↓wait + jitter
🔁Retryfails again
→↓wait longer
🔁Retryworks

04Why Do We Need It?

🩹Hide small failures
Most failures last a moment. A retry fixes them before the user notices.
🌱Let services recover
Longer waits give an overloaded service space to catch up instead of being hit again.
🌊Avoid retry storms
Jitter spreads retries over time, so the load stays smooth, not in waves.
⚖️Bounded effort
A maximum number of tries or a deadline stops a call from hanging forever.
💸Save cost
Fewer pointless calls mean less load and often a smaller bill.
🧯Pair with breakers
Retries plus a circuit breaker stop retrying when a service is clearly down.
Retry the temporary errors, wait longer each time, add randomness, and stop after a limit.

05Where Do We Use It?

🔌Service-to-service calls
HTTP and gRPC clients retry on timeouts and 503 errors, often built into the client library.
🗄️Database connections
A connection that drops is reopened with backoff after a failover.
📨Message consumers
A failed message is retried later, and then moved to a dead letter queue.
📱Mobile and web apps
Apps retry on a weak network, with growing waits to save battery.
☁️Cloud SDKs
Cloud SDKs retry throttled calls with exponential backoff and jitter by default.
🪝Webhook delivery
Senders retry failed webhooks on a schedule that gets longer and longer.

06Types / Parts / How It Works

Backoff strategies

StrategyHow the wait is chosenGood and bad
Immediate retryNo waitFine once for a tiny blip. Dangerous in a loop: it hammers the service.
Fixed intervalThe same wait every timeSimple. But all clients stay in step, so waves of load appear.
Linear backoffWait grows by a fixed step (1 s, 2 s, 3 s...)Gentler, but it grows slowly, so a long outage still sees many retries.
Exponential backoffWait doubles (1 s, 2 s, 4 s, 8 s...) up to a capBacks off fast. Still synchronised if all clients started together.
Exponential + jitterA random wait up to the exponential valueSpreads clients out. The usual best choice.

Kinds of jitter

KindWait isNotes
Full jitterrandom(0, delay)Spreads the most. A very short wait is possible.
Equal jitterdelay / 2 + random(0, delay / 2)Keeps some minimum wait, spreads less.
Decorrelated jitterrandom(base, previous wait x 3), cappedEach wait depends on the last one, so clients drift apart.

Where retries can multiply

Retries at every layer multiply
📱App3 tries
→↓x3
🧩Service A3 tries
→↓x9
🧩Service B3 tries
→↓x27
🗄️Databasedrowning
1 request More calls Retry amplification
Three layers that each try 3 times can turn 1 user request into up to 27 calls at the bottom (3 x 3 x 3), when the bottom service is failing.
VisualizerFixed, exponential and jittered retries after an outageMany clients call a service that is down, then recovers. Switch strategies and compare the peak load. Try more clients, a lower capacity, and a short base wait to see retry storms.
Retry strategy

The wait doubles each time (base x 2^n, capped). Clients are still in step with each other, so the waves are just further apart.

Requests the service receives each second (red band = outage, dashed line = capacity of 100 per second)
0 s20 s40 s
Peak load
600
requests in one second
Total requests
1800
for 300 clients
Succeeded
300
of 300
Gave up
0
out of attempts
All done at
15.5 s
last success
Strategy
Peak / s
Total requests
Done at
Fixed wait
600
2400
never
Exponential, no jitter
600
1800
15.5 s
Exponential + full jitter
861
1986
never
After the service recovered, the retry waves pushed it over its capacity in 1 second(s) (peak 600 per second). The extra requests were rejected, so they had to retry again.

A simplified model with a simulated clock (100 ms ticks). All clients start at second 0, so the first wave is synchronised. The jitter uses a fixed seeded generator, so the same settings always give the same picture. Each wait is capped at 16 s. The numbers are illustrative, not a benchmark.

07How to Use It / Steps / Implementation

Retrying safely

  1. 1
    Decide what is retryable
    Retry timeouts, connection errors, 502/503/504 and 429. Do not retry 4xx errors that are your own fault, such as 400, 401 or 404.
  2. 2
    Check idempotency first
    A retry may run the same action twice. Make writes idempotent, or send an idempotency key, so a repeat does not charge a customer twice.
  3. 3
    Use exponential backoff with a cap
    Start with a small base (for example 100 to 500 ms), double each time, and cap the wait (for example 30 s).
  4. 4
    Add jitter
    Pick a random wait up to the cap. This is the single most useful step against retry storms.
  5. 5
    Limit the effort
    Set a maximum number of attempts and a total deadline. When the budget is gone, fail clearly.
  6. 6
    Retry in one layer
    Choose where retries happen (often the outermost client or one layer) so they do not multiply through the stack.
  7. 7
    Respect the server
    If the response has a Retry-After header, wait at least that long. Combine with a circuit breaker and watch your retry rate.
A small helper (illustrative pseudocode)
for attempt in 0..maxAttempts:
    result = call()
    if result.ok or not result.isRetryable:
        return result
    ceiling = min(cap, base * 2^attempt)
    sleep(random(0, ceiling))      # full jitter
fail("out of attempts")
⚠️A retry budget protects the whole system
Some systems limit retries to a share of normal traffic (for example, allow retries to add at most 10% more requests). This keeps retries from taking over when many calls fail at once.

08Key Terms to Know

TermMeaning in easy words
RetryTrying the same call again after it failed
Transient errorA short-lived failure that may go away on its own
BackoffWaiting between retries, usually longer each time
Exponential backoffA wait that doubles after every failed try, up to a cap
JitterA random amount added to or used for the wait, to spread clients out
Thundering herdMany clients acting at the same moment and overwhelming a service
Retry stormA flood of retries that keeps a struggling service down
Retry budgetA cap on how many retries are allowed, as a share of traffic or a total time
DeadlineThe total time after which the caller gives up, however many tries are left
Idempotency keyA unique ID that lets the server recognise a repeated request and not do it twice
Retry-AfterAn HTTP header in which the server says how long to wait before trying again

09Pros, Cons and Trade-offs

✅ Pros
  • Hides short failures from users
  • Gives struggling services time to recover
  • Jitter smooths the load
  • Easy to add, often built into clients
  • Works well with queues and dead letter queues
⚠️ Cons
  • Can repeat a non-idempotent action
  • Can multiply load through many layers
  • Adds delay to the slow path
  • Hides a real problem if nobody watches the retry rate
  • Wrong settings can make an outage worse

Trade-offs

ChoiceWe gainWe lose
More attemptsMore chances to succeedMore load and a longer wait before a final failure
Longer base waitCalmer service, smaller wavesSlower recovery for users
Higher capFewer calls during long outagesA single client may wait a long time
Full jitterBest spreading of loadSome retries come very quickly
Short deadlineFast, clear failureGives up before a service that would have recovered
Retries in every layerEach layer is robust aloneRetry amplification (3 x 3 x 3 = 27)

10Common Problems and Mistakes

Common mistakes

  1. Retrying without a limit. An endless loop keeps a request and a thread busy forever.
  2. No jitter. Clients stay in step and hit the service together after every wait.
  3. Retrying non-idempotent calls. A payment is taken twice, or two emails are sent.
  4. Retrying errors that cannot succeed. A 400 or 404 will fail every time.
  5. Retries at every layer. One failure becomes dozens of calls at the bottom.
  6. Ignoring Retry-After. The server asked for time, and you kept pushing.
  7. Not logging retries. A system that is silently retrying a lot is sick, and nobody sees it.

Things to watch

🌪️Retry storm
Load rises sharply after an outage ends. Add jitter and a cap on the rate of retries.
🧮Amplification
Count the retries across the whole call chain, not just one service.
⏱️Timeouts vs retries
A retry needs a timeout on each try. Without it a hung call blocks the retry.
🔌Breaker interplay
When the circuit breaker is open, do not retry. Fail fast or use a fallback.

11Real-World Examples

☁️Cloud SDKs
The SDKs of the large cloud providers retry throttled and failed calls with backoff and jitter by default.
🪝Webhook senders
Payment and code-hosting services retry a failed webhook on a schedule with growing gaps.
📨Queue consumers
Message systems redeliver a failed message after a delay, then dead-letter it.
🕸️Service meshes
A mesh can retry for you, with limits and budgets set in one place.
🗄️Database drivers
Drivers reconnect with backoff after a failover or a dropped connection.
📶TCP itself
TCP retransmits lost packets with a growing timer, the same idea at the network layer.
📝Note
Defaults differ between libraries and versions, so check the settings of the client you use before relying on them.
Mini case: recovering after an outage
👥300 clientsall failed at the same time
→↓backoff + jitter
🧩Servicerecovering, smooth load
With jitter, the 300 clients spread over several seconds instead of arriving together.

12Related Topics to Read Next

Idempotency→

A retry is only safe if repeating the call does no harm.

Circuit Breaker→

Stops retries when a service is clearly down.

Rate Limiting→

A 429 tells you to slow down, and backoff is how you do it.

Dead Letter Queues→

Where a message goes after its retries run out.

Delivery Semantics→

Retries give at-least-once delivery, with duplicates.

Handling Failures→

Retries are one tool in the wider toolbox of failure handling.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔What: try again after a failure, waiting longer each time, with randomness.
  • ✔Formula: wait = random(0, min(cap, base x 2^n)).
  • ✔Retry: timeouts, 5xx, 429. Do not retry: 400, 401, 404.
  • ✔Safe only if: the call is idempotent or carries an idempotency key.
  • ✔Limits: max attempts, a total deadline and a retry budget.
  • ✔Layers: 3 tries in each of 3 layers can mean 27 calls. Retry in one place.
  • ✔Pair with: timeouts, circuit breakers, dead letter queues, Retry-After.
  • ✔One line: “If it fails, wait a bit, wait longer next time, and do not all come back at once.”
← Previous
Kafka Deep Dive
Next →
RabbitMQ and SQS