Concepts For Your System Design Interview
It was a casual Sunday afternoon. Grandma, as usual, was resting in her reclining chair while indulging in her favorite homemade cake.
For years, Mary has been our friendly provider of neighborhood cookies. Although her cookies were a delicacy, they were the reason behind Grandma’s deteriorating health.
For the ninth time, my grandmother tried to spark a conversation with me. As always, he started with,
“Grandson, what do you do as a software engineer at an e-commerce company?”
“Are you the one who introduces the products?”
“Do you know first-hand about the latest promotional cookie offers?”
I peeked at my watch and mumbled
“Okay, I have some time today, let’s finish this question once and for all.”
I left the book I had been gently devouring, and gradually met a glance at my grandmother
“Grandma, let me tell you a story. The title begins with…”
Internet in a nutshell.
Remember the location
famousamos.com The one you browse nearly every day?
How do you see these web pages?
Well, they are basically files written by engineers. Your browser reads these files and makes them attractive web pages.
The question that will come next will be, given the huge amount of web content, how does your browser store all these files?
It would simply be too much or borderline impossible.
Hence, when you place your thumb on the browse button, your browser requests these files from a remote computer over the Internet.
You are the client, and the remote computers are the servers. This is what we call the client-server model.
Do you ever remember that you made your cookie request to Marie?
What if everyone presented their order differently? Some write her an email, some send her, and some simply shout over the fence.
It will be messy.
Hence, Marie standardized the format, or you could call it a protocol. To order cookies, you need to fill out a certain form and send it via mailbox.
HTTP works similarly – a protocol or set of rules for the browser to send a request.
When your browser sends an HTTP request, it complies with the predefined HTTP rules. It includes having a method, a path, a protocol version, a header, and a request body.
Once you fill out the form, you need an address before submitting, right?
Similarly, to send a request to a server, your browser needs to specify the address of the server – file IP. that it 128 bits It is similar to this
If you remember, Mary lives in a big house. They receive a lot of messages every day. At the top of the address you will also need to indicate the recipient – Mary.
A server is just another computer running many applications simultaneously, such as chrome, paint, etc. Each of these applications carries a port number.
Hence, when sending a request, you must specify the IP address of the server and the port number of the application you are sending to.
Grandma: “Okay, but I’ve never seen an IP address in my whole life.”
you are right. It is very difficult to memorize these cryptic-looking terms. This is where our life saver – DNS turn comes.
DNS It is a server that acts as a phone book. It stores all the corresponding IP addresses and port numbers of websites.
www.famousamos.com, your browser gets the IP address and server port number from DNS and sends the request to the IP address and port number when it receives them.
This is how your browser communicates with a remote server!
Now, if you’ll excuse me, can I have one of these cookies?
As business improves, Marie accepts more orders. Likewise, the server receives a lot of requests every day.
How does Mary deal with these increased demands?
She can work more hours, days and nights.
This is known as vertical scaling.
There is a limit to the number of simultaneous requests a computer can handle. Scaling vertically means adding more storage, RAM, and CPU power to the server so that it can do more work and handle more requests.
As you can tell, this is not sustainable as the business continues to grow. There is a limit to how “scale” Mary can be.
Luckily , Horizontal scaling come to the rescue.
Instead of expanding, Marie can expand horizontally by hiring more workers! Each of these workers will work together to process more orders.
Likewise we can Hire more servers To handle more incoming requests simultaneously, this is also known as Horizontal scaling.
Grandma: “If there are more servers, which server does my order go to?”
The million dollar question is, when an order arrives, how does Mary decide who to turn over the order for it Everyone gets their fair share of work?
She can either
- randomly distributed
- Round Robin
- Distribution based on workers’ ability (based on weight, give them more if they can handle more)
- Distribution based on cookie flavor (by path)
- Distribution based on sender (user segmentation)
In this case, Mary serves as load balancer.
Oftentimes, your request will first arrive at a server that acts as a load balancer. The load balancer will then route the request to a server depending on the algorithm.
A load balancer plays a vital role in preventing over-float and under-exploitation of servers. Ensures that every server Get the right amount of work.
Having lived in the neighborhood for so long, you must realize that he, Mary and Parker, do not get along very well.
Grandma: “Yeah… Parker was planning to sabotage Mary’s business”
Yes. One potential attack that Parker can do is to send a lot of invalid requests with the wrong flavor. Although workers can easily identify them, it exhausts their bandwidth.
They will be busy checking these requests and won’t have time to process normal requests. Customers will eventually leave due to the long waiting time.
This is known as Denial of Service (DOS) attack.
The attacker sends a large number of empty or invalid simultaneous requests to the server. will be the server hoarded In the end it is dropped.
rate setting It is the antidote. If a user sends multiple requests in a file short period of timeMary simply scattered them without going to the workers.
Servers can determine the rate by checking if the user is sending a lot of requests in a short period. Alternatively, this can be done by a load balancer.
In case you forgot, Mary needs someone to bake cookies.
Of course, she can leave it up to the workers. They can enter the order, rush to the kitchen, bake cookies, deliver them and rush to their seat.
That’s a lot of work! and what? Have a suggestion?
Yes you are right. We can Separation of responsibilities By hiring one person for each role.
Every single one of them does One thing and does it well.
And that’s what Microservices be!
For example, an e-commerce application often has a dedicated server for each of these functions
- Handling authentication
- Payment performance
- Delivery notification
The server that accepts your request just does basic treatment. Then it routes your request to the respective server to perform a file specific task.
While the orders are piling up, Marie orders these operations to be done At the same time When the order arrives
- The person begins to knead the dough
- The person is heating the oven
- Someone prepares the package
This means that the operator who manages the order must do Notify everyone! He’ll need to run to make sure everyone gets the message.
To add things to the swamp, the worker needs to Track who notifies As more concurrent operations are added.
As a skilled businesswoman, Mary added a whiteboard.
The worker adds a sticky note to the whiteboard when an order is made. Interested parties, simply Monitor and retrieve data from the blackboard.
This is known as pub-sub . system Also known as message queue. It is necessary in communication between microservices.
For example, when a user checks out an item, they must notify the checkout server
- Ask the server to create a new request
- Inventory server to reduce inventory
- Warehouse server to ship the item
All this must be done At the same time.
With sub public server, exit server no need Notify anyone. It simply adds the event to the pub-sub server, while interested parties subscribe to it and are notified accordingly.
Wait there and we’ll be done soon!
Well Grandma, I’m sure you’ve pre-ordered some cookies for the next few months. For these advanced orders, Mary needs to store them somewhere.
Fortunately, she bought some racks and stored all these logs structurally on them.
These shelves are very similar to Databases – permanent storage for servers persist data and information.
There are a lot of technical aspects when it comes to databases. Since it’s almost time for our naps, I’ll skip this now.
Just as Marie and her team store all of the order records on the shelves to keep track of, a new bottleneck has emerged.
When an order arrives, the front line worker will need it
- Order processing
- Add a note afterwards to the whiteboard
- Rush to the shelves and include them in the records
Grandma: “Lots of people will be running around and crowding the shelves”
exactly! The time lost running the shelves can be used to process more orders. On top of that, cluttering the shelves will only slow things down further.
In this case, a laptop may come in handy. When making an order, write it down in a notebook and update only the shelves once in a while.
Databases store data in a file Hard Disk And it takes time to recover. Constantly querying databases will cache the database server and slow things down.
This is where caching begins. It stores data on Server memory (RAM). It is faster but also volatile. If the server goes down, you will lose data.
Grandma: “What if someone ran away with the notebook?”
This is a smart question! Yes, caching helps speed things up, but it comes with challenges as well,
- If the server goes down, you will lose data
- Data will become obsolete if it is updated either in the database or only in the cache
- Cache may consume a lot of memories if cache clearing policy is not appropriate
Hence, a lot of considerations and efforts are put into when it comes to designing a cache.
Pardon! Looks like we ran out of cookies! Well, I guess it’s time to call it a day.
With more in-depth study, I think Grandma will win the system design interview in no time!
Now, if you’ll excuse me, I need a nap before serving some cookie promotion again!