CSE Roadmap

Core tier · about 5 hours · 8 parts

Part 5 of 8 · 2 min read

Message flows

Discovery, one-to-one, devices and groups.

Service discovery. When a user logs in, the API server checks the credentials. Service discovery then picks the best chat server by location and spare capacity, and returns its address. The client connects to it by WebSocket. A coordination tool such as Apache ZooKeeper (Level 5) registers the live chat servers for this job.

One-to-one flow. User A sends to user B.

  1. A sends the message over the WebSocket to chat server 1.
  2. Server 1 asks the ID generator for a message id.
  3. Server 1 puts the message on the message sync queue.
  4. The message is saved in the key-value store.
  5. If B is online, the message is passed to chat server 2, where B is connected. If B is offline, the push notification servers send a push.
  6. Server 2 sends the message to B over B's WebSocket.
A --ws--> chat server 1 --id--> ID generator
              |--> sync queue --> key-value store
              |--> (B online)  chat server 2 --ws--> B
              '--> (B offline) push servers  --push--> B

Several devices. Each device keeps a value called the latest message id it has seen. A message is new for that device if it is addressed to the signed-in user and has a larger id than that value. Each device simply asks the key-value store for newer messages. A worked example (made up): the phone has seen id 500, the laptop id 480, and the store holds up to 520. The phone fetches ids 501 to 520, which is 20 messages. The laptop fetches 481 to 520, which is 40.

Group flow. For a small group, the sender's message is copied into each member's inbox, which is that member's message sync queue. Each client then reads only its own inbox. This is simple, and cheap for small groups. A worked example (made up): a group of 100 gets one message. The system makes 99 copies. At 100 bytes, that is 9.9 KB per message. For groups of thousands, copying to every inbox is too costly, and a different design is needed. The book notes WeChat caps its groups at 500 for this reason.

Inbox (message sync queue)

An inbox is a per-recipient list of messages waiting for that user's devices to read.

Example: a message to a group of 3 puts a copy in each of two other members' inboxes.

Check yourself: 1. What does service discovery return? 2. How does a device know which messages are new? 3. Why is the inbox copy design limited to small groups?