Prev Next

Spring / Spring Retry Interview Questions

Can Spring Retry be used with Spring Cloud OpenFeign? How?

Yes, Spring Retry integrates with Spring Cloud OpenFeign to add retry capability to Feign client calls. When Spring Retry is on the classpath and spring.cloud.openfeign.okhttp.enabled or Feign defaults are in use, you can configure retry through Feign's own Retryer mechanism or delegate to Spring Retry's RetryTemplate.

Approach 1 — Feign native Retryer bean:

@Bean
public Retryer feignRetryer() {
    return new Retryer.Default(
        100,    // period (ms)
        1000,   // maxPeriod (ms)
        3       // maxAttempts
    );
}

Approach 2 — Wrapping the Feign interface with @Retryable:

@Service
public class OrderService {

    @Autowired
    private OrderFeignClient feignClient;

    @Retryable(retryFor = FeignException.ServiceUnavailable.class, maxAttempts = 3)
    public OrderDto getOrder(Long id) {
        return feignClient.getOrder(id);
    }
}

The second approach is more flexible because you can mix @Recover and backoff strategies from Spring Retry while Feign handles HTTP communication. Note: Feign's built-in Retryer and Spring Retry should not both be active for the same operation to avoid double-retry issues. Disable the default Feign retryer (Retryer.NEVER_RETRY) if using Spring Retry.

What should you do to avoid double-retry when using Spring Retry with Feign?
Which exception type is appropriate in retryFor when retrying Feign calls for 503 errors?

More Related questions...

What is Spring Retry and why is it used? How do you add Spring Retry to a Spring Boot project? What is the @Retryable annotation and what are its key attributes? What is the @Recover annotation and when is it invoked? What is the @Backoff annotation and how does it control retry delays? What is RetryTemplate and how does it differ from annotation-based retry? What are the different retry policies available in Spring Retry? What is a BackOffPolicy in Spring Retry and what implementations are available? What is a RetryContext and what information does it carry? How does the RetryListener interface work in Spring Retry? How does Spring Retry implement the circuit breaker pattern? What is stateful retry in Spring Retry and when should it be used? How do you configure Spring Retry using application properties in Spring Boot? What is the difference between include and exclude in @Retryable? How does Spring Retry integrate with Spring Batch? What common pitfalls should you avoid when using @Retryable? How do you write a unit test for a @Retryable method? How does Spring Retry handle exceptions that are not in the retryFor list? What is the RetryOperationsInterceptor and how is it used? How can you use Spring Retry with Spring WebClient or RestTemplate? How does the ExceptionClassifierRetryPolicy work? What is the RetryCallback interface in Spring Retry? How does Spring Retry differ from Resilience4j Retry? What is the RetryContextCache and when do you need a custom implementation? Can Spring Retry be used with Spring Cloud OpenFeign? How? How do you implement a custom RetryPolicy in Spring Retry? How do you use Spring Retry with Spring Kafka consumers? What is the proxyTargetClass attribute in @EnableRetry? How does the CompositeRetryPolicy work in Spring Retry? What is the difference between retry and idempotency? Why does it matter? How do you configure Spring Retry to use a fixed backoff programmatically? What is the maxAttemptsExpression attribute and how does it differ from maxAttempts? How does Spring Retry integrate with Spring's @Transactional? What is the use of the recover attribute in @Retryable? What are transient vs permanent failures and why does the distinction matter for retry? How do you implement retry with a custom exception condition using RetryPolicy? How does Spring Retry handle the case when @Recover returns void? What is exponential backoff with jitter and why is it preferred over plain exponential backoff? How can you monitor and observe Spring Retry behavior in production? What is the difference between RetryTemplate.execute() and RetryTemplate.executeWithLoadBalancer()?
Show more question and Answers...


Comments & Discussions