Design a Chat System
Chat apps are everywhere: Facebook Messenger, WeChat, WhatsApp, Discord, Slack. In this chapter we design one that supports one-on-one and small group chats, works on mobile and web, and shows who's online.
- 12.1Requirements1-on-1 chat with low delivery latency Small group chat (max 100 people) Online presence indicator Multiple devices: the same account on…
- 12.2Polling vs long polling vs WebSocketSending is easy: the client makes an HTTP request to the chat service (with keep-alive to reduce TCP handshakes). Receiving is the hard…
- 12.3High-level designStateless services (behind a load balancer): login, signup, user profile… Plus service discovery, which gives clients a list of chat server…
- 12.4Storage, data models and message IDsGeneric data (profiles, settings, friend lists) goes in robust, reliable relational databases with replication and sharding. Chat history…
- 12.5Service discoveryWhich chat server should a client connect to? Service discovery (Apache Zookeeper is a popular open-source option) registers all available…
- 12.61-on-1 message flowUser A sends a message to chat server 1. Chat server 1 gets a message ID from the ID generator. It sends the message to the message sync…
- 12.7Syncing messages across multiple devicesUser A has a phone and a laptop, both connected over WebSocket. Each device keeps a variable cur_max_message_id: the latest message ID it…
- 12.8Small group chat flowWhen User A sends a message in a group with B and C, the message is copied into each member's message sync queue: one for B and one for C.…
- 12.9Online presencePresence servers manage online status and talk to clients over WebSocket. Login: after the WebSocket is built, A's status and…
- 12.10Wrap upWe designed a chat system with WebSockets for real-time messaging, stateless API servers, a KV store for history, service discovery,…