Steps for Developing
5 tips to solve any System Design question
Let's start with a few best practices to help you structure your answer. This will also help you avoid mistakes that trip up even the most skilled devs.
1) State your assumptions
Start by listing all required features of the system, common problems you expect to encounter with this sort of system, and the traffic you expect the system to handle.
The listing process lets the interviewer see your planning skills and correct any possible misunderstandings before you begin the solution.
2) Ask your interviewer follow-up questions
Most System Design questions are purposefully vague. Ask clarifying questions to hone in on the requirements and demonstrate your knowledge of the system’s needs.
3) Narrate trade-offs
Every design choice matters. There is never a "perfect" solution that optimizes for every requirement. At each decision point, list at least one positive and one negative effect of that choice.
4) Discuss emerging technologies
Conclude each question with an overview of how and where the system could benefit from machine learning, or other emerging tech. This will demonstrate that you’re not just prepared for current solutions but future contingencies as well.
5) Study your architectures
Most modern services are built upon a flexible microservice architecture. Unlike the monolithic architectures of tech companies in the past, microservices allow smaller, agile teams to build independently from the larger system.
Some older companies will have legacy systems. However, microservices can function in parallel to legacy code and help refresh the company architecture as a whole.
Case Study: Design a Messaging Service (e.g. WhatsApp)
Let's put these tips to the test in a real-world case study.
Variations of this question are very popular across the industry. The specific wording may vary (e.g. "Design WhatsApp" or "Design Facebook Messenger"), but the objective is the same — you will have to design a messaging service, which will have a unique set of characteristics and requirements.
Essentially, you will design a service that allows users to chat with each other over the internet. Conversations can be one-on-one or can be group chats with many members. Messages should only be accessible by those included in the conversation.
A good place to start with any System Design Interview question is to review functional and non-functional requirements.
(NOTE: You can find a much more detailed, step-by-step breakdown of Design WhatsApp in our popular course: Grokking Modern System Design Interview for Engineers & Managers).
Requirements
Functional requirements
Online: Messages must be sent and received via the internet
Multi-functional: Service must support one-on-one and group chats
Archived: Messages should be stored for later viewing
File agnostic: Users should be able to send pictures and videos as well as text messages
Protected: Messages should be encrypted during transit
Non-functional requirements
Consistency: Above all else, the system should be consistent; users should see the same chat log on all devices.
Availability: High availability is desirable, but not as crucial as consistency.
Low latency: The system should be extremely low latency, offering a real-time chatting experience.
High-level Design
The high-level design for a messenger application is fairly simple. A user contacts a chat server and sends a message. The chat server relays the message to the user it was intended for, and then stores the message in a database. Once the message is delivered, the server notifies the sender that the message was successfully transmitted.
The following steps describe the communication between both clients:
User A and user B create a communication channel with the chat server.
User A sends a message to the chat server.
Upon receiving the message, the chat server acknowledges back to user A.
The chat server sends the message to user B and stores the message in the database if the receiver’s status is offline.
User B sends an acknowledgment to the chat server.
The chat server notifies user A that the message has been successfully delivered.
When user B reads the message, the application notifies the chat server.
The chat server notifies user A that user B has read the message.
WhatsApp APIs
WhatsApp in particular offers a large quantity of features via APIs. It is a good idea to brush up on essential APIs calls, including:
Send message
Get message
Upload a media file or document
Download a media file or document
Send location
Send contact
Create a status
(For a detailed breakdown of WhatsApp's high-level and API design, check out our course: Grokking Modern System Design Interview for Engineers & Managers)
Key Tradeoffs
In every System Design problem, there are important trade-offs that you will have to make and justify to your interviewer. There are two particular tradeoffs you will want to account for in this particular design:
1) Consistency vs. Availability
According to CAP theorem, a system can either ensure consistency or availability during network disruptions. For an application like WhatsApp, where the sequence of messages is vital, consistency must take precedence over availability.
2) Latency vs. Security
While users value a real-time experience through low latency, security remains paramount, especially for platforms like WhatsApp. The dilemma arises when attempting to securely transmit messages, especially multimedia. Encrypting such data can introduce significant delays (as depicted in the illustration below), necessitating an intentional balance between quick responses and robust security measures.
Detailed Design Questions to Prepare For
What happens if a message is sent without an internet connection? Is it sent when the connection is restored?
How will you encrypt and decrypt the message without increasing latency?
How do users receive notifications?
Are messages pulled from the device (server periodically prompts the devices if they’re waiting to send a message) or are pushed to the server (device prompts the server that it has a message to send)?
Tools & Strategies to Consider
Split the database schema into multiple tables: user table (with the user ID and contacts), a chat table (with chat IDs and a list of participating user IDs), and message table (with past messages a reference to the chat ID).
Use WebSocket for bi-directional connections between device and server.
Use Push notifications to notify members even if they’re offline.
Grokking Modern System Design Interviews
I hope this quick case study was helpful!
For a much more in-depth breakdown of Design WhatsApp, along with 12 other popular design questions (including Design Uber, YouTube, and Facebook Newsfeed), I highly recommend our best-selling course: Grokking Modern System Design Interview for Engineers & Managers.
You'll learn System Design fundamentals that will set you up for success in your interviews and beyond. Give it a try and let me know what you think.