A load balancer is a system that sits between users and multiple servers to distribute incoming network traffic evenly, preventing any single server from becoming overwhelmed. The Round Robin algorithm is a simple load balancing technique where each incoming request is handled by the next available server in a sequential order, cycling back to the first server after reaching the last one. This is implemented in Java using an ArrayList of Server objects and a currentIndex variable that increments with each request and wraps around using the modulus operator (currentIndex = (currentIndex + 1) % servers.size()).
Round Robin Load Balancing in Java: A Practical Guide
Added:Have you ever shopped during big billion days or else Diwali sales and wondered with crows of people shopping at once how does that website not crashed? You know behind the scenes it's the power of something called as load balancer. In this video we are going to explore how load balancing works in real life and yeah let's implement that using Java.
Now imagine you're at a shopping mall and there is just only one billing counter. Now picture thousand people trying to check out there you know from the same billing counter you know it's very hectic right but what if there are 10 billing counters and there is a friendly manager who is guiding each person to the next available counter that manager is your load balancer in the back end load balancer sits between the user and multiple servers means you know for example user will be sending a request that request will be handled by the load balancer That load balancer will distribute that request to next available servers. Means there will be multiple servers will be there. User will be sending a request.
That request will be hand over to the respective server from the load balancer. When a user opens your site, the load balancer decides which server will handle that user's request. In order to make that happen uh there are lot of algorithms are there like uh roundroin is there leashed connections IP hash like that today we will try to implement roundroin load balancing using Java you know in roundroin algorithm each incoming requests will be handled by the next available server in order let's build a simple Java simulation with three servers and a load balancer distributing 15 incoming connections so in order to To make that happen, I'm having three classes like I'm taking three classes. Server class, load balancer and uh e-commerce load balancer. So first let me just try to write the code for server class. In this I'm going to take a private string variable. Let me just try to go like this. Okay, string name.
And I want to take a constructor here.
Let me make it as public parameterizer constructor.
And here I'm going to use a parameter exactly as name. Now I want to assign this value to my instance variable. So let me just try to go like uh name and obviously I'll be using this keyword as simple as this. Okay. Now I have created a server class. In this server class, I'm having a private string variable and I'm having a constructor parameterized constructor and I want to write a method. Okay, just let me try to write public void handle requests. Okay, handle requests. Just a second.
Handle request. Now I want to make this method as a parameterizer method. Just let me try to write uh int request id. Okay, just a small statement system.out.print ln here.
All right, I'm going to write server plus name is handling the handling the requests request ID plus request ID. Let me just try to write like this.
Okay, this is great. Now I have taken a server class. In the server class this is a private string variable and uh this is a constructor parameterized constructor. My intention is whatever the value this name is having I want to assign that value to my instance variable and I just wrote a small method handle request method and of course it is a parameterizer method request okay request id and uh system.print length server name and here request ID. All right that's it. Not more than that. Now let me just try to write the code for load balancer. Okay, this is completely separate class. Now in this class uh yeah I want to take a private p r i vta.
Okay, just array list. Let us try to deal with the array list. Okay. And this array list will accept the generic that is server.
Okay. array list. Now I'm going to take a private int variable. Let me make this as current index and I'm going to make it as zero. I'll explain this later. Wait for it. All right. Now let me just try to take a method just a normal method. Okay.
Public void.
Let me make this as distribute uh requests I guess. Okay, distribute requests. And here I'm just trying to make this as a parameterizer method.
Enter request id just like this. So I have taken a private array list. Okay. And uh private variable current index. Let me just try to take a constructor also.
Public parameterizer constructor by the way public load balancer and I'm going to take my parameterizer constructor I want to pass array list as its parameter and uh yeah here also I want to use let me go like uh equal to l so this array list values I'm assigning to my instance variable okay now inside this method distribute requests method I'll do one thing let me try to write uh get method.
I'll be using get method al.get current index. Okay, it is going to give me a server here. I'll go with my server. Okay, now with the help of this reference, I will be calling this method. Which method? Here we are having this handle requests method, right? The same method I'm going to call here.
And of course it's a parameterizer method. Let me just try to pass the parameter that is request ID. All right, I'm passing the parameter that is request ID. This is cool. Now you know what I'm trying to implement roundroin algorithm, right? So in order to make that happen I need to write a small only one line of code that is let me just try to write uh current index + 1 modulus modulus u I'll go with al dot size and I will assign this to again current index basically this is the heart of our code. Let me just try to save it. Basically, this line number 18 is the heart of roundroin algorithm.
I'll explain this. Wait for it. So, yeah, we have our server class and we have our load balancer class. Right now, let me just try to go with our e-commerce. Let me just try to minimize a little bit.
Okay, now it's better. Now, in this class, um I'm going to take my main method. Sure. Let me just try to take a main method. Inside this main method, I want to take an array list. Okay. New array list. And uh the generic I'm passing this as server server generic.
Okay.
Let me just try to take this equal tovr server and of course it is of type array lists server. Okay. Now in this array list I need to pass server objects. Right. So let me just try to create three server objects. My intention is I want to distribute 15 incoming requests between three servers. So I'm going to create an object three times. Okay, this is let me make this as server one and uh exactly like same three objects.
This is server B and this is server C.
Let me just try to make this as s_ub_2 and s3. Now I want to add this into my error list. Yeah, sure. Let me go like uh server dot add s1 server dot add server dot add s2 and just one more server dot add s3. So right now I had written I had passed all these three server objects into our error list.
Okay, let me just try to create an object for this class load balancer class new. Okay, load balancer like this. Just a second load balancer you know we are having a parameterizer construct here, right? So basically we need to pass the parameter.
I'm passing our array list as a parameter. Let me make this as LB and here load balancer. All right. Now I want to simulate 15 incoming connections. So obviously I'll be using for loop here. Two semicolons int i = 1 and uh I should be less than or equal to 15 means 15 incoming connections and I ++. All right. Now I'll be calling this distribute requests method. Let me LB dot distribute request and the request ID is okay. I'm just trying to go with like I enough that's it. All right. Now what happens here? What happens here guys? Please observe here clearly.
Whenever we are creating our error list okay and we are passing our server objects into this error list. How many servers we have created? Like how many server objects we have created? Three server objects we have created. Okay, let me do one thing. For example, let me take this as an array list. Let's for consider this as an array list. So here one two. Okay. Now I'm going to keep this as I see here we are server A and uh here server B and here server C. So totally there are three servers are there. All these three servers are present inside an array list. Okay. And uh you guys know that index position when you're speaking about index position in array list index position starts with zero, right? 0 1 and 2. Length starts with obviously length starts with one.
One just a second. Let me Okay. 1 2 and 3 just like this. Okay. Now let me just try to write like this for our understanding purpose. Index in DX this is these are the index positions and basically this is the length L E N GH length.
Okay. Just give me let me try to write like this. Okay, index position starts with zero. Length starts with one. So right now in our array list, in our array list, what is the size of our array list? Please check this. The size of our array list is three. So basically we are trying to pass three server objects into our array list. Right? This is the meaning of that code here. whenever we are adding this.
So we are passing three servers into our error list. Now I'm calling this load balancer. I'm creating an object for this load balancer class and I'm passing this array list into this load balancer constructor as a parameter. Whatever the parameter which I'm passing the same values will be assigned to this array list. I mean this is the instance variable which is present in this load balancer. Right? Okay. Now here we are simulating we are using for loop and we are simulating 15 incoming connections.
Now let's deal with the first one means I value is one first request totally 15 requests are there. We are dealing with the first request. Okay. Now I'm going to copy this code here. Let me just try to paste this here for our better understanding.
Let me keep this.
Okay. Okay. I guess now so totally how many requests we are having? We are having 15 requests. 1 2 3 4 so on just so on up to 15 requests we are having.
Currently we are dealing with the first request means the request ID will be one. All right. Now here AL dot get current index. AL means this error list.
This error list. Okay. So, current index. What is the value for the current index here? The current index value is zero. All right. Let me just try to write here. Okay. C error ent.
Current index is zero. First starting the current index value is zero. So here let me just try to keep it like this. All right. Zero. Which element is present at the zero index position means which object is present at the zero index position server A. That server A will be referred here. And with the help of that server A, we are actually calling handle requests method. And please do remember this request ID. What is the request ID?
Still we are dealing with the first request that is request ID is one. So from here from handle request our company will be directly coming to here and request ID is one and yeah server server name what is the server name we have given here A. So server A is handling the request ID one will be printed. All right. After that coming back to here. Coming back to here. Now in this code what is happening here?
Current index. What is the current index? 0 + 1 that is 1. So right now 1 1 modulus al dot size. What is the size of our array list? It is three. One modulus 3.
What is the value of one modulus 3? That is one only. So this current index will be updated to one. Let me just try to write like this. Its value will be updated to one. Okay. Now again request ID two. All right. Now coming here current index value. Previously it is zero. Right. Now what is the value for this current index? One. Which object is present at the first index position server B. Now server B will be coming here and we are calling that with the help of this reference S.AL request. And kindly please do remember what is the request id we are currently dealing with that is two right. So again coming to here. So server name what is the server name? B server B is handling the request id two will be printed. After that again coming back here. Coming back here. Now please tell me what is the current index value? Previously it is one. Right? So 1 + 1 that is two. Let me just try to okay two here two 2 modulus 3 2 modulus 3 is two only so right now the current index value got updated to two all right now let's deal with the third request third incoming request okay now coming here request ID is three remember what is the current index right now the current index is two right okay so server C will be here and we'll be trying to call handle requests method and that method will be giving you the output server C. Server C is handling the request ID. What is the request ID we are dealing with? Three will be printed. Okay, done. Now coming back coming back again to this current index.
Okay. Now tell me what is the current index here? Two, right? 2 + 1 that is three. 3 modulus 3 will be giving you the output as zero means again the current index came with zero right I hope you're understanding the point here so right now the current index value is zero let me just try to keep it like this it is zero all right now we are dealing with the fourth request now We are dealing with the fourth request right? Whenever this fourth request is coming what is the current index value that is zero right? So yes alt get of0ero which object is available at the zero index position again we are cycling back means server A again the same process will be getting iterated means our first output will be when you're speaking about here our first output will be server A is handling the request ID 1 after that server B is handling the request ID 2 server C is handling the request ID 3 again cycling back means again server A is handling the request ID 4 5 6 like that let me just try to show you the output here right click run as Java application let me try to save it okay one more time right click run as Java application and please check this output here server A first request ID 1 after that server B request ID 2, server C request ID 3.
After this, as we are using roundroin algorithm again our server A will be trying to handle request ID 4. I hope you understand the point here. What if you know what uh let me just try to delay the response means I'll be using uh thread dots sleep uh let me just try to delay with the 300 milliseconds and I'll be using interrupted exception I mean throws clause let me just try to throw interrupted exception now let's see what happens right click run as Java application just let me try to save it Okay, same output absolutely same output but you know there will be slight delay I hope you're observing the point here right this is how roundroin algorithm will be working in real time as you can see the load balancer keeps cycling through the servers and distributing the requests evenly is roundroin load balancing in action what we built here is a very simplified version you know in real world like Amazon they use tools like EngineX, AWS elastic load balancer, Kubernetes ingress controller. These tools can handle traffic spikes, automatically scale servers and even do health checks.
If you found this helpful, imagine what more we can do with Java. Like, comment your thoughts and subscribe us for more such realtime examples explained with code. Thank you. See you in the next video.
Up Next

Virtual Nodes in Consistent Hashing: Load Balancing Explained
@thecodelucky
114 views•2025-12-13

Introduction to Secure Multiparty Computation with Yehuda Lindell
@fhe_org
7.7K views•2021-02-04

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science












































![[017] Load balancing in microservices [Microservice in and out 017]](https://i.ytimg.com/vi/liwjgNrfuFA/maxresdefault.jpg)