Designing a Chat Application
Difficulty: Intermediate Patterns: Mediator, Observer, State Asked at: Meta, Discord, Slack, Amazon
Direct messages, group rooms, blocking, the ticks under each message, the “typing…” line. Every candidate has used one for years, which is why interviewers like it: the vocabulary is free, so the whole hour is spent on design. And almost everybody opens with the same two classes, User and Message, with User holding a list of the other users it talks to.
That opening is the one the Mediator pattern lesson takes apart, so this page will not re-derive it. The short version: n peers each holding the others is O(n^2) references rewritten on every join and leave, and a rule about a pair – Asha blocked Ben – has no natural owner among them. One hub per room, ChatRoom, owns the membership and the rules, and a participant holds one reference. Settled, and worth four minutes.
The interesting half of the hour is what the hub does once it exists, and it comes down to three questions. What order do messages arrive in? Two members hit send in the same millisecond, and every one of the room’s twelve devices has to render those two in the same order, forever, including the device that was asleep and reconnects tomorrow. A timestamp looks like the answer and is not one. Who has seen it? A single status field cannot say “Asha read it, Ben has not opened the app since Tuesday”, so delivery state is per recipient, and the ticks are a fold over those rows. And what does that cost in a 200-member room? Per-recipient state means 199 rows per message, which is why real products stop showing who read your message above some room size and show a count instead.
The trap, in one sentence: candidates spend the hour on the mesh-versus-hub argument they already know, and treat ordering and receipts as bookkeeping to mention at the end. They are the design.
💡 Mediator = one object a group of peers talk to instead of talking to each other, which is where any rule about two of them has to live. ChatRoom is the mediator here. Learn more →
💡 Fan-out = one inbound message turning into n outbound deliveries, one per recipient. A group room is a fan-out of size n-1, and its cost decides how big a room you can support. Learn more →
Functional Requirements
- Direct messages – any two members exchange messages in a private room, and either side opening the conversation resolves to the same room.
- Group rooms – a member sends once and every other member of the room receives it, with the sender excluded from their own fan-out.
- Message ordering – every member of a room sees the same order for the same messages, including two sends that race, and including a device that reconnects after an hour away.
- Delivery and read receipts – a sender learns per recipient whether a message was delivered and whether it was read, and the summary rendered under the message follows the slowest recipient.
- Blocking – a blocked pair exchanges nothing in either direction, enforced by the room rather than by either member.
- Join and catch up – a joining or reconnecting member receives recent history in order, and asks for “everything after sequence N” to resume exactly where it stopped.
- Presence, typing and unread counts – online state and a typing indicator that expire on their own and are never stored with the messages, plus a per-member unread count per room maintained as messages arrive rather than recomputed.
Non-Functional Requirements
- One total order per room, assigned in O(1), and catch-up is a seek – the hub hands out a strictly increasing sequence number under its own lock, and
messagesAfter(seq)finds its starting point in O(log n), so a reconnecting client costs O(log n + k) rather than a comparison against every message in the room. The order is per room, not global, which is what lets two rooms be locked independently today and placed on different servers later. - No lock is held across delivery – the room’s lock covers the counter, the append and a snapshot of the members, and is released before any
receiveruns, so one slow socket cannot block anybody else’s send. - Receipt memory is bounded and acknowledged as the dominant cost – state is one row per recipient per retained message, so a 200-member room costs 199 rows per message. Retention trims the rows with the message, and above a configured room size the UI is served a count instead of a list of names.
Discussion
Newest first