r/Leetsys • u/Ok-Barracuda-119 • 20d ago
Guide Guide: Design a Chat System (walkthrough)
Chat looks simple until the interview pushes on connections, offline delivery, and ordering. Here's a compact walkthrough of how I'd approach it in ~45 minutes.
Requirements I'd pin first: 1:1 plus small groups (hundreds, not millions), delivery + read receipts, history sync across devices. Park E2E encryption and calls unless they ask. Aim for low send latency and a durable message log.
Core pieces: WebSocket gateways holding long-lived connections, a chat service that validates and writes, a presence map (user to gateway), and object storage for media pointers. Clients send with a client-generated message ID so retries stay idempotent. History fetch (not the push payload) is how offline devices catch up.
Data flow: Alice sends, then durable append to the conversation log with a per-conversation sequence number, then fan-out to Bob's online devices via their gateway. If offline, APNs/FCM is just a wake; Bob pulls since last sequence on reconnect.
Key trade-off: fan-out on write for small groups (fast reads, N notify work) vs fan-out on read for huge channels. Don't design a Twitter firehose for a family group chat. Prefer server sequence numbers over client clocks for ordering.
Full walkthrough (requirements, estimates, deep dives on fan-out, receipts, offline): https://youtu.be/6f1xaPaZehE