System Design Basics: Horizontal vs Vertical Scaling
Tags: design, architecture, codenewbie, microservices
Published: 2020-04-23 07:27:39

## Let's understand this concept through a story:

**Mughal** *accidentally* made a script and it is running on a local computer, he writes some documentation and makes it public for people to look at.

Now some folks find this functionality very useful and they contact him and tells him that they'll pay for this scripts usage.

So, Mughal Makes an **API** and exposes the script functionality to them, but it's still running on a computer that he has and he can't keep it running forever, there might be a power outage or it shuts down accidentally, that's quite a problem.

He decides to host/deploy his script on a cloud, which is essentially another computer somewhere, but all the maintenance is provided and you just need to login and do your thing, your immediate concerns are taken care of.

**Now**, the usage of your scripts functionality is made popular and many other people also want to implement its usage, but more the implementation, more the traffic and more the computational power required to handle those requests sent to the API.

So, He got 2 options:
**1:** Buy Bigger machine or make computational power of existing machine strong.
**2:** Buy more machines with the somewhat same computational power, so the requests can be distributed among them.


Here, **Option 1** is called **Vertical Scaling**, which means that you're upgrading your existing machine with regards to your need, while **Option 2** is called **Horizontal Scaling**.

Now, let's compare the **Horizontal** and **Vertical** Scaling:

**1:** In case of **Horizontal Scaling**, you need an **_Load Balancer_** to manage the request pouring in and have them consistent with every machine. While it is not required in the **Vertical Scaling**.

**2:** **Horizontal Scaling** is **_Data Resilient_**, which means that if one machine is powered off(let's say), other machines can handle its requests. While that is not the case in the **Vertical Scaling**, there is only a single point of failure.

**3:** We have many machines involved in **Horizontal Scaling**, so the communication between them is done through the **_Network calls_**, which makes it a little slow, while on the other side we have a single machine and it uses **_Inter Process Communication_**, which is quite fast.

**4:** Let's say in **Horizontal Scaling**, machines are communicating with each other and tasks might be dependent, like **machine 1** sends data to **machine 2**, then **machine 2** sends it to **machine 4**, here, you can see that data is complicated to maintain, so if this kind of transaction is happening, what happens is that we have to lock all the databases, which is impractical and because of which we have this issue of **_loose transactional guarantee_**, and this is called **_Data Inconsistency_**, which is quite a big issue. While it's not the case in **Vertical Scaling**.

**5:** Very important point, there's gonna come a point when you just can't upgrade your existing computer, it's the **Hardware Limit**, In this scenario, **Horizontal Scaling** Scales well as the number of Users increases.

## The Question is: What is being used in the real world?
The **Answer** is **_Both_**, What happens is that we start with **Horizontal Scaling** and **Vertical Scale** Every Machine as possible, and we take Strong points from both types, 

We take:
Resilience and User Scaling from **Horizontal Scaling**
Inter Process Communication and Data Consistency from **Vertical Scaling**