AI Hints
Home›System Design Tutorial›Case Study: Chat and Feeds
System Design · Lesson 97 of 120

Chat App: Delivery

Online and offline.

⏱ 2 min read▶ Video at 1:57:29🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Every online user keeps a WebSocket open to a chat server. With millions online, there are hundreds of chat servers. So when Sam messages Priya, how does the message find Priya's connection?

Chat App: Delivery
From the video: Chat App: Delivery
  • WebSockets to chat servers
  • Hundreds of servers
  • Find Priya's connection

Sending a message

Sam's app sends the message over its WebSocket to chat server one. The server saves it to the message store first, so it's never lost, and tells Sam it's sent. Then it looks up Priya in a presence service, which knows that Priya is connected to chat server seven. Server seven pushes the message down Priya's WebSocket.

  1. Sam's WebSocket
  2. Save message
  3. Sent ✓
  4. Find Priya's server
  5. Push to Priya
Sending a message
From the video: Sending a message

Online vs offline

If Priya is online, the message arrives in milliseconds, and her app sends back a delivered receipt. If she's offline, the message waits in the store. A push notification is sent to her phone. And when her app reconnects, it asks for every message after the last one it has, and catches up.

Priya online

  • Pushed instantlyDelivered receiptThen read receipt

Priya offline

  • Waits in the storePush notificationSyncs on reconnect

Summary

Chat messages are saved first, then routed through a presence service to the receiver's server. Offline users get a notification, and sync when they reconnect. Next, storing messages and handling groups.

Key takeaways
  • Save first
  • Presence finds the server
  • Sync on reconnect
Next up: Chat App: Storage and Groups — history at scale