How I Would Design a Chat Application
If you've ever prepared for backend system design interviews, you've probably come across this question:
At first, it sounds intimidating. You start thinking about billions of messages, millions of users, end-to-end encryption, and suddenly it feels like you need to rebuild WhatsApp itself. But here's the reality: interviewers don't expect you to recreate WhatsApp. They want to understand how you think, how you break down a problem, and whether you understand the core building blocks of scalable systems. Let's walk through how I would approach this question in an interview.
First, I'd clarify the requirements
Before drawing any architecture, I'd ask a few questions: 1. Are we building one-to-one chat only, or group chats too? 2. Do we need message delivery and read receipts? 3. Should users appear online/offline? 4. Do messages need to be stored permanently? 5. How many users are we expecting? For this discussion, let's assume we're building a basic WhatsApp-like application with: One-to-one messaging Group chats Online status Message history Read receipts Push notifications
- One-to-one messaging
- Group chats
- Online status
- Message history
- Read receipts
- Push notifications
The biggest challenge: Real-time communication
The first thing that comes to mind is that HTTP alone won't work. Imagine sending a message and refreshing the page every second to check if someone replied. That's obviously not how chat applications work. Instead, we'd use WebSockets. A WebSocket creates a persistent connection between the client and the server, allowing both sides to communicate instantly. The flow becomes simple: User A → WebSocket Server → User B As soon as User A sends a message, the server immediately pushes it to User B without waiting for another request. In Node.js, technologies like Socket.IO or native WebSockets make this relatively straightforward.
But where do we store the messages?
eal-time delivery is great, but messages also need to survive server restarts and network failures.
So every message must eventually be written to a database. A simple message structure could look like this:
{
senderId: "user_1",
receiverId: "user_2",
message: "Hello!",
status: "sent",
createdAt: Date.now(),
updatedAt: Date.now()
}For smaller applications, PostgreSQL or MongoDB work perfectly. At a much larger scale, databases like Cassandra become attractive because they're designed to handle enormous write workloads. The key point is this : Never rely on WebSocket memory for storing chat history. WebSockets handle communication. Databases handle persistence.
How do we know if a user is online?
When someone opens WhatsApp, you immediately see whether they're online. A database isn't a good solution here because online status changes constantly. Instead, I'd use Redis. Whenever a user connects: user:123 -> socket_abc Whenever they disconnect: delete user:123 Now, checking if someone is online becomes a simple Redis lookup, which takes only a few milliseconds.
The first scaling problem
A single WebSocket server works fine until you have thousands of concurrent users. Eventually, you'll need multiple servers:
Load Balancer
|
WS1 WS2 WS3Now we have a new problem. Suppose User A is connected to WS1, but User B is connected to WS3. How does WS1 know where User B is? This is where many candidates stop, but this is exactly what interviewers want you to think about.
Redis Pub/Sub solves this problem
Instead of forcing WebSocket servers to communicate directly, we can introduce Redis Pub/Sub. The architecture now looks like this:
WS1--Redis-- --WS2
|
WS3When User A sends a message: WS1 publishes the event. Redis broadcasts it. WS3 receives it. WS3 delivers it to User B. This keeps all WebSocket servers synchronized while remaining relatively simple.
What if the database becomes slow?
Let's say thousands of users start sending messages simultaneously. If every WebSocket server waits for the database before responding, users will experience delays. Instead, I'd introduce a message queue. The flow becomes: User ↓ WebSocket ↓ RabbitMQ/Kafka ↓ Workers ↓ Database Now the WebSocket server only needs to: Validate the message Push it into the queue Return success immediately Background workers handle database writes asynchronously. This approach dramatically improves performance and reliability.
What about group chats?
Group chats introduce another interesting challenge. Imagine a group with 1,000 members. A naive approach would send 1,000 individual messages directly from the WebSocket server. That quickly becomes expensive. Instead, I'd: Store group membership separately Publish a group event Let background workers distribute messages Something like: Sender ↓ Group Service ↓ Message Queue ↓ Fan-out Workers ↓ Group Members This prevents our WebSocket servers from becoming overloaded.
Handling read receipts
Those famous WhatsApp ticks are actually just state changes. A message can have multiple states: Sent ↓ Delivered ↓ Read Whenever a recipient opens the message, the client emits a "read" event, and the server updates the message status. Simple concept, but an important feature.
What happens when users are offline?
Not everyone stays connected all the time. If a user is offline: Store the message. Trigger the notification service. Send a push notification through Firebase or Apple Push Notifications. Something like: "John sent you a message." Once the user reconnects, the application can fetch any unread messages.
How would I scale this to millions of users?
At very large scale, I'd start introducing: Multiple WebSocket clusters Database sharding Redis clusters Kafka for event streaming CDN for media files Kubernetes for container orchestration Monitoring with Prometheus and Grafana The architecture would evolve over time rather than being built all at once.
Final thoughts
When interviewers ask you to design WhatsApp, they aren't checking whether you've memorized an architecture diagram. They're checking whether you understand questions like: Why do we use WebSockets? Why is Redis useful? When should we introduce RabbitMQ or Kafka? How do we handle offline users? How do we scale from one server to hundreds? If you can explain those trade-offs clearly, you're already demonstrating the skills most companies look for in a backend engineer. And honestly, that's what system design interviews are really about: understanding the "why" behind the architecture, not just drawing boxes and arrows.