FAANG System Design Interview: Design A Chat System (WhatsApp, Facebook Messenger, Discord, Slack)
Summary
The article describes designing a chat system (like WhatsApp, Facebook Messenger, Discord, Slack) capable of handling real-time messaging for millions of users, supporting one-on-one and group chats up to 100 participants, guaranteeing message delivery, and showing online presence. It proposes a hybrid client-server communication approach using HTTP for sending messages and WebSockets for instant receiving. The system architecture is divided into three layers: stateless services (authentication, user profiles, message sending via REST APIs), stateful chat servers (handling persistent WebSocket connections), and third-party integrations (push notifications via APNS/FCM for offline users). Key mechanisms include the inbox pattern for guaranteed offline message delivery, service discovery and direct RPC for message routing between chat servers, and a fan-out pattern for group chats. Online presence is managed using 30-second WebSocket ping heartbeats, marking users offline after 60 seconds of no activity. Scaling challenges include connection bottlenecks, database limits, and cross-region routing complexity.
Key takeaway
For software engineers designing real-time chat applications, prioritizing a hybrid communication model with HTTP for sending and WebSockets for receiving is essential for balancing scalability and instant delivery. Implement an inbox pattern to guarantee message delivery for offline users, ensuring reliability. You should also integrate third-party push notification services like APNS or FCM to reach disconnected devices efficiently. For online presence, use 30-second WebSocket heartbeats, marking users offline after 60 seconds of inactivity to manage connection state effectively.
Key insights
Designing a scalable chat system requires a hybrid communication model, layered architecture, and robust mechanisms for message delivery and presence.
Principles
- Hybrid communication (HTTP for sending, WebSocket for receiving) optimizes for both scalability and real-time delivery.
- Layered architecture (stateless, stateful, third-party) simplifies scaling and leverages specialized services.
- Inbox pattern ensures reliable message delivery for offline users.
Method
The system design involves a hybrid HTTP/WebSocket communication, a three-layer architecture (stateless, stateful chat servers, third-party push notifications), an inbox pattern for guaranteed delivery, service discovery for message routing, and heartbeats for presence tracking.
In practice
- Use WebSocket pings every 30 seconds to track online presence, marking offline after 60 seconds of no activity.
- Implement an inbox pattern to store messages for offline users, retrying delivery until acknowledged.
Topics
- System Design
- Chat System Architecture
- WebSockets
- Message Delivery
- Online Presence
- Distributed Systems
Best for: Software Engineer, CTO, Consultant
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by ByteByteGo.