Open In App

Spring Cloud – Load Balancer

Last Updated : 14 May, 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. Before understanding What is Load Balancer let’s understand what is the need for Load Balancer in Spring Applications first?

What’s the Problem?

The problem is here 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.

Spring Cloud - 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.

What is Load Balancer in Spring Cloud?

Taking about the general definition, Load Balancer is a network device that sits 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. Please refer to the below image and observe how the above problem is fixed by Load Balancer.

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

Let’s understand both of them by taking the same above example. 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.

Note: Here we are keeping the load balancer on the client side and giving the load balancing responsibility to the client, hence it’s called Client-Side Load Balancing. Similarly the same goes for Server-Side Load Balancer.

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.

Point to Remember:

  • Client-Side Load Balancer Example: Spring Cloud Load Balancer, Netflix Ribbon
  • Server-Side Load Balancer Example: Spring Cloud Gateway, Netflix Zuul

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads