High availability — if the chat system goes down users can't send or receive messages
Low latency — messages should deliver in under 100ms
Scalability — needs to handle billions of messages per day across millions of concurrent users
Durability — no message can ever be lost once sent
Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
Define the APIs expected from the system. This is your chance to analyze and define the read and write paths so that you can come up with the high-level design...
POST /users/register
input: { username, password }
output: { user_id, token }
POST /groups
input: { name, member_ids }
output: { group_id }
POST /groups/:group_id/members
input: { user_id }
output: { confirmation }
DELETE /groups/:group_id/members/:user_id
output: { confirmation }
GET /groups/:group_id
output: { group name, member list }
GET /messages/:chat_id
input: { page }
output: { list of messages in order }
WebSocket /chat
input: { user_id, token }
establishes: persistent connection for sending and receiving messages
Describe the overall system architecture. Identify the main components needed to solve the problem end-to-end. Use the diagramming tool to create a block diagram.
"The system has two separate flows. The websocket flow handles all real-time messaging. The client establishes a persistent websocket connection to a chat server. When a message is sent it goes from the chat server into a Kafka queue. A worker server consumes from Kafka, saves the message to the messages database, then asks the session service which chat server the recipient is currently connected to. The worker pushes the message to that chat server which delivers it instantly through the recipient's websocket. If the recipient is offline the message is stored in an undelivered messages cache and delivered when they reconnect. The HTTP flow handles everything else — creating accounts, creating groups, managing group members. These go through the API gateway, load balancer, and user metadata service which stores data in two separate databases — one for users, one for group membership."
Define the data model. Identify the main entities, their attributes, and relationships. Consider the choice of database type (SQL vs NoSQL) and justify your decision based on access patterns...
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.
"The chat server is the entry and exit point for all real-time communication. Every connected user maintains a persistent websocket connection to a chat server instance. When a user sends a message the chat server immediately drops it into Kafka and responds to the client — the user sees their message sent instantly without waiting for it to be stored or delivered. When an incoming message arrives from the worker, the chat server pushes it through the recipient's websocket to their device. The chat server also registers each user with the session service on connect and deregisters on disconnect.
The session service is a lookup table that maps every active user to the specific chat server they're connected to. It stores this mapping in its own dedicated database. When the worker needs to deliver a message it queries the session service — 'which chat server is user B on?' — gets back a server address and pushes directly to it. For group messages the worker queries the session service for every member of the group and pushes to each of their chat servers simultaneously.
The worker server consumes messages from Kafka and has two jobs. First it writes every message to the messages database permanently. Second it handles delivery — for each message it checks the session service, finds the recipient's chat server, and pushes the message there. If the session service says the user is offline, the worker stores the message in an undelivered cache. When that user reconnects their chat server checks the cache and flushes any pending messages to them immediately."