Skip to content

Real-time & chat · 5 of 6

Step 1 · The pattern

A conversation through a server

A and B write to the same chat. Each message goes to the server, which tells the sender sent and passes the message on to the reader over one of two network paths. On a calm day the messages arrive in the order they were written.

  • message
  • sent / shown

In short

Ordering & delivery receipts: how it works and how it breaks

Two chat messages sent close together can take different paths and reach a reader in the wrong order. A sender whose acknowledgement was lost sends again, and the reader may get the same message twice. Sorting by the sender's clock does not help, because phones' clocks disagree. The usual design gives the job to the server: it stamps each message in a conversation with the next sequence number. Clients show messages in sequence order. A client that sees number 3 while missing number 1 knows there is a gap, so it waits or asks the server for the rest. Each message also carries an id made up by the sending client, so the server recognises a resend and keeps its original number. Acknowledgements flow back in stages: sent when the server has accepted the message, delivered when a device has it, read when the person has seen it.

How this comes up in interviews

"Two people send chat messages within a second of each other, and some readers see the reply above the question. How do you give every reader the same order?"

Then they ask

  1. The sender's acknowledgement is lost and the app sends the message again. How do you avoid showing it twice?
  2. A reader receives a later message before an earlier one. What should the client do?
  3. What exactly do sent, delivered and read mean, and what changes when a person has several devices?

A strong answer covers

  • Phones' clocks disagree and arrival order differs per reader, so neither can define the order.
  • The server stamps each message in a conversation with the next sequence number, and clients show messages in that order.
  • A missing number tells a client there is a gap, so it waits or asks the server for the rest.
  • Each message carries an id made up by the sending client, so the server recognises a resend and keeps its original number.
  • Receipts come in stages: sent when the server accepts the message, delivered when a device has it, read when the person has seen it.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last reviewed: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.