8.3 Client Side Load Balancing

When discovering services from Consul, Eureka or other Service Discovery servers the DiscoveryClient will emit a list of available ServiceInstance.

Micronaut by default will automatically perform Round Robin client-side load balancing using the servers in this list. This combined with Retry Advice adds extra resiliency to your Microservice infrastructure.

The load balancing itself is handled by the LoadBalancer interface which defines a LoadBalancer.select() method that returns a Publisher that emits a ServiceInstance.

The Publisher is returned because the process for selecting a ServiceInstance may result in a network operation depending on the Service Discovery strategy employed.

The default implementation of the LoadBalancer interface is DiscoveryClientRoundRobinLoadBalancer. You can replace this strategy for another implementation if you wish to customize how client side load balancing is handled in Micronaut since there are many different ways you may wish to optimize load balancing.

For example, you may wish to load balance between services in a particular zone or you may wish to load balance between servers that have the best overall response time.

To replace the LoadBalancer used you should define a bean that replaces the DiscoveryClientLoadBalancerFactory.

In fact that is exactly what the Netflix Ribbon support does, described in the next section.