Spring / Spring Retry Interview Questions
How do you configure Spring Retry to use a fixed backoff programmatically?
A fixed backoff means the same delay is applied between every retry attempt, regardless of how many attempts have occurred. This is appropriate when the failure cause is likely to resolve in a predictable timeframe and exponential growth in delay is not needed.
Using RetryTemplate.builder() (modern fluent API):
RetryTemplate retryTemplate = RetryTemplate.builder() .maxAttempts(4) .fixedBackoff(2000) // 2 seconds between each retry .retryOn(IOException.class) .build();
Using legacy explicit configuration:
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); backOffPolicy.setBackOffPeriod(2000L); // milliseconds SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(4); RetryTemplate template = new RetryTemplate(); template.setBackOffPolicy(backOffPolicy); template.setRetryPolicy(retryPolicy);
The fluent builder API (available since Spring Retry 1.3) is more readable and less error-prone. It also supports chaining multiple configuration steps and inline recovery:
String result = RetryTemplate.builder() .maxAttempts(3) .fixedBackoff(1500) .retryOn(HttpServerErrorException.class) .build() .execute(ctx -> callApi(), ctx -> "fallback");
More Related questions...