Home/Learn/System Design/Retry & Exponential Backoff

Retry & Exponential Backoff

Intermediate
Reliability & Fault Tolerance

Retry with exponential backoff re-attempts failed operations with progressively longer delays, preventing thundering herd problems and giving failing services time to recover.

Overview

Transient failures (network glitches, momentary overload, connection resets) are common in distributed systems. Retrying a failed request can succeed on the next attempt. However, naive retries (immediately, unlimited) can overwhelm an already struggling service. Exponential backoff spaces retries exponentially: 1s, 2s, 4s, 8s, etc. Adding jitter (random delay) prevents multiple clients from retrying in sync (thundering herd). A maximum retry count prevents infinite loops. Retry should only be used for transient/retriable errors (500, 503, timeout, connection refused) — never for client errors (400, 401, 404). Combined with circuit breakers and idempotency keys, retry with backoff is a cornerstone of resilient distributed systems.

Exponential Backoff with Jitter

Each retry waits longer than the previous one. Jitter adds randomness to prevent synchronized retries from multiple clients hitting the server at the same time.

Java — exponential backoff with jitter
// Exponential backoff formula
// delay = min(base * 2^attempt, maxDelay)
//
// Attempt 1: 1s
// Attempt 2: 2s
// Attempt 3: 4s
// Attempt 4: 8s (capped at maxDelay)
//
// With full jitter:
// delay = random(0, min(base * 2^attempt, maxDelay))
//
// With equal jitter:
// temp = min(base * 2^attempt, maxDelay)
// delay = temp/2 + random(0, temp/2)

// Java implementation
public <T> T retryWithBackoff(Supplier<T> operation, int maxRetries) {
    int attempt = 0;
    while (true) {
        try {
            return operation.get();
        } catch (RetriableException e) {
            if (++attempt > maxRetries) throw e;
            long delay = Math.min(1000L * (1L << attempt), 30_000L); // exp backoff
            long jitter = ThreadLocalRandom.current().nextLong(delay); // full jitter
            Thread.sleep(jitter);
            log.warn("Retry attempt {} after {}ms: {}", attempt, jitter, e.getMessage());
        }
    }
}

Spring Retry & Resilience4j

Spring Retry and Resilience4j provide declarative retry with configurable backoff, max attempts, and retriable exception filtering.

Java + YAML — declarative retry configuration
// Spring Retry — annotation-based
@Retryable(
    retryFor = { ServiceUnavailableException.class, TimeoutException.class },
    noRetryFor = { BadRequestException.class },
    maxAttempts = 3,
    backoff = @Backoff(delay = 1000, multiplier = 2, maxDelay = 10000)
)
public OrderResult placeOrder(OrderRequest req) {
    return orderClient.submit(req);
}

@Recover
public OrderResult placeOrderFallback(Exception e, OrderRequest req) {
    log.error("All retries exhausted for order: {}", req.getId());
    return OrderResult.failed(req.getId(), "Service temporarily unavailable");
}

// Resilience4j retry config
resilience4j:
  retry:
    instances:
      paymentService:
        maxAttempts: 3
        waitDuration: 1s
        enableExponentialBackoff: true
        exponentialBackoffMultiplier: 2
        retryExceptions:
          - java.net.ConnectException
          - java.util.concurrent.TimeoutException
        ignoreExceptions:
          - com.example.BadRequestException

Key Points to Remember

  • 1Retry only transient errors (500, 503, timeout) — never client errors (400, 401, 404).
  • 2Exponential backoff prevents overwhelming a recovering service: delay = base * 2^attempt.
  • 3Add jitter to prevent thundering herd — multiple clients retrying at the same moment.
  • 4Always set a maximum retry count to prevent infinite loops.
  • 5Combine retry with circuit breakers and idempotency for robust fault tolerance.

Interview Questions

Sign in to ask Aria
1

What is exponential backoff and why is jitter important?

EasyTCS
2

Which types of errors should you retry and which should you not?

MediumAmazon
3

How does retry interact with circuit breakers?

MediumGoogle
4

What is the thundering herd problem and how does jitter solve it?

MediumFlipkart
5

Design a retry strategy for a distributed payment processing pipeline.

HardUber

Ask Aria about Retry & Exponential Backoff

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…