Use Case: Horizontal Scaling vs Vertical Scaling with Nginx

When deploying high traffic sites/web apps, often times you’ll be faced with a dilemma on whether to use horizontal scaling or vertical scaling. Horizontal scaling is the use of several smaller servers sitting behind a load balancer, to deliver a web application to users. Vertical scaling is the use of a bigger server to deliver an application to users. There are times when vertical scaling is the better way to go, but at some point when your site traffic is very high, vertical scaling should no longer be used. This article tries to explain the use of a Nginx software load balancer to show you why horizontal scaling is the better way, for high traffic web applications.

We run a simple Wordpress site as our web app for demonstration purposes.

Case for Horizontal Scaling with a Nginx Load Balancer

Environment setup

The servers used are as follows:

Server Quantity Specs Hostname and IP Other details
MySQL Database server 1 1GB RAM, 1 Core [email protected], 10GB HDD Hostname: dbserver IP: 192.168.100.9 Centos7.6; phpMyAdmin; MySQL5.7; Apache2.4.6
Apache Web server 3 Each 1GB RAM, 1 Core [email protected], 10GB HDD Hostname: webserver1 IP: 192.168.100.5 Hostname: webserver2 IP: 192.168.100.10 Hostname: webserver3 IP:192.168.100.11 Centos7.6; Apache2.4.6; PHP7.3
Load Balancer 1 1GB RAM, 1 Core [email protected], 10GB HDD Hostname: loadbalancer IP: 192.168.100.4 Debian10; PHP7.3; Nginx
Client 1 1GB RAM, 1 Core [email protected], 10GB HDD Hostname: client IP: 192.168.100.12 Centos7.6; Apache Benchmarking utility (ab)

Methodology

For the setup environment to work well, the following steps were taken:

  • On the database server, I installed MySQL5.7. Then to be able to quickly configure database and permissions, I installed php7.3 and phpMyAdmin so I can manage users via GUI. This was accessed externally as the database server was a minimal installation of Centos – hence had no GUI. I used a browser on another node to access phpmyadmin, create the database for wordpress site, the user and grant needed permissions. To succeed in this, you will need to allow remote access to the databse server via /etc/my.cnf file and in /etc/httpd/conf.d/phpMyAdmin.conf files.
  • On the Web servers, I installed Apache2.4.6, PHP7.3 and all php modules needed for wordpress to run and deployed wordpress files onto /var/www/html folder. You’ll need to also disable the default Apache landing page by removing /etc/httpd/conf.d/welcome.conf
  • On the Load balancer, I installed Nginx and configured load balancing on /etc/nginx/conf.d/lb.conf as per this guide
  • On client computer – I installed Apache and Apache bench marking utility. I also configured a custom domain, app.local and pointed it to the load balancers’ IP via /etc/hosts
  • Then, I turned off the firewalld and selinux for the setup to work.
  • Finally, I logged in to client server and run the command below and made observations

ab -n 10000 -c 10 http://app.local/

Observations

  • The database server did not experience high loads even at peak traffic. Load remained below 0.15
  • For web servers, the test completed after 904.476 seconds – representing 90.4ms per request or 11.05 req/sec. Maximum load for any web server went to 2.5 during the test. Fastest request was served at 189ms and slowest at 3.162seconds
  • The load balancer experienced no load issue at all with load maintaining below 0.1 althrough.

Case for Vertical Scaling with Load Balancer

Envrionment setup

The servers used are as follows:

Server Quantity Specs Hostname and IP Other details
MySQL Database server 1 1GB RAM, 1 Core [email protected], 10GB HDD Hostname: dbserver IP: 192.168.100.9 Centos7.6;phpMyAdmin; MySQL5.7; Apache2.4.6
Apache Web server 1 4GB RAM, 2 Core [email protected], 10GB HDD Hostname: webserver1 IP: 192.168.100.5
Centos7.6; Apache2.4.6; PHP7.3
Load Balancer 1 1GB RAM, 1 Core [email protected], 10GB HDD Hostname: loadbalancer IP: 192.168.100.4 Debian10; PHP7.3; Nginx
Client 1 1GB RAM, 1 Core [email protected], 10GB HDD Hostname: client IP: 192.168.100.12 Centos7.6; Apache Benchmarking utility (ab)

Methodology

The same methodology as the one above was used only that this time, I deployed only 1 web server that is bigger in resource compared to each individual one in the other setup.

Observations

  • The database server still did not experience high loads even at peak traffic. Load remained below 0.15
  • For web servers, the test completed after 978.940 seconds – representing 97.89ms per request or 10.22req/sec. Maximum load for the web server went to 10.5 during the test. Fastest request was served at 199ms and slowest at 4.541seconds
  • The load balancer experienced no load issue at all with load maintaining at 0.1

Cost analysis of each setup.

I will use prices for Digital Ocean servers to get the sum we would incur for each setup. This is because Digital Ocean is a global brand recognized for their VPS solutions. We will check only the RAM and CPU when comparing similar VPSs.

Note: The Load balancer is a software load balancer created using Nginx Open Source. It is not a hardware load balancer.

Server Horizontal Scaling Vertical Scaling
Database server $5 $5
Web server(s) $15 $20
Load balancer $5 $5
TOTAL $ per month $25 $30

Conclusions

From these, we’ll be able to deduce the following:

  • Horizontal Scaling provides a faster application delivery than vertical scaling as seen on the average time per request.
  • Horizontal Scaling is likely to provide better user experience as the server loads are lower, meaning application performs better for the user.
  • Horizontal Scaling is cheaper than vertical scaling.

Note: For the second setup, you may choose to forego the load balancer as there’s only one backend server. This would bring the total cost of deployment for each case to $25 dollars. However, the case for horizontal scaling still gets an upper hand in that:

  • in the second setup, the load experienced by the single web server is 5x the recommended load for the server. The load experienced by the independent web servers in first setup is at most 2.5 times the recommended load for each server.

The load of a server should be at most equal to the number of CPU cores the server has. Eg a 1 core CPU server should have a maximum load of 1.

There are some situations where vertical scaling works well, but in most very busy web applications, horizontal scaling is the faster, cheaper and more reliable way to go. Even if Vertical Scaling would match horizontal scaling setup on cost and up-time, the best aspects of horizontal scaling are: that it provides redundancy and allows flexibility without downtime. These ensure a nearly 100% up-time guarantee, saving your business face which above all, is priceless.

Leave a Reply

Your email address will not be published.