Product · · 3 min read
Chat Message Ordering: Keep Conversations Consistent
Learn why chat messages arrive out of order and how sequence numbers and gap detection keep clients consistent.
Chat messages do not automatically arrive in the same order they were created. Network latency, retries, queues, multiple servers, and multiple devices can make a newer message reach a client before an older one. This guide explains chat message ordering for production chat systems, including what happens when requests are retried, delayed, or delivered out of order.
Chat message ordering is therefore an application-level problem. A reliable design assigns an authoritative ordering value on the server and makes clients render messages using that order.
Chat message ordering
Why arrival order is unreliable
Imagine a user sends two messages. The first takes 300 ms to reach the server. The second takes 80 ms. If the UI sorts by arrival time, the second message can appear first.
A realtime transport delivers events. It does not automatically define the business order of those events.
Use a server-assigned sequence
A strong design gives each message a monotonically increasing sequence number within its conversation.
| Message | Sequence |
|---|---|
| Are you free tonight? | 1042 |
| Let's grab dinner. | 1043 |
| 7 PM works? | 1044 |
Every client can now use the same authoritative order.
Why client timestamps are not enough
Device clocks can be incorrect or out of sync. Two devices can create timestamps that make a later message appear older.
Client timestamps are useful metadata, but they should not normally be the canonical conversation order.
Detect missing messages
Suppose the last confirmed sequence is 1045 and the client receives 1047. The client immediately knows that 1046 is missing.
It can wait briefly, request the missing range, reconnect, or resynchronize from the database. This is much safer than guessing based on timestamps.
Ordering across multiple devices
A user may have the same conversation open on a phone and laptop. The devices can observe events in different arrival orders.
If the server assigns the sequence, both devices eventually converge on the same conversation. The client does not need to invent a global order.
Ordering and optimistic UI
Chat should feel instant, so a client can show a local message before the server responds. That message has a temporary ID and a sending state.
When the server acknowledges it, the client replaces the temporary record with the canonical message ID, server sequence, and server timestamp.
The local message should not be treated as permanently ordered until the server assigns its canonical position.
Ordering with retries
Retries make ordering more interesting. If the same message is submitted twice, idempotency should make both requests resolve to one logical message rather than two sequence numbers.
Message identity and message ordering should therefore be designed together.
For the reliability side of this problem, see Idempotent Chat APIs: Safe Retries for Messages and Chat Message Delivery Guarantees Explained on the ByoTalk blog.
A practical data model
A message record can contain a server message ID, conversation ID, client message ID, sequence, sender ID, body, and creation time.
Useful constraints include a unique client message ID within a conversation and a unique sequence within a conversation.
The exact database implementation can vary, but the contract should remain stable.
FAQ
Is a timestamp enough for ordering?
Usually not. A server-controlled sequence is stronger and easier to use for gap detection.
Should sequence numbers be global?
Usually no. Conversation-scoped sequences reduce contention and match the problem being solved.
What should the client do when a sequence is missing?
Treat it as a gap and wait briefly, request the missing range, or resynchronize from the source of truth.
The practical rule
Transport order is not conversation order. Give messages a server-authoritative sequence, detect gaps, make retries idempotent, and let clients resynchronize from durable data when events arrive out of order.
Related ByoTalk guides
- chat-infrastructure
- realtime
- byo-database
Build chat on ByoTalk
Realtime chat and calls, with the permanent record in your own database.