Enable Automatic Retries for Spring Cloud Ribbon Calls

This guide explains how to configure Spring Cloud Ribbon with @LoadBalanced RestTemplate to add client‑side load balancing and a robust retry mechanism that automatically re‑issues failed requests across service instances.

Programmer DD
Programmer DD
Programmer DD
Enable Automatic Retries for Spring Cloud Ribbon Calls

When using Spring Cloud Ribbon for client‑side load balancing, developers typically annotate a RestTemplate with @LoadBalanced so that calls can be made using service names instead of fixed URLs.

For example, a request to the hello-service at the /hello endpoint will have the service name resolved by the load‑balancer, which selects an instance from the service registry according to the configured strategy.

In practice, an instance may fail before the service‑governance system removes it, causing a request to error out. To build a more resilient application, a retry strategy is needed so that a failed request is automatically retried on another instance rather than returning an immediate failure.

Starting with Spring Cloud Camden SR2, Spring Cloud integrates Spring Retry, allowing developers to enable retry behavior simply through configuration properties.

Key configuration parameters (add them to your application.yml or application.properties): spring.cloud.loadbalancer.retry.enabled – Enables the retry mechanism (default is false).

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds

– Sets the Hystrix timeout; it must be larger than Ribbon’s timeout to trigger retries. ribbon.ConnectTimeout – Connection timeout for the request. ribbon.ReadTimeout – Read timeout for the response. ribbon.OkToRetryOnAllOperations – Allows retries on all operation types. ribbon.MaxAutoRetriesNextServer – Number of retry attempts on a different server after the first failure. ribbon.MaxAutoRetries – Number of retry attempts on the same server.

With these settings, when a request encounters a faulty instance, the client will first retry the current instance; if it still fails, it will switch to another instance and retry according to the configured counts. If all attempts fail, the call finally returns an error.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

RetryresttemplateSpring Retryspring-cloudLoadBalancing
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.