Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How does Spring Cloud LoadBalancer work with a @LoadBalanced RestClient.Builder or WebClient.Builder, and what replaced Netflix Ribbon?

Spring Cloud LoadBalancer is the client-side load balancer that replaced Netflix Ribbon, which went into maintenance and was removed from Spring Cloud. When we mark a RestClient.Builder, RestTemplate or WebClient.Builder bean with @LoadBalanced, Spring adds an interceptor or filter that treats the host in a URL such as http://orders/... as a service name, asks a ServiceInstanceListSupplier for the instances from discovery (optionally cached and health-checked), and picks one, round robin by default. Random and custom algorithms can be configured, and retries on other instances can be enabled for idempotent calls.

Why this question is asked

In a microservice system, a service rarely talks to a fixed host. The orders service may have six instances today and ten tomorrow, with new addresses each time. Client-side load balancing lets the caller choose an instance itself, without a load balancer box in the middle. Interviewers ask about it because the @LoadBalanced annotation looks like magic, and they want to know what it actually adds and what replaced the Netflix stack. In this answer, we will recap client-side load balancing, set up a load-balanced RestClient, follow one call through the interceptor, look at where instances come from, and then change the algorithm and add retries.

1A Quick Recap of Client-Side Load Balancing

With server-side load balancing, the caller sends every request to one address, a load balancer, which forwards it to a healthy backend. With client-side load balancing, the caller knows the list of backend instances and picks one itself for each request. That removes a network hop and a central component, and lets the client make smarter choices, such as preferring instances in its own zone.

For years, Spring Cloud used Netflix Ribbon for this. Netflix put Ribbon into maintenance mode, and Spring Cloud replaced it with its own Spring Cloud LoadBalancer, which has been the default since the 2020.0 release train, when Ribbon support was removed. LoadBalancer is simpler, works with both blocking and reactive clients, and plugs into any discovery mechanism.

2Setting Up a Load-Balanced Client

We add spring-cloud-starter-loadbalancer, which most discovery starters already include, and mark the builder bean with @LoadBalanced. Every client built from that builder then resolves service names.

Java
@Configuration
class ClientConfig {
@Bean
@LoadBalanced
RestClient.Builder loadBalancedBuilder() {
return RestClient.builder();
}
}
@Service
class OrderClient {
private final RestClient rest;
OrderClient(RestClient.Builder builder) {
this.rest = builder.baseUrl("http://orders").build(); // "orders" is a service name, not a host
}
Order find(long id) {
return rest.get().uri("/orders/{id}", id).retrieve().body(Order.class);
}
}
// discovery: orders -> 10.0.3.17:8080, 10.0.4.22:8080, 10.0.5.9:8080
// find(1) -> sent to 10.0.3.17:8080
// find(2) -> sent to 10.0.4.22:8080
// find(3) -> sent to 10.0.5.9:8080 (round robin)

The same works for a RestTemplate bean and for a WebClient.Builder bean. Notice that without @LoadBalanced, http://orders would be resolved by DNS, which inside Kubernetes might work through the Service, but outside it would usually fail with an unknown host error.

3What Happens Inside One Call

@LoadBalanced is a qualifier: Spring Cloud finds beans carrying it and customises them. For RestTemplate and RestClient it adds a LoadBalancerInterceptor, a hook that runs before each request is sent. For WebClient it adds an exchange filter function that does the same job reactively.

The interceptor reads the host part of the URL, orders, and asks the load balancer for an instance of that service. The load balancer gets the current instances from a ServiceInstanceListSupplier, applies its algorithm, and returns one instance. The interceptor then rewrites the URL to that instance's real host and port and lets the request continue. If no instance is available, the call fails at once with an error saying there is no instance for the service, rather than hanging.

Step throughA call through a @LoadBalanced RestClient
OrderClientLB interceptorLoadBalancerInstance supplierorders 10.0.4.22
GET http://orders/orders/2
choose an instance of orders
get instances
3 instances (cached, healthy)
10.0.4.22:8080 (round robin)
GET http://10.0.4.22:8080/orders/2
200 order 2

7 messages in order. Play them, or step through them one at a time.

4Where the Instances Come From

The ServiceInstanceListSupplier is the piece that knows which instances exist. The default one asks the DiscoveryClient, which may be backed by Eureka, Consul, Kubernetes or a fixed list in configuration. Asking discovery on every request would be wasteful, so the default chain wraps it in a cache: instances are reused for a short time, 35 seconds by default (check your version), and the cache is backed by Caffeine when it is on the classpath.

Suppliers can be stacked like layers. A health-check supplier pings instances and filters out those that fail. A zone-preference supplier keeps instances in the caller's zone when there are any. We pick these through the spring.cloud.loadbalancer.configurations property, for example health-check or zone-preference, or by defining our own supplier bean.

5Changing the Algorithm and Adding Retries

Round robin is the default: each request goes to the next instance in turn. A RandomLoadBalancer is included, and we can write our own by implementing ReactorServiceInstanceLoadBalancer. The algorithm is chosen per service with @LoadBalancerClient and a configuration class. That class must not be picked up by component scanning, or it would apply to every service.

Java
@Configuration
@LoadBalancerClient(name = "orders", configuration = RandomForOrders.class)
class LoadBalancerSetup {}
class RandomForOrders { // not annotated with @Configuration, so scanning ignores it
@Bean
ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment env,
LoadBalancerClientFactory factory) {
String name = env.getProperty(LoadBalancerClientFactory.PROPERTY_NAME); // "orders"
return new RandomLoadBalancer(
factory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
}
}
# Retries (blocking clients use Spring Retry when it is on the classpath)
spring.cloud.loadbalancer.retry.enabled=true
spring.cloud.loadbalancer.retry.max-retries-on-same-service-instance=0
spring.cloud.loadbalancer.retry.max-retries-on-next-service-instance=1
spring.cloud.loadbalancer.retry.retry-on-all-operations=false # GET only

Retries let a failed call try another instance, which hides a single bad instance from the caller. Notice the last line: by default only GET requests are retried, because repeating a POST could create the same order twice. We enable retries for other methods only when the operation is idempotent, meaning that doing it twice has the same effect as doing it once.

6Follow-up questions

Do we need Spring Cloud LoadBalancer on Kubernetes?

Not always. A Kubernetes Service already spreads connections across pods, so many teams call http://orders and let the platform do it. Client-side balancing adds value when we want per-request choices such as zone preference, health filtering or retries on another instance, or when discovery is not Kubernetes.

Why does an injected RestClient.Builder sometimes not load-balance?

Because Spring Boot also provides its own RestClient.Builder bean. If we inject the builder without the @LoadBalanced qualifier at the injection point, we may get Boot's plain builder. Injecting with @LoadBalanced on the parameter, or giving our bean a clear name, avoids the confusion.

What replaced Hystrix and Zuul at the same time?

Spring Cloud CircuitBreaker with Resilience4j replaced Hystrix, and Spring Cloud Gateway replaced Zuul 1. Together with LoadBalancer replacing Ribbon, that removed the Netflix stack apart from Eureka, which is still maintained.

7Conclusion

In this answer, we saw that Spring Cloud LoadBalancer replaced Ribbon, that @LoadBalanced on a RestClient.Builder, RestTemplate or WebClient.Builder adds an interceptor that resolves service names, that a ServiceInstanceListSupplier provides instances from discovery with caching and optional health checks, and that round robin is the default, with random or custom algorithms and retries for idempotent calls available through configuration.

What to say in the interviewAbout 80 seconds aloud

Open with

Spring Cloud LoadBalancer is the client-side load balancer Spring Cloud uses since Netflix Ribbon went into maintenance and was removed in the 2020.0 release train. The idea is that the caller picks an instance itself instead of going through a central load balancer.

Then, if they want more

In code, I define a RestClient.Builder, RestTemplate or WebClient.Builder bean with @LoadBalanced and use a service name as the host, like http://orders.

The annotation makes Spring Cloud add a LoadBalancerInterceptor, or a filter function for WebClient, which runs before each request: it takes the host, asks the load balancer for an instance of that service, rewrites the URL to the real host and port, and sends the request.

The instances come from a ServiceInstanceListSupplier, which by default asks the DiscoveryClient, so Eureka, Consul, Kubernetes or a static list, and caches the result for a short time; we can add health-check or zone-preference suppliers through configuration. The default algorithm is round robin; there is a random one, and we can plug in our own per service with @LoadBalancerClient.

Retries on another instance can be enabled, and by default only GET is retried, which is right, because only idempotent calls should be retried. One gotcha is injecting Boot's plain RestClient.Builder instead of the load-balanced one.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Spring Cloud LoadBalancer work with a @LoadBalanced RestClient.Builder or...": https://scrivio-interview-library.netlify.app/spring-boot/spring-cloud/d6c482f48a/