Optimizing My First Web Application Using Load Balancer

Load Balancer Simulation

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

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.

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:

  1. The load balancer, a software program, is listening on the port where client make requests.

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.

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:

  1. Load Balancer

I used Nginx as my load balancer and Django as my website framework to create my website application.

Install Nginx

  1. You can install Nginx on https://nginx.org/en/download.html?_ga=2.62050220.1209641971.1574994167-326907252.1574994167. I downloaded the mainline version.
Installing Nginx

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.

Allow access for Nginx

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.

Nginx has worked

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

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:

  1. Go to the server block (inside the http block).
Server 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.

  1. 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.
Upstream block

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.

Update the location block

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

  1. 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.
Run the app in different port

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

An undergraduate Computer Science Student at University of Indonesia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store