Load Balancing
How do you think popular e-commerce websites handle millions of users in a fast and reliable manner? And most importantly, with no errors.
High Availability is what a good website has. It is a term used to describe a system that works perfectly in a long-term period. In it’s applications, a web does not rely only on one web server as it will overload and overwork the server thus resulting in a degradation of it’s performance. Multiple web servers is the way to go to maintain it’s high availability.
Now, how do we achieve this so called high availability?
A load balancer sits in front of your server distributing millions of user requests across a number of web servers. They improve the overall performance of applications by decreasing the burden on servers associated with managing and maintaining application and network sessions, as well as by performing application-specific tasks. It basically acts as a “traffic cop” in front of you servers while distributing requests from client as illustrated below.
How does it work?
Basically, it receives request from the client, then forwards the request to one of the servers. The server then returns the HTML content or resource needed back to the Load Balancer, then the load balancer forwards it back to the client.
Enough theory crafting.
Let’s get on to how to use a load balancer. There are several load balancers you can use such as Nginx, Apache, Tomcat, and HAProxy, you can chose any of them but now I’ll only be showing how to use Nginx on Windows.
First of all, go to nginx’ download url at http://nginx.org/en/download.html, it will show a page like below.
Then click on either of the versions of Nginx I boxed, I recommend using the stable one, but it doesn’t really matter. Once you’ve downloaded it, extract it anywhere (on your desktop or documents will do). Once you’ve extracted it, go to the directory of nginx that you just installed, and type in “cmd” at the directory address as shown below. And the command prompt will show up.
In the command prompt, type in “start nginx”. Once you’ve done that, you check if nginx is working properly by opening you browser and type in “localhost” at the url bar. Nginx works properly if the screen below show up after typing in “localhost”.
Once Nginx works perfectly, now we can configure the Nginx to distribute load to our servers (I’ll be using it locally with Django’s runserver). Pretty simple, go to nginx’ directory, open the “conf” folder as illustrated below.
Then you should see “nginx.conf” file. Open it on any text editor you’re comfortable with (I’ll use Visual Studio Code for now).
It’s contents will by default look like below.
Now, just comment out the http part and it’s contents or delete it. Then replace it with the code below on your “nginx.conf” file. You can configure the contents of the upstream as you like, but I’ll be using Django’s localhost port 8000, 8001, 8002, 8003.
http {
upstream myproject {
server localhost:8000;
server localhost:8001;
server localhost:8002;
server localhost:8003;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://myproject;
}
}
}
Once you’re done, save the file. Now, I want to know which port nginx uses, you can do this by opening your Django project, make another app for just testing nginx or you can just use an existing app. Now in the views.py of the app, create a view method such as below.
from django.http import HttpResponsedef nginx_view(request):
html = "<h1>Using port " + request.META["SERVER_PORT"] + "<h1>"
return HttpResponse(html)
I’ll assume you’ve configured the urls.py so I won’t show it here. What the method does is just show the port it’s using. Now, open a terminal for every Django’s runserver port, so each one has it’s own terminal. And at each terminal run as below.
C:\Users\User>cd <Your django project directory>C:\Users\User\django_project_dir>python manage.py <port_number>
Once you’ve done that, open another terminal to start nginx. Go to the directory where your nginx is located. After that run the code below.
C:\Users\User\nginx-1.16.1>start nginxC:\Users\User\nginx-1.16.1>nginx -s reload
Open your browser, type in the url “localhost”. And the page should look like below.
Now, try removing Django’s runserver port 8000. Then, refresh the page. Load balancer will change it’s port to 8001 automatically even if you use the same url. Cool isn’t it?
If you keep refreshing, it should also change the port to 8002, 8003, and back to 8001 and keeps looping that way.
That’s probably it. You can check out this link for more stuff regarding nginx :