Almost everyone has at least one of the popular chat apps installed on their phones. Messaging services like Facebook Messenger, WhatsApp, Discord and Slack allow you to send and receive messages from other users. Different apps have different functions, but there are certain key features that are common in all of them.
Let’s design an instant messaging service that supports one-on-one chat between users through mobile and web interfaces. Depending on your requirements, you may design a system that supports group chats too.
Designing a chat service is a popular interview question asked by companies like Facebook, Amazon and Twitter. As with any system design question, designing a messaging app can be a broad topic so it’s important not to address a single approach and start discussing it from the beginning.
Take a few minutes to explore what the interviewer wants you to design. List down the key features that your messaging app should have and then generate a customized system based on the requirements.
Our messaging app should at the minimum support the following key features:
As of January 2021, WhatsApp supports 2 billion monthly active users according to Statistica. To build a system that can support such a large number of users, there are some additional requirements you will have to incorporate:
Estimating around 10B messages sent each day, with an average size of 140 bytes for each message, we’ll need around 1.4 TB of storage capacity for daily messages.
10 billion messages * 140 bytes = 1.4 terabytes
The above estimation assumes there’s no meta data and a single copy of messages is saved. In actual messaging applications, however, metadata along with other information are also stored. Messages are replicated across multiple caches for scalability and reduced latency.
Keeping our design simple, we’ll assume 1.4 TB of daily message storage. Now, assuming we store chat history for 10 years, we will need:
10 years * 365 days * 1.4 TB ≅ 5 petabytes
Messages aren’t the only data that is stored. In the case of Facebook Messenger, for example, the users’ statuses are also stored. However, these will not need to be saved in the database, just the memory cache.
Let’s assume that Facebook Messenger also has 2 billion monthly active users. Considering the real-world scenario, they won’t all be active at the same time. Let’s assume that half of them, i.e. 1 billion are active at a time.
If each entry takes 1 KB, you’ll need:
1 B * 1 KB = 1 terabyte of memory space in the distributed cache.
For a scalable service such as this one 1 TB of memory cache is reasonably affordable. The messaging service sends heartbeat timestamps to the cache. A user’s online presence will be confirmed by his/her presence in the cache with a recent timestamp.
For the most elementary architecture of the system, assume that there are two users, A and B who want to communicate with each other. In the ideal scenario, where A knows the address of B and B knows the address of A, they can each send information to the other at the known IP address.
Coming closer to the real-world scenario, consider clients A and B are part of a bigger network, which also facilitates millions of other users for the exchange of messages. Now, in such cases, it’s not feasible that the users know the IP addresses of other users. So instead of direct connection between the users, there has to be a server in between to manage the connections.
In a horizontally scaled system, there will be multiple servers that are a part of the messaging system that A and B want to communicate through. If A wants to send a message to B, it will connect to the server that’s free and has the capacity to serve the communication. Here’s where we’ll need a load balancer.
50% off Udemy courses
Grokking the System Design Interview
Java Multithreading for Senior Engineering Interviews
Grokking the Advanced Design Interview
Grokking the Coding Interview: Patterns for Coding Questions
Grokking Dynamic Programming Patterns for Coding Interviews
Coderust: Hacking the Coding Interview