Understanding the scene
You have written some piece of code which is running on your machine .Some people find the code very useful and are ready to pay you money for using that code. Now obviously you cannot give out your machine to everyone so what you do is that you host that code on the internet using an API(Application Programming Interface) of your backend server running on your machine. Simply speaking your backend server hosts an API to which whenever a request is made , your code runs and generates some output which is sent as response to the request-maker(basically your client machine).
Now there are some problems which you might face:
your machine stops working
there is a power cut
Now obviously you will need to tackle them since there are people who are paying you so one solution is upgrade to cloud service which is going to host your backend obviously in exchange for some money (sometimes throwing money at the problem is the best solution , isn't it? Not for ISRO scientists tho).
Now there are other problems:
- your service's popularity grows and there are a lot of clients in the market which are making requests to your server and the server is not able to keep up
In this case, you throw more money and either buy:
a bigger machine , or
a lot of machines
Buying a bigger machine means solving a request faster and buying more machines means that there are lot of machines to divide the calls into .Both of the solutions lead to scalability-ability to handle more requests .Bigger machine corresponds to vertical scaling while more machines correspond to horizontal scaling.
Horizontal Scaling VS Vertical Scaling
HS=Horizontal Scaling , VS=Vertical Scaling
Load balancing: HS requried load balancing to route the requests to the respective servers but for vertical scaling there is no such requirement as such.
Resilience: HS is more resilient since there are other machines in case one stops working but for VS there is only one machine and hence has a single point of failure
Communication: In HS since there are more machines , data communication between them is quite complicated and relies on Network calls or Remote Procedure Calls(RPCs) which has loose transactional guarantee or basically it is slow whereas in VS we have Inter process communication which is very fast since it occurs within a single machine.
Consistency: In HS as mentioned in the above point , data communciation is quite complicated or inconsistent whereas data transfer in VS is consistent.
Hardware Limitations: In HS , due to use of multiple machines , it scales well as users increase since we can simply increase the number of the machines but for VS , there is a limit upto which the machine size can be increased.
Final Verdict
So in a real world scenario which one of the these two is used? Well ,both of them. Both of them have their own pros and cons where VS has faster communication and better data consistency whereas HS is scalable and resilient. And thats what System Design is about where we will need to understand the tradeoffs and make the best out of our available resources.
Wrapping Up
Thanks for reading till this point and I will meet you in my next article where we would discuss "Monoliths vs Microservices"!