Sub topic 10
Group chat and fan-out strategies
This chapter assumes you know how a one-to-one message flows through a chat server, and that each chat has its own sequence numbers and idempotent writes (covered in earlier chapters). Here we extend that flow to groups.
From one recipient to many
In a one-to-one chat, a message has one recipient. In a group, one incoming message must reach every member. Delivering a copy to each member is called fan-out. The key design question is where the copy happens: when the message is written, or when members read it.
The message itself should be stored once, in the group's log, with one sequence number per chat. Fan-out then concerns only how members learn that the message exists.
Two fan-out strategies
Fan-out on write pushes the message to each member at send time. Each member gets a delivery task, and an inbox entry if you keep per-user inboxes. Reads are cheap, because a client reads only its own inbox. Writes grow with group size.
Fan-out on read stores the message once in the group log. Each member fetches new messages by asking for everything after the last sequence number it has seen. Writes are cheap, but every reader queries the group log.
| Aspect | Fan-out on write | Fan-out on read |
|---|---|---|
| Work at send time | One task per member | One write |
| Work at read time | Read own inbox | Query group log |
| Storage | Message ID copied per member | Stored once |
| Best for | Small groups | Very large groups |
Most chat systems use a hybrid. For small groups, they push to every member's live connection immediately. For large groups, they push a lightweight signal such as "chat c1 has seq 4021", and clients fetch the content. A common cutoff is a few hundred members, but pick yours from measured load.
What the server does per message
- Validate that the sender is a member of the group.
- Deduplicate using
(chat_id, sender_id, client_msg_id)and assign the next sequence number, as in the earlierChatLog. - Load the member list from a cache.
- For each member, find the connection server holding their session and forward the message.
- For members with no live connection, enqueue a push notification.
Here is the fan-out step in Python. It sends to every member except the sender.
def fan_out(msg, members, sessions, push_queue):
for user_id in members:
if user_id == msg["sender_id"]:
continue
server = sessions.get(user_id)
if server is not None:
server.append(msg)
else:
push_queue.append((user_id, msg["chat_id"], msg["seq"]))
sessions = {"bob": []}
push_queue = []
msg = {"chat_id": "c1", "seq": 1, "sender_id": "alice", "text": "hi"}
fan_out(msg, ["alice", "bob", "carol"], sessions, push_queue)
assert len(sessions["bob"]) == 1
assert push_queue == [("carol", "c1", 1)]Common errors
- Fanning out before the message is durably stored. If the server crashes, members have seen a message that does not exist.
- Giving each member their own sequence number. Members then disagree on order. Use one sequence per chat.
- Sending fan-out inline in the request path. A group of 500 slows the sender's acknowledgement. Acknowledge after the write, then fan out through a queue.
- Reading stale membership. A removed user may still receive messages until the cache expires. Invalidate on membership change.
Exercise
A group has 20,000 members and each message is about 200 bytes. Why is fan-out on write a poor fit, and what would you send instead?
Show answer
Each message would create 20,000 delivery tasks and possibly 20,000 inbox writes, which is about 4 MB of copies per message. Instead, store the message once in the group log and send members a small notification with the chat ID and latest sequence number, so clients fetch the content (fan-out on read).