How Netflix Ribbon Implements Client‑Side Load Balancing in Java

This article explains Netflix's open‑source Ribbon client‑side load balancer, detailing how it intercepts @LoadBalanced RestTemplate calls, resolves service names to IPs using various algorithms, and forwards requests via HttpClient, while highlighting its key features such as in‑process balancing, multiple strategies, fault‑tolerance, and protocol support.

Lobster Programming
Lobster Programming
Lobster Programming
How Netflix Ribbon Implements Client‑Side Load Balancing in Java

Overview

Ribbon is Netflix’s open‑source client‑side load balancer. It moves the load‑balancing decision from the server to each client instance, allowing the client to choose a concrete service endpoint at runtime based on a configurable algorithm.

Integration with Spring Cloud

When a Spring Boot application includes spring-cloud-starter-netflix-ribbon, Ribbon automatically creates a LoadBalancerInterceptor that wraps any RestTemplate bean annotated with @LoadBalanced. The interceptor extracts the logical service name from the request URL, resolves it to a set of reachable instances, applies the selected load‑balancing rule, and forwards the request to the chosen host.

Typical Bean Definition

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Service‑Name Resolution

Ribbon obtains the list of instances for a logical service name from a service‑discovery client (e.g., Eureka, Consul) or from a static configuration file ( application.yml / application.properties). The result is a collection of Server objects containing host IP and port.

Load‑Balancing Rules

After the instance list is retrieved, Ribbon selects one entry according to the configured rule. Common built‑in rules are:

RandomRule – picks an instance uniformly at random.

RoundRobinRule – cycles through instances in order.

WeightedResponseTimeRule – prefers instances with lower recent response times.

BestAvailableRule – selects the instance with the fewest concurrent connections.

ConsistentHashRule – hashes a request attribute (e.g., URL path) to achieve sticky routing.

The rule can be changed by setting the property ribbon.NFLoadBalancerRuleClassName for the target service.

HTTP Request Execution

Once an instance is chosen, Ribbon creates an HttpClient (Apache HttpClient by default) and issues the actual HTTP call to http://{host}:{port}/{path}. The response is returned to the original caller as if the request had been made directly to the logical service name.

Key Technical Features

In‑process load balancing : The decision is performed inside the client JVM, eliminating an extra network hop.

Pluggable algorithms : Developers can implement IRule to provide custom selection logic.

Fault tolerance : Ribbon tracks health metrics; unhealthy instances are automatically excluded. It integrates with Hystrix for circuit‑breaker semantics.

Protocol agnostic : While most examples use HTTP, Ribbon also supports TCP and UDP via the Client abstraction.

JavaSpringresttemplateRibbonclient-side load balancing
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

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.