System Design - Scale 0 to Millions of Users
Supporting Millions of users is a challenging task, it requires understanding distributed Systems and concepts.
What does a traditional system look alike?
Web/Mobile Client sends a request to the WebServer, which retrieves data from the database and returns it to the client.
In this scenario what If
WebServer Fails
WebServer Overloaded by Requests
Database Fails
Database Overloaded
Let’s fix the first Two Problems by using Redundancy, few things we can solve in a distributed system by using Increasing Resources.
Increase Resources called Scaling in Distributed Systems, Typically Distributed Systems have Two types :
Vertical Scaling
Horizontal Scaling
Please refer to the image above. When new resources, such as RAM or CPU, are added to the existing system to meet expectations, it is known as vertical scaling.
On the other hand, when new server racks are added to the existing system to meet higher performance, it is known as horizontal scaling. If your application is expected to handle more traffic and needs high availability, then horizontal scaling is recommended, as shown in the diagram below.
To distribute the request traffic among web servers, we use a load balancer. I will explain load balancers and the techniques used by them in more detail.
Our Updated Design after using Scaling :
Now we can fix the problem if the Server has too many requests or if any server fails we have other servers to bear the load but still our Database is the single point of failure.
Let’s fix this issue, I have used Master-Slave Architecture to address this issue.
Master-slave replication, also known as active/passive replication, is a technique used in databases to keep multiple copies of the same data synchronized across different servers. This enhances system resilience and facilitates efficient data distribution across multiple nodes in a distributed database environment.
In the above diagram as you can see, the Master Database is used for only write operations, and all the read operations are diverted towards the Slave Database aka Read Replicas.
In most of the Systems Read Heavy operations where write operations are less. If the Master Database fails then in case one of the Slave Databases gets promoted and becomes the Master Database.
End Result :
High Performance:
In the master-slave model, all writes and updates occur in the master nodes, while read operations are spread across the slave nodes. This model enhances performance by enabling parallel processing of more queries.
Reliability:
Even if one of your database servers is destroyed by a natural disaster, such as a typhoon or an earthquake, your data remains preserved. You don't need to worry about data loss because the data is replicated across multiple locations.
High availability:
By replicating data across different locations, your website can continue operating even if a database is offline, as you can access data stored in another database server.