# Chat and messaging - one socket, many deliveries

> Delivery guarantees, presence and ordering across devices. The connection tier is stateful and nothing else here is. Requirements free on Tierslate.

Source: https://tierslate.com/templates/chat-messaging

Messaging & realtime · System design · core · 8 step walkthrough

Delivery guarantees, presence and ordering across devices.

**Own this board: 400 credits.** The first act plays on the page. Owned once with credits, never a subscription.

## What this board gets wrong on purpose

The tension: The connection tier is stateful and nothing else here is. A gateway holds its sockets in memory, so it cannot be rolled or scaled like the stateless services beside it - deploying gw-1 hangs up on everybody on gw-1 - and the presence registry that is the only record of which gateway holds you is a single store with no replica behind it.

## Practise this as an interview

Take Chat and messaging - one socket, many deliveries as a timed system design interview on a blank board - on your own, with the requirements to check yourself against, or with a candidate you invite.

## Start here

### Why this design

Chat is the standard question for testing whether a candidate can let go of request/response. The send path is easy; the hard parts are that the server must reach a person who asked for nothing, that the tier holding those connections is stateful while it looks like every other service, and that "delivered" is a claim about a screen the system cannot see. Interviewers use it to probe delivery guarantees, ordering and fan-out; teams that run chat learn the same lessons from their first gateway deploy, which hangs up on a hundred thousand people at once.

### What you will learn

- Why the connection tier is stateful, and why a sticky load balancer balances once per connection rather than once per request - so load does not rebalance itself.
- Persist first, then fan out: why the message store is the delivery guarantee and every hop after it may fail and be retried.
- How a per-conversation sequence number (not arrival order, not a clock) gives every device the same order, and how gaps are detected and filled.
- The fan-out arithmetic - one send becomes ~5.6 device deliveries - and why large groups switch from push to pull.
- How the offline path works through APNs/FCM, and why a push notification is never a delivery receipt.
- How to roll a stateful gateway fleet: drain, jittered reconnects, admission control, and why the gateway should change rarely.

### How to read this board

1. Diagram, left to right: PEOPLE (a sender with an open socket, a recipient on two devices), then CONNECTION TIER · STATEFUL (Edge LB, gw-1, gw-2 and Presence), then CORE · STATELESS (Chat service, Message store, Fan-out, Delivery queue, Receipt writer).
2. Then the band along the bottom, OFFLINE PATH · NO SOCKET TO PUSH DOWN: Push service and APNs / FCM, the only dashed wires. Press play for the eight-step walkthrough - one message, two endings.
3. Page 1 · Requirements - the questions to ask first, and the latency and delivery targets the rest of the design is held to.
4. Pages 2 and 3 · Capacity and Storage - messages per second, sockets, gateways, fan-out, and the bytes behind them.
5. Page 4 · Availability - what happens when a gateway dies, when Presence dies, and what is shed first.
6. Page 5 · How it works - the send path told end to end, including ordering, receipts and the offline device.
7. Page 6 · Deployment plan - rolling the one tier that cannot simply be restarted, and running it in several regions.

## 1 · Requirements

### Ask these before drawing anything

- One-to-one only, or groups? How big can a group get? Group size is the fan-out factor, and above a few hundred members the delivery model has to change from push to pull.
- Multi-device? If one person reads on a laptop and a phone, every message is delivered per DEVICE, receipts are per device, and a new device needs history - which decides whether the server keeps a log at all.
- Does the server keep history, or only hold messages until they are delivered? Store-and-forward is cheaper and more private; a server-side log is what makes multi-device sync and a new phone work. This board keeps the log.
- What does "delivered" mean - stored by us, received by a device, or painted on a screen? Pick one per tick and say it out loud.
- What order must two devices agree on? Total order per conversation is achievable; a global order across conversations is neither achievable nor needed.
- Is the content end-to-end encrypted? If yes the server routes opaque envelopes and cannot index, search or preview them, and push payloads carry no text.
- Are presence ("online", "last seen") and typing indicators in scope? They are the highest-volume, lowest-value traffic and the first thing to shed.

### Functional

- Send a text message to a person or a group of up to 1,024 members; receive it live on every connected device.
- Deliver to devices that are offline via a platform push notification, and sync everything missed when the app next opens.
- Show sent (stored by us), delivered (acked by a device) and read (rendered on screen) per recipient.
- Keep a per-conversation history that a newly signed-in device can page backwards through.
- Show online / last-seen presence and typing indicators, best effort.

### Non-functional

- Send acknowledged ("sent" tick) only after a quorum write: p99 under 300 ms from the sender's socket.
- Delivery to an online recipient in the same region: p50 under 100 ms, p99 under 500 ms end to end; cross-region p99 under 1 s.
- Zero loss once "sent" is shown. Delivery is at-least-once; the client de-duplicates by message id, so the user sees exactly once.
- Every device of every member renders a conversation in the same order (by sequence number), even when copies arrive by different paths.
- Reconnect and catch up on 1,000 missed messages in under 2 s on a mobile network.
- Sending: 99.99% available. Live delivery: 99.95%, falling back to sync-on-open when it is not. Presence and typing: no target - they may go dark.

### Out of scope

- Media upload and storage, which is object storage plus a CDN and its own board.
- End-to-end key exchange (the Signal protocol) - assumed to exist; this board routes envelopes.
- Voice and video calls, search, spam and abuse classification.

## The rest of this board

This page publishes the question. The answer - 5 more written pages and an 8-step narrated walkthrough - is the board itself, and it opens in Tierslate.

- 5 written pages
- 8 walkthrough steps

5 pages behind this one:

- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan

## Related system design templates

- [Transactional email system design](https://tierslate.com/templates/email-delivery) - Why getting an email into the inbox is a negotiation with a receiver whose rules you cannot see.
- [Presence system design](https://tierslate.com/templates/presence-service) - Why a green dot is a claim that expires, and why fanning it out costs more than every message sent.
- [Push notifications system design](https://tierslate.com/templates/push-notifications) - Why push failures are silent, and how token lifecycle keeps a dead token from counting as delivered.
- [Team channels system design](https://tierslate.com/templates/slack-channels) - Why a 30,000-member channel is not a big DM, and why the unread badge is the hardest number to keep.
- [Encrypted delivery system design](https://tierslate.com/templates/whatsapp-delivery) - What changes when the server cannot read the message, and why every tick after the first is a device claim.
- [Webhook delivery system design](https://tierslate.com/templates/webhook-delivery) - Why a webhook is a promise to keep calling until the receiver answers, and how retries avoid becoming an attack.
- [Notification router system design](https://tierslate.com/templates/notification-router) - How one event is turned into email, push or SMS by preferences and quiet hours, without sending it twice.

All 100 system design templates: https://tierslate.com/templates (Markdown: https://tierslate.com/templates.md)
