Sub topic 3
Group Chat and Fan-Out Strategies
Assume you understand persistent connections, message IDs, acknowledgements, and basic key-value stores. This chapter focuses on how a chat system distributes events after a message enters the system.
Group Chat and Fan-Out
A group message has one conversation ID and several members. The service stores one canonical message, then delivers an event to each member's active devices.
Two common fan-out strategies are:
- Fan-out on write: copy a message into each recipient's inbox when it is sent. Reads are fast, but large groups create many writes.
- Fan-out on read: store one message in the conversation log. Each reader finds new messages when loading the conversation. Writes are cheaper, but reads require more work.
A hybrid approach often fits best. Use fan-out on write for small groups and fan-out on read for very large groups.
Presence with Heartbeats
Presence is an estimate, not proof that a person is actively reading. A client sends a heartbeat periodically. The connection service records the latest time. A user is online while that timestamp is within a configured time-to-live window.
Use expiration rather than a permanent online flag. A crashed client cannot send a “going offline” event, but its presence will expire.
Notifying Offline Users
When a member has no active device connection, the notification service can send a push notification. The notification should contain minimal content and identify the conversation or message. The client then fetches authoritative data from the chat service.
Avoid sending one push for every message in a busy group. Coalesce notifications and apply per-user preferences.
Syncing Devices and Loading History
Each device tracks its last received sequence number per conversation. After reconnecting, it requests messages after that sequence. The server returns a bounded page and a cursor for the next page.
This model handles missed messages and multiple devices. A device should acknowledge receipt only after it has safely stored or displayed the event, according to the product's guarantee.
Scaling Connection Servers and Routing
Connection servers hold WebSocket connections, so they are stateful. A routing layer maps a user or device to its current connection server. A shared broker distributes message events between servers.
Keep message storage and delivery separate. If a connection server fails, another server can accept the reconnecting device and replay messages from its last sequence number.
Handling Media and Attachments
Store images and files in object storage, not in the message database. The chat service creates a short-lived upload URL, records metadata after the upload, and sends a message containing the attachment ID. Validate type, size, ownership, and malware status.
Security, Privacy, and Rate Limiting
Authenticate every connection and authorize membership for every send and read. Encrypt traffic in transit and restrict stored data by conversation access. Apply limits per user, device, IP address, and conversation to reduce spam and abusive fan-out.
Exercise
When is fan-out on read usually preferable to fan-out on write?
Show answer
For very large groups or celebrity-style conversations, because one send avoids writing a copy for every member.