The article initially was published here https://www.linkedin.com/pulse/journey-system-design-interview-jahid-hasan-ef9lc/
Recently, I was talking with a friend about his interview question, and I found one interesting question which i wanted to answer too. I thought it was easy-peasy but I was wrong. Let's deep dive into the question and the discussion.
The initial question was simple, but it goes into depth as normally happens in system design interview. At first, he was asked to "Design a simple system that allows users to read products from a product database." He inquired me about the number of users, types of data, and other details, but the interviewer told me to skip those for now and design for a minimal number of users with simple raw text data.
So, we simply answered that user will call ```GET /products``` and server will request the products from the database server and respond with them. Below is a diagram that is illustrating the design.
Simple System Diagram
The interviewer then added some constraints, this time there would be many more requests per minute to the server. What should we do now?
In this scenario, reading data directly from the database server would be too time-consuming, especially with so many more requests per minute. Therefore, he introduced caching to improve performance. He added a cache layer to the current system design, as illustrated in the updated diagram below.
System with Caching
Next, the interviewer added another constraint: the product data includes images. The candidate inquired about the image size and quantity, and the interviewer pointed out that the images might be too large to store in the database server or the cache.
To solve this problem, the candidate suggested using a Content Delivery Network (CDN) server. This approach would handle the large image files efficiently by offloading them from the database and cache, providing faster and more scalable access to users.
So, the final solution looked like this:
The server will first check for the product data in the cache.
If the data is not found in the cache, the server will fetch it from the database server.
Once retrieved, the server will update the cache with this data.
If the product data includes images, the system will retrieve those images from the CDN server.
The updated system design is illustrated in the diagram below.
Then The interviewer mentioned that the text-based data changes frequently and is updated by different servers. Also the number of requests per minute has increased significantly, and it will result a high volume of server requests. (He indicated that caching is not allowed, so you need to redesign the whole system.)
At this point, we were stuck and unsure of what to do next. Though I have been studying system design books and articles and found solutions for these kinds of problems. They seem straightforward, It's puzzling why they didn't come to mind when needed.
In this scenario, optimizing our database servers won't be sufficient. We need to add more slave database servers. These slave servers will be read-only and will help distribute the load.
Our system will use round-robin or similar load-balancing algorithm to divide the incoming read/write requests to multiple slave servers. This approach ensures that the requests are evenly distributed among all servers, and it will improve response times and will reduce the load on any single server.
We will scale our database horizontally. This approach will distribute data and workload across multiple servers, managing the increased number of request and frequent updates. The solution involves adding more database servers and distributing requests among All the slave Database has products DB's actual copy.
The updated system design is illustrated in the diagram below.
our infrastructure is ready
Furthermore, we can optimize more with horizontal scaling. For example, we can add more CDN servers to handle image delivery more effectivly. Before I didn't imagined that using separate load balancers for database servers. I used to think that load balancers were applicable at the gateway level only, but I was wrong. Load balancers can deployed anywhere they are needed in the system to distribute the workload effectively.
