System Design · Senior · Question 9 of 75
How do you guarantee correct message order in a chat?
- ✓Correct answer: Server-assigned sequence numbers per conversation, not client time
- 2Sort messages by the sender's device time
- 3Send messages strictly one at a time and wait for everyone's acknowledgment
Explanation
Device clocks drift, and messages are delivered along different paths. The server assigns a monotonic number within a conversation; the client shows a sent message with a temporary id and replaces it with the confirmed one. Gaps in numbers signal the need to fetch missing messages.