javaintermediate
Retry Mechanism with Exponential Backoff
Implement retries with exponential backoff, jitter, max attempts, and exception-specific handling.
javaPress ⌘/Ctrl + Shift + C to copy
import java.time.Duration;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Supplier;
public class Retry {
public static <T> T withRetry(
Supplier<T> action,
int maxAttempts,
Duration initialDelay,
Set<Class<? extends Exception>> retryableExceptions
) {
Exception lastException = null;
long delayMs = initialDelay.toMillis();
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return action.get();
} catch (Exception e) {
lastException = e;
boolean retryable = retryableExceptions.isEmpty()
|| retryableExceptions.stream().anyMatch(cls -> cls.isInstance(e));
if (!retryable || attempt == maxAttempts) {
throw new RuntimeException(
"Failed after " + attempt + " attempts", e);
}
// Exponential backoff with jitter
long jitter = ThreadLocalRandom.current().nextLong(delayMs / 4);
long sleepMs = delayMs + jitter;
System.out.printf("Attempt %d/%d failed: %s. Retrying in %dms%n",
attempt, maxAttempts, e.getMessage(), sleepMs);
try {
Thread.sleep(sleepMs);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException("Retry interrupted", ie);
}
delayMs = Math.min(delayMs * 2, 30_000); // cap at 30s
}
}
throw new RuntimeException("Failed", lastException);
}
// Fluent builder version
public static <T> T retry(Supplier<T> action) {
return withRetry(action, 3, Duration.ofMillis(500), Set.of());
}
public static void main(String[] args) {
Random rng = new Random();
String result = withRetry(
() -> {
if (rng.nextInt(3) != 0) {
throw new RuntimeException("Transient error");
}
return "Success!";
},
5,
Duration.ofMillis(200),
Set.of(RuntimeException.class)
);
System.out.println(result);
}
}Use Cases
- Resilient API calls with transient failure handling
- Database connection retry logic
- Network request retry with backoff
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Retry with Exponential Backoff
Retry failed async operations with exponential backoff, jitter, and configurable retry conditions.
Best for: API call resilience
#nodejs#retry
pythonintermediate
Retry Decorator with Exponential Backoff
Generic retry decorator with configurable attempts, exponential backoff, and exception filtering.
Best for: Network request resilience
#decorator#retry
pythonbeginner
Tenacity Retry with Backoff
Add robust retry logic with exponential backoff, jitter, and conditional retry using the tenacity library.
Best for: Flaky API calls
#retry#tenacity
pythonintermediate
Retry Logic for Data Pipelines
Configurable retry decorator with exponential backoff and jitter for resilient data pipeline tasks.
Best for: Resilient API calls in data pipelines
#retry#resilience