Boost SpringBoot Reliability with Fast‑Retry Asynchronous Retry Framework
Fast‑Retry is a high‑performance, asynchronous retry framework for SpringBoot that handles millions of concurrent tasks, offering configurable retry attempts, delay, exception handling, and custom result policies, and outperforms traditional synchronous solutions like Spring‑Retry and Guava‑Retry in throughput and resource usage.
1. Introduction
In distributed systems, external calls such as HTTP requests, database operations, or message queues may fail due to network glitches or temporary service unavailability. A retry mechanism improves robustness and user experience by automatically re‑executing failed operations. This article introduces Fast‑Retry , a high‑performance, asynchronous retry framework for SpringBoot.
2. Practical Examples
2.1 Add Dependency
<code><dependency>
<groupId>io.github.burukeyou</groupId>
<artifactId>fast-retry-all</artifactId>
<version>0.2.0</version>
</dependency></code>2.2 Programming Retry
Configure the retryer programmatically with custom result policies, exception handling, and delay settings.
<code>public String process() throws Exception {
// Custom result‑based retry policy: retry when result is not "success"
RetryResultPolicy<String> resultPolicy = result -> !result.equals("success");
FastRetryer<String> retryer = FastRetryBuilder.<String>builder()
.attemptMaxTimes(2)
.waitRetryTime(1, TimeUnit.SECONDS)
.retryIfException(true)
.retryIfExceptionOfType(RuntimeException.class)
.exceptionRecover(true)
.resultPolicy(resultPolicy)
.build();
CompletableFuture<String> future = retryer.submit(() -> {
int r = new Random().nextInt(10);
System.out.printf("Executing business method, random value: %d%n", r);
if (r != 1) {
// return a failure value that triggers the result policy
return "dead";
}
return "success";
});
return future.get();
}</code>Running the above code may produce:
<code>Executing business method, random value: 5
Executing business method, random value: 4
Executing business method, random value: 1
Result: success</code>If the maximum retry count is exceeded, an exception is thrown and the final result is null .
2.3 Annotation Retry
Using the @FastRetry annotation provides a concise way similar to Spring‑Retry.
<code>@FastRetry(
retryWait = @RetryWait(delay = 2),
exceptionRecover = false,
maxAttempts = 2,
retryStrategy = PackRetryPolicy.class)
public String business(Long id, String name) {
int r = new Random().nextInt(10);
System.out.printf("Executing business method, random value: %d%n", r);
if (r != 1) {
throw new RuntimeException("Invalid parameter: " + r);
}
return "success";
}</code>A custom result‑based policy can be defined as:
<code>public class PackRetryPolicy implements RetryResultPolicy<String> {
public boolean canRetry(String t) {
return !t.equals("success");
}
}</code>2.4 Asynchronous Retry
Fast‑Retry also supports asynchronous tasks.
<code>@FastRetry(
retryWait = @RetryWait(delay = 2),
maxAttempts = 2,
retryStrategy = PackRetryPolicy.class)
public CompletableFuture<String> asyncBusiness(Long id, String name) {
return CompletableFuture.supplyAsync(() -> {
System.out.println("async executing business method...");
int r = new Random().nextInt(10);
if (r != 1) {
return "1"; // trigger retry via result policy
}
return "success";
});
}</code>Output example:
<code>async executing business method...
async executing business method...
async executing business method...
FastRetryTimeOutException: The maximum retry count has been exceeded after 2 times. Stop retry</code>In contrast, Spring‑Retry does not retry asynchronous tasks:
<code>// Spring‑Retry annotation (does not retry async)
@Retryable(maxAttempts = 2)
public CompletableFuture<String> asyncBusiness(Long id, String name) {
// method body similar to above, throws exception on failure
}</code>Result:
<code>async executing business method...
RuntimeException: Invalid parameter: 3</code>Fast‑Retry’s asynchronous support and configurable policies make it a superior choice for high‑throughput, resource‑efficient retry handling.
Performance Comparison
Tests were conducted with an 8‑thread fixed pool, each task performing five retries with a 2‑second interval (total 10 seconds). Fast‑Retry demonstrated exponential performance gains over Spring‑Retry and Guava‑Retry, especially as task volume increased.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.