Horizontal Vs Vertical Scaling & When Should We Use Either
Tags: system, design, devops
Published: 2022-02-02 11:10:18

Before we dive deeper into this subject. Let's look at how servers work.

![Request Response Pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5q6s9ls9jte3q6auo6tp.png)

We **Request** the server for a certain resource and in the server we query a database for the specific resource and get a **Response**. This architecture works fine where we have minimal users but what happens when we have a lot of users. We will find that our server resources don't have enough capacity to handle the increasing number of requests , this calls for an increase in capacity.

We have two options, to scale vertically or horizontally.

## Vertical Scaling
Simply put, vertical scaling is when we buy bigger servers. We increase the size of ram, CPUs & memory to one machine.

![Vertical Scaling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3vmhtnv6i327qikjac6.PNG)

## Horizontal Scaling
In horizontal scaling we increase the number of machines in our servers and distribute requests across the different machines. Here there exists a master-slave architecture where there is a master that has read and write capabilities and there are slaves where data is replicated to. Slaves have read only capability.

![Horizontal Scaling](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/memkzifmhg48hkf9x5y6.PNG)

The master node serves as the keeper of information. The true data is kept at the master machine, thus writing only occurs there. Reading, on the other hand, is only done in the slave. In case the master node fails , a slave node is chosen as a master while waiting for the master to recover. On recovery the master may be a master node or continue as a slave node.

##What is this for? 

This architecture serves the purpose of safeguarding site reliability. If a site receives a lot of traffic and the only available database is one master, it will be overloaded with reading and writing requests. Making the entire system slow for everyone on the site.

## What are the pros and cons?

For **Horizontal scaling**, **load balancing** is required while in **Vertical scaling** we dont need a load balancer. _A load balancer acts as the “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests in a manner that maximizes speed and capacity utilization and ensures that no one server is overworked_ 

**Horizontal Scaling** is resilient while **Vertical scaling** has a __single point of failure__ Case in example if we have a server failure in a vertical scaling setup we lose our server but in a horizontal setup the process are distributed to the other machines.

**Horizontal Scaling** uses network calls to communicate with different machines while **Vertical scaling** uses inter-process communication as its one big machine. Communication in A Horizontal setup is slow due to its I/O nature while communication in a vertical setup is much faster.

In **Horizontal Scaling** data consistency is a challenge as it distributed between different machines while in **Vertical scaling** its consistent as its one machine.

**Horizontal Scaling** scales well when users increase while **Vertical scaling** has hardware limits.

## When should you use Horizontal or Vertical scaling ?

Horizontal scaling is preferred when scaling with many users and where resilience is needed. Such that when one server goes down there are other backups.

Vertical scaling is preferred where data consistency is highly needed and the number of users haven't outgrew the current hardware limit

Thanks for reading.Please follow me on twitter @zeelearner