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");
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
