What is Scaling ? Which one is best : vertical Scaling or Horizontal Scaling ?
Intro :
The scalability of an application can be measured by the number of requests it can effectively handle in a given time frame.
When your website grows in popularity and getting more incoming traffic than normal, to support simultaneous amount of request which are expected(and unexpected), we need more resources(CPU, RAM, GPU power) to handle those request.
Vertical Scaling :
One way of tackling is increasing the resource of an existing machine to such extent so it can handle large amounts of requests easily. This technique of scaling is called Vertical Scaling. This kind of scaling is good when you are just getting started because it provides simplicity and low latency. The reason for low latency is
all of components of the website resides in one machine
which makes the API/DB calls much faster as we don't need to perform network calls.But as your incoming traffic increases you have to increase the resources as well, but we have certain hardware limits beyond which we can't upgrade our existing machine. Currently we have 24TiB RAM(which I guess is highest at the time of writing) machine provided by amazon. For more info.. But this high resourced compute machines comes at a very high cost.
Horizontal Scaling :
To resolve the above problems(high cost/hardware limit) instead of putting all the resource into one machine, we are putting multiple machine with normal resources, so that we can handle any amount of request without worrying of hardware limitation. When traffic grows we can simply add more servers(& vice versa) and the website will run smoothly. We can simply achieve this using "autoscaling" feature on AWS.
But the catch with vertical scaling is, we need to implement load balancer which can distribute the traffic equally among all the machines and have to make our web servers(machines) stateless to able to provide responses to user from any machine irrespective of what machine it was logged in first.
What do I mean by stateless ?
If you have build any web application with some authentication or used cookies for storing particular information about the user for a constant time. Typically when you using a cookies for a particular session(user), these details reside with in that machine only. So let's say if a user performs logging in ServerA but due to huge request load balancer redirects the same user to another ServerB after login succed. But this ServerB doesn't have any knowledge about the session created in ServerA. So then ServerB again asks user to login. If you have 'n' machines then in worst case the user have to login in every machine(n) to use the application which is very bad User experience.
To mitigate the above problem stateless approach comes handy.
In this approach when a user logs in or done something, we are keeping track of the whole session in a separate database. (preferably NoSQL for Key-Value and simplicity and low latency). So every time request comes from a particular user, web server first checks the the DB for session details and responds accordingly.
Now after stateless implementation, UserA performs login in ServerA and when login succeeded it stores all the needed details to the database(like
userId:{session_details}
). Then due to high load, load balancer redirects UserA to ServerX. But this time ServerX goes first to DB and look through the session if it finds user logged in then it will redirects user to home/product page.So in that way if we implement Vertical Scaling, each request will be independent of which machines it is was logged in or performed some actions as we keeping global track of the session via DB. So we can add as many machines we want to.
The best possible solution will be mixture of both Vertical & Horizontal Scaling. We can add multiple machine with medium resources to handle incoming traffic with stateless architecture.
Let me know your thoughts on it. Will love to discuss any further addition/feedback about this post.
Ending with a quote :
"Design is not just what it looks like and feels like. Design is how it works." - Steve Jobs