Chat Application

Real-Time Messaging System

Design the core of a chat server: direct and group conversations, server-assigned order, an offline queue flushed on reconnect, delivery and read receipts kept as watermarks, and paged history.

Overview

A chat server takes a message from one user and gets it to everyone else in the conversation, in the same order for all of them, whether they are online now or connect an hour later. It also tells the sender how far the message got: stored, delivered to every device, read by everyone.

Interviewers use this problem to see whether you can pick the right source of order (the server, not the clocks on the phones), keep an offline user's messages without losing or reordering them, and track receipts without storing one row per message per reader. Concurrency is part of it too: two people in a group send at the same moment, and both messages must get distinct, gap-free positions.

A strong answer gives each conversation its own sequence counter and lock, pushes to online users and queues for offline ones in the same locked step, and keeps two numbers per member (delivered up to, read up to) instead of a status on every message.

Scoping the problem

Ask these questions before you design, because each answer changes the model. Each comes with the answer most interviewers accept.

  • One-to-one only, or groups too? Groups add membership, admins and receipts from many readers. Usually both, and groups have admins.
  • How big can a group be? This decides whether sending copies to every member is fine. Usually small groups, tens to hundreds of members.
  • What must the sender see? This decides what receipts to track. Usually sent, delivered to all, and read by all.
  • What happens when a user is offline? That needs a queue per user and a flush on reconnect. Usually the server queues the messages and delivers them on connect, in order.
  • Does a new group member see old messages? This changes where their watermarks start. Usually no; they start at the current end.
  • What about media, typing indicators and presence? Each is its own feature. Name them; typing is not stored.
  • One server or many? This decides how order and routing are shared. One process here; many servers as a follow-up.

Functional requirements

  • Direct conversations (one per pair of users) and groups with admins
  • send(conversation, sender, text) gives the message the next sequence number in that conversation
  • Online recipients get the message at once; offline recipients get it, in order, when they connect
  • mark_read(conversation, user, up_to_seq) records that the user has read everything up to a message
  • status(conversation, seq) tells the sender SENT, DELIVERED (to every recipient) or READ (by every recipient)
  • history(conversation, user, before, limit) pages backwards with a sequence cursor
  • Only admins add or remove members; non-members cannot send or read

Non-functional requirements

  • Every member sees a conversation in the same order: the server assigns gap-free sequence numbers
  • No message is lost while a recipient is offline
  • Receipts cost two numbers per member, not a row per message per reader
  • Thread safe: one lock per conversation, one server lock, always taken conversation first
  • History pages stay correct while new messages arrive

Interview questions

  • Why not order messages by the timestamp the phone sends?
  • How does this change with many chat servers?
  • How do you handle a group with 100,000 members?
  • What happens if the client sends the same message twice because it did not see the reply?
  • How would you add typing indicators and online presence?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio