Spring / Spring Retry Interview Questions
What is the difference between RetryTemplate.execute() and RetryTemplate.executeWithLoadBalancer()?
RetryTemplate.execute() is the standard method for running a retryable operation. It takes a RetryCallback and optionally a RecoveryCallback, then applies the configured retry policy and backoff until the operation succeeds or retries are exhausted.
There is no method named executeWithLoadBalancer() in the standard Spring Retry library. This is a common interview trap. The term is sometimes confused with:
- Spring Cloud LoadBalancer + Retry: Spring Cloud has its own retry integration through
spring-cloud-starter-loadbalancerwhich retries HTTP requests on different service instances using a load balancer. This is not part of Spring Retry itself but uses Spring Retry under the hood. - RetryTemplate with stateful execute:
retryTemplate.execute(RetryCallback, RecoveryCallback, RetryState)— the three-argument form that uses stateful retry with aRetryStatekey, which is the closest concept but not load balancing.
The actual execute overloads in RetryTemplate:
// Stateless <T, E> T execute(RetryCallback<T, E> retryCallback); <T, E> T execute(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback); // Stateful <T, E> T execute(RetryCallback<T, E> retryCallback, RecoveryCallback<T> recoveryCallback, RetryState retryState);
If an interviewer asks about this, the correct answer is to clarify that load balancer retry is a Spring Cloud feature layered on top of Spring Retry, not a method on RetryTemplate.
More Related questions...