Just keep it simple and break it down

"Just keep it simple and break it down" it is not just a statement, it is a mantra we all should think about first when designing any system.

If an interviewer asks you how will you design whatsapp? Take a pose for a moment and think about how whatsapp works? what is the core functionality of whatsapp?

It is a messaging app, sending and recieving messages is the most important part of it. Lets focus on this part for now and lets not think about how group chats will work? how to manage online/offline status etc? think about plain simple text messages.

Lets list down things

  1. We need one sender who sends the message and one receiver who recieves the messages.
  2. Sender and reciever could be an android app, ios app or a webapp.
  3. We need backend server which receives the messages from the sender and sends it to the receiver.
Do we need anything else? Umm lets think about. Sender and receiver can be online or offline at any point. It might happen that sender sends the message but receiver is not online. so we need a database to temporarly hold the data. Lets update our list.

  1. We need one sender who sends the message and one receiver who recieves the messages.
  2. Sender and reciever could be an android app, ios app or a webapp.
  3. We need backend server which receives the messages from the sender and sends it to the receiver.
  4. A database for temporarily storing the data.
Lets draw our simple design diagram





Next thing to think about is how client is going to communicate with servers? Client can create an http connection with server. Opening a new connectione each time to send a message or request for new messages will be costly. 
To save resources, we need a connection which is long and persistent. For such scenarios we can think of Http long polling or websockets. We will discuss about which type of connection is better in some other post. Go read about them.

Once we are able to satisfy basic requirements, we can think about scaling. As we add more servers, we will need to add load balancer to manage the load between whatsapp servers. 

When you have to quickly access any data, think about cache, think about do we need local cache or global cache? We might want to cache the information about which client is connected to which server to quickly transfer the client request to right server.

Another important component is queue? Think about peak time, when there are lots of requests and our system is not able to handle all the requests at the rate they are coming in. we can introduce queues to queue the requests and servers will pick requests one by one from the queue and process it. 

Where else we can use queues? Whenever you have to think about asynchronous processing or asynchronous communication between systems, queues are most definitely the answer to those questions.

To end this post, I will say do not memorize any system design for the interviews, try to learn about the different tools (cache, load balancer, queue, serverless system, etc) and their applications and utilize these tools to solve any bottleneck in the design. Remember, keep things simple.






















Comments

Post a Comment

Popular posts from this blog

HTTP Long Polling vs WebSockets

Data Structures and System Design Interview Prep Notes