java
43 lines · 8 steps
Exponential backoff retry in Java
A reusable executor retries a failing operation with exponentially growing, jittered delays until it succeeds or exhausts its attempts.
Explained by
highlit
1public final class RetryExecutor {
2
3 private final int maxAttempts;
4 private final Duration initialDelay;
5 private final double multiplier;
6 private final Duration maxDelay;
7
8 public RetryExecutor(int maxAttempts, Duration initialDelay, double multiplier, Duration maxDelay) {
9 this.maxAttempts = maxAttempts;
10 this.initialDelay = initialDelay;
11 this.multiplier = multiplier;
12 this.maxDelay = maxDelay;
13 }
14
15 public <T> T execute(Callable<T> operation) throws Exception {
16 Exception lastFailure = null;
17 long delayMillis = initialDelay.toMillis();
18
19 for (int attempt = 1; attempt <= maxAttempts; attempt++) {
20 try {
21 return operation.call();
22 } catch (Exception e) {
23 lastFailure = e;
24 if (attempt == maxAttempts) {
25 break;
26 }
27
28 long jitter = ThreadLocalRandom.current().nextLong(delayMillis / 2 + 1);
29 long sleep = Math.min(delayMillis + jitter, maxDelay.toMillis());
30 try {
31 Thread.sleep(sleep);
32 } catch (InterruptedException ie) {
33 Thread.currentThread().interrupt();
34 throw ie;
35 }
36
37 delayMillis = (long) (delayMillis * multiplier);
38 }
39 }
40
41 throw new IllegalStateException("Operation failed after " + maxAttempts + " attempts", lastFailure);
42 }
43}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Exponential backoff spreads retries over widening intervals so a struggling dependency gets room to recover.
- 2Adding random jitter prevents many clients from retrying in lockstep and hammering a service simultaneously.
- 3Restoring the interrupt flag and rethrowing on InterruptedException keeps the operation responsive to cancellation.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
rust
use std::collections::VecDeque; use std::sync::{Arc, Condvar, Mutex}; use std::time::{Duration, Instant};
Building a counting semaphore in Rust
concurrency
synchronization
condition-variable
Advanced
9 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/exponential-backoff-retry-in-java-explained-java-0861/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.