← Lessons

quiz vs the machine

Gold1490

System Design

The Chat System Design

Delivering real time messages with persistent connections, presence, and ordering.

5 min read · core · beat Gold to climb

What a chat system needs

A chat system delivers messages between users in near real time. Unlike a request response api, it must push messages to recipients the moment they arrive and keep them in sync across devices.

Persistent connections

  • Clients hold a long lived connection, a websocket, to a connection server.
  • A user is mapped to whichever server holds their socket, so the system can find where to deliver a message.
  • A routing layer sends a new message to the server holding the recipient connection.

Storing and ordering

  • Messages are persisted so they survive offline recipients and load on reconnect.
  • Each conversation needs a consistent order, often by a sequence number or timestamp per chat, so all devices see the same sequence.
  • Delivery and read receipts track message state.

Presence and offline

  • Presence shows who is online, updated as connections open and close.
  • If a recipient is offline, the message is stored and a push notification wakes their device, then they sync on reconnect.

Key idea

A chat system pushes messages over persistent websocket connections through a routing layer, persists and orders messages per conversation, and uses presence and push notifications for offline users.

Check yourself

Answer to earn rating on the learn ladder.

1. Why do chat clients hold a long lived connection?

2. Why give each conversation a consistent message order?

3. How are messages delivered to an offline recipient?