CSE Roadmap

Core tier · about 5 hours · 8 parts

Part 7 of 8 · 2 min read

Trade-offs

What breaks and how to answer.

Failures, trade-offs and what to say

A chat server fails. Thousands of connections drop. Clients ask service discovery for a new server and reconnect. Then each device fetches the messages it missed using its latest id. Nothing is lost, because messages were saved before being forwarded.

Message resend. If a send is not confirmed, the client retries. The server uses the message id to drop duplicates. This is at-least-once with dedupe (Level 10, module 6).

Offline users. Messages wait in the store and in the inbox. A push notification tells the user to open the app.

Choice Gain Cost
WebSocket for both directions One simple channel Servers hold many open connections
Key-value store for history Scales out, low delay Weaker search and joins
Copy to every member's inbox Simple reads Too costly for large groups
Heartbeat presence No flicker Constant small traffic
Local message ids Easy, no global counter Order only within one channel

Extra topics (from the book). Photos and video, using compression, object storage and thumbnails. End-to-end encryption. Caching messages on the client. Spreading data across the world for faster loading.

Interview angle

Answer in your head first, then open it.

Why WebSocket?

Two-way, persistent, and works through firewalls.

Why is the chat server stateful?

Each client keeps one connection to one server.

How do you keep order?

A message id that grows within a channel, assigned by the server.

How do you sync devices?

Each keeps its latest id and asks the store for newer messages.

How do you show online status?

Heartbeats, with a timeout, and publish to friend channels.

One follow-up to try. "Support groups of 50,000 members." A good reply: stop copying into every inbox. Store the message once in the group's channel, and let each member read from the channel with their own latest id. Presence for such a group is fetched when the member opens it, not pushed.

Check yourself: 1. What happens to a user whose chat server dies? 2. How are duplicate sends dropped? 3. What changes for a 50,000-member group?

Words you will meet

Word Meaning
WebSocket A two-way channel that stays open after an HTTP start
Long polling Holding a request open until news arrives
Stateful service A service that keeps per-client state, such as a connection
Service discovery Finding the right server for a client
Heartbeat A regular "still alive" signal
Presence Whether a user is online
Inbox A per-recipient list of waiting messages
Channel A conversation, one-to-one or a group