Optimizing My First Web Application Using Load Balancer
In this article, I would like to share my experience in using Load Balancer. This is my first Medium post, so I would like to offer some background on myself. I am an undergraduate computer science student at University of Indonesia.
What is Load Balancer
Load balancing is the process of distributing network traffic efficiently across multiple servers. Load balancer decides which servers can handle that traffic so no one server is overworked. If a server goes down, the load balancer redirects traffic to the active servers.
Why We Need Load Balancer
The following are load balancer’s role:
- Distribute incoming traffic across multiple servers efficiently
- Maintain reliability and high availability by redirecting requests only to the servers which are available
- Adding and removing servers in the network easily as per demand
Benefits of Load Balancing
There are some benefits of using load balancer such as:
- Less downtime. If a server goes down, load balancer will redirect the traffic to another active server.
- The content will be loaded faster. This implies a better user experience for visitors. Load balancer could distribute the load equally among servers or deliver the content to user from the nearest available servers. These will make the servers respond faster.
Software Load Balancer vs Hardware Load Balancer
Load balancers could run as hardware appliances or as software-defined. When you use the hardware version, the vendors will load the software onto the machine they provide. If the traffic at your website is increase, you have to buy more or bigger machines from the vendor. As computing moves to the cloud, lately people prefer using the software version. Software version is less expensive and more flexible. You can install the software on the hardware of your choice or in cloud environments.
How Load Balancer Works
The way a load balancer works is quite simple:
- The load balancer, a software program, is listening on the port where client make requests.
- When a request comes in, the load balancer takes that requests and forwards it to a server that is available.
- The server then fulfills the request and replies back to the load balancer
- Finally, the load balancer passes on the reply from the server to the client.
The way load balancer chooses the server based on one of the following algorithm :
- Round Robin — Requests are distributed across the group of servers sequentially.
- Least Connections — A new request is sent to the server with the fewest current connections to clients.
- IP Hash — The IP address of the client is used to determine which server receives the request.
This way the user is never aware of the division of functions between the load balancer and each backend server.
Steps in doing load balancing
Here are some prerequisites to do load balancing:
- Load Balancer
- Web application
I used Nginx as my load balancer and Django as my website framework to create my website application.
- You can install Nginx on https://nginx.org/en/download.html?_ga=2.62050220.1209641971.1574994167-326907252.1574994167. I downloaded the mainline version.
2. In the folder of Nginx that we have just downloaded, double click nginx.exe, then allow access so you can use it on your computer.
3. To check whether nginx has worked, runserver the django app. And then type in your browser ‘localhost’.
**Notes: you only need to type ‘localhost’ without specifying the port like ‘localhost:8000’
4. If Nginx has worked as the load balancer, you will see this on your browser.
After finished installing Nginx on your device, next I will show you how to configure Nginx so we can use it as the load balancer for our website application.
Configuring Nginx as our load balancer is quite simple and easy, we only need to edit a file named nginx.conf inside conf folder. Now open up the file using your text editor and I will give some introductory before editing the configuration:
- Go to the server block (inside the http block).
2. Inside the server block, you will see this.
Look at the listen and server_name variables. It means that it will wait the request which comes from ‘localhost’ to port 80 (the default port).
3. You will also see ‘location /’ block. Inside this block, it specifies the html file (index.html) that will show up whenever you type ‘localhost’. That’s the reason why when we type ‘localhost’ the browser shows ‘Welcome to Nginx’ which is specified inside the index.html
In order to configure nginx as our load balancer, we only need to edit those blocks I have mentioned before.
- First, we have to specify which servers that we will use. Inside the http block, create upstream block include the variable name (I create this block above the location block). Inside this block, specify the servers that we want to use. For example, the variable name is ‘django’, localhost as the server and the port is 8000 and 9000.
2. Now go to the location block. We want the browser to show our django app whenever we type ‘localhost’ (not ‘Welcome to Nginx’). So we can replace the content of this block by reverse the proxy and pass the request that comes to the upstream that we have just created in step one.
*Notes: the ‘django’ in ‘http://django’ refers to the name of the upstream i’ve created before.
3. Every time you change the configuration, double click the ‘nginx.exe’ in order it will reload the new configuration.
4. Now we have finished the configuration. Let’s check whether our load balancer has worked as we expect.
Check Whether The Load Balancer Has Worked
- Open 2 cmd that route to your django project. One of the cmd, type ‘python manage.py runserver 8000’, while the others type ‘python manage.py runserver 9000’.
**Notes: we need to specify the port based on we have configured in the nginx.
2. Type ‘localhost’ in your browser. Now you will not see the ‘Welcome nginx anymore’, but your django project.
3. Refresh the page, and see the cmd. You will notice that the port 8000 and 9000 serve our django project alternately.
4. Now try to shut down one of them by press Ctrl+C in one of your cmd and then refresh your page. You will see that your page still works because one of the server still run. This means our load balancer has worked.
Congratulation, you have just finished doing load balancing! Pretty simple and easy, right? Although it’s simple, load balancing has a great impact in order to maintain high-availability for our website