Open In App

Spring Cloud – Client Side Load Balancer

Last Updated : 25 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Spring Cloud is a collection of projects like load balancing, service discovery, circuit breakers, routing, micro-proxy, etc will be given by Spring Cloud. So spring Cloud basically provides some of the common tools and techniques and projects to quickly develop some common patterns of the microservices.

What is Load Balancer in Spring Cloud?

Taking about the general definition, Load Balancer is a network device or we can also say router that seats between a set of backend servers and clients. It distributes the incoming traffic to multiple servers to reduce the load. Load Balancers typically use various algorithms, such as round-robin to determine which server to send incoming traffic to.

s Load Balancer in Spring Cloud

 

So, in Spring, if you want to use Load Balancer then Spring Cloud provides us with some already developed ready-made Load Balancer. By using this you don’t need to write any Load Balancer or create your own, you can just use the tool very easily by importing some dependencies and writing minimal code.

Type of Load Balancer

There are two ways to load balance the request

  1. Client-Side Load Balancer
  2. Server-Side Load Balancer

So in this article, we are going to discuss what are the Client Side Load Balancers available in Spring Cloud.

1. Client-Side Load Balancer

If you are keeping the load balancer on the client side and giving the load balancing responsibility to the client, then it’s called Client-Side Load Balancing. Let’s understand this with an example in the context of Spring Boot Microservices.

Imagine we have two microservices called address-service and employee-service. Please refer to the below image. This is your address-service instance 1 and this is running on port number 8081. Another instance of the address app we have deployed inside a different server. So address-service instance 2 is running on port number 8081. Similarly, we have only one instance of employee-service and this is running on port number 8080. So basically we have started two instances of our address server or address service and one instance of employee-service.

Client-Side Load Balancer

 

Right now in our employee-service we are making calls to the address-service. So if there is no load balancer every time whenever we are making a call the call is going to port number 8081. But let’s say lots of loads are coming to the address-service instance 2 (Port – 8082), so we have scaled up our application and created another instance of our address-service (address-service instance 1). So what we want is sometimes the load should go to the address-service instance 1 and sometimes the load should go to the address-service instance 2. So this way the load will be distrusted between these 2 instances. So the goal is any request is coming we should equally maintain the load. And this thing could happen by introducing a Load Balancer.

So in the above example, employee-service is making calls to the address-service. So in this scenario, employee-service is the client and address-service is the server. You can think like the client and server are the same things but based on the scenario, for example in this scenario employee service is needing the address service data. So the employee is the client of the address.

In the above example, this is the client-side load balancer because right now the employee is a client. So here instead of making a call directly, we will introduce a load balancer. And now the load balancer will help us to balance the load. Sometimes it will route the call to address-service instance 1 and sometime it will route the call to address-service instance 2. The client will call the address-service in a load-balanced way.

So in Client-side Load Balancing the instances of the microservice are deployed on several servers. The logic of Load Balancer is part of the client itself and it carries the list of servers and determines to which server a particular request must be directed based on some algorithm.

Client Side Load Balancing

 

So sequentially the load balancer will route each request coming from the employee service to different instances of address service. For this load balancer, we have Spring Cloud Load Balancer and if you are coming from a legacy background you also would have heard the name Ribbon. Ribbon is a particular load balancer given by Netflix you don’t have to write any code to develop this load balancer or to make this pattern happens. We can simply use the Netflix Ribbon to have the client-side load balancing. But right now spring has simply removed the Ribbon from 2020 as part of the Spring Cloud and they’re recommending using the Spring Cloud Load Balancer instead of the Ribbon load balancer.

Note: Client-Side Load Balancer Provided by Spring Cloud: Spring Cloud Load Balancer, Netflix Ribbon

Advantages of Client-Side Load Balancing

  • In the case of the traditional load balancer approach no more single point of failure.
  • Less network latency as the client can directly call the backend servers.
  • Cost Reduction as there is no need for server-side load balancing.

Disadvantages of Client-Side Load Balancing

  • The microservice code is combined with the load balancer’s logic.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads