Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

A group chat service builds message ids from the sending server's clock plus a random suffix, and clients sort by id. Users with a phone and a laptop report messages out of order in busy groups, and some messages show twice on the second device after it reconnects.

What causes both symptoms, and how would you design message ordering and multi-device sync so every device shows the same order with no gaps or duplicates?

Both symptoms come from treating a timestamp as a sequence. Server clocks differ by tens or hundreds of milliseconds, so timestamp-based ids do not match the order in which messages were accepted, and devices that receive messages in different orders and sort by id disagree; duplicates come from client retries stored twice and from reconnect sync overlapping messages already pushed. The fix is a per-conversation sequence number assigned in one place, deduplication on (conversation_id, client_msg_id) at write time and by seq on each device, and a per-device cursor of the last contiguous seq, with gap filling before display.

Why this question is asked

Ordering bugs in chat are maddening for users: a reply appears above the question, or a message shows twice on the laptop but once on the phone. They are also a favourite interview question, because they test whether we understand that distributed systems have no single clock. In this answer, we will see exactly how clock-based ids produce the wrong order, where the duplicates come from, and then design per-conversation sequencing, deduplication and per-device cursors that make every device converge on the same history.

1A Quick Recap of the Current Design

Today a message id is the sending server's clock time in milliseconds followed by a random suffix, so ids roughly increase with time and are unique. Clients sort messages by id. In a quiet one-to-one chat this looks fine, because messages are seconds apart. In a busy group, several people write within the same few hundred milliseconds, through different servers, and that is where it breaks.

2Why Timestamp Ids Give the Wrong Order

Server clocks are never exactly the same. Even with network time sync, two servers commonly differ by 50 to 200 ms, more after a pause or a bad sync. Suppose Ana's message reaches server S1 first and is accepted first, then Ben's reply reaches server S2 80 ms later. If S2's clock is 150 ms behind S1's, Ben's reply gets the smaller timestamp and sorts before the message it answers.

Different devices then make it worse. The phone may receive Ana's message first and show it, and then receive Ben's reply and insert it above. The laptop, reconnecting later, fetches both and sorts by id. Depending on what each device received when, and whether it re-sorts, the two devices can show different orders. The underlying problem is that a timestamp says when a clock thought something happened, not in what order the system accepted it.

Clock skew reversing the order
EventReal orderServer clockId prefix
Ana's message accepted on S11stS1 correct10:00:00.500
Ben's reply accepted on S22nd, 80 ms laterS2 150 ms behind10:00:00.430
Sorted by id--Ben's reply shown first

3Where the Duplicates Come From

There are two sources. First, client retries: the phone sends a message, the server stores it, but the ack is lost; the phone sends it again, and the server, seeing a new request, stores a second copy with a new id. Second, the reconnect sync: while the laptop was connected, some messages were pushed to it, then it disconnected and reconnected, and asked for messages since a time or id that was older than what it already had. The overlap is delivered again, and if the device does not deduplicate, it shows twice.

Both are normal events in a network, so the design must make them harmless rather than hope they do not happen.

4Fix 1: Assign a Sequence Number per Conversation in One Place

Order should mean acceptance order. So each conversation gets a counter, and every accepted message takes the next number: 1041, 1042, 1043. The number must be assigned in one place so it is unique and gapless. One approach is a single owner: conversations are partitioned, for example by hashing the conversation id onto a Kafka partition or a service shard, and the one process that owns a partition assigns the numbers for its conversations in memory, persisting them as it writes. Another is a conditional increment on a counter row in the database, so two concurrent writers cannot both take the same number.

Because numbers are gapless, a device can now detect a missing message: if it has 1042 and receives 1044, it knows 1043 exists somewhere.

SQL
-- A conditional increment: only one writer can move the counter from 1042 to 1043
UPDATE conversation_counter SET next_seq = 1043
WHERE conversation_id = 'c_81' AND next_seq = 1042; -- 1 row: we own seq 1043
-- 0 rows: someone else took it, read again and retry
-- Deduplicate client retries at write time
INSERT INTO message_dedup (conversation_id, client_msg_id, seq)
VALUES ('c_81', '7f3a-...', 1043)
ON CONFLICT (conversation_id, client_msg_id) DO NOTHING; -- retry: 0 rows, return the stored seq

The example uses SQL for clarity. In a wide-column store the same idea is a lightweight transaction, a compare-and-set, though at high rates the single-owner approach is cheaper because the owner needs no coordination for each number.

5Fix 2: Deduplicate at Write Time and on Each Device

Every message carries a client_msg_id generated on the sending device. The server keeps (conversation_id, client_msg_id) unique; when a retry arrives, it returns the seq already stored instead of storing again. The sender's device then sees the same seq in the ack and knows it is the same message.

On each device, messages are stored keyed by (conversation_id, seq). If the reconnect sync delivers a seq the device already has, it is simply ignored. The two layers together remove both sources of duplicates.

6Fix 3: Per-Device Cursors and Gap Filling

Each device keeps its own cursor per conversation: the last contiguous seq it holds, meaning it has every message up to that number. On reconnect it asks for messages after its cursor, so it never misses anything and only overlaps harmlessly. When a pushed message arrives with a seq beyond the cursor plus one, the device fetches the gap before displaying, so it never shows 1044 above an empty space where 1043 belongs.

Because every device uses the same server-assigned numbers, every device converges on the same order with no gaps and no duplicates, whatever order the pushes happened to arrive in.

Step throughA device receiving a pushed message
  1. Receive message with seq Nfor conversation c_81
  2. Already have seq N?
    YesIgnore the duplicateNoCheck for a gap
  3. Is N equal to cursor + 1?
    YesStore, display, advance cursorNoFetch messages cursor+1 to N-1 first
  4. Display in seq orderevery device shows the same order

4 steps in order. Play them, or step through them one at a time.

7Why Per-Conversation Order Is Enough

It is tempting to aim for one global order of all messages. But no user ever compares the order of messages in two different conversations, and a global sequencer would have to handle every message in the system, roughly 100,000 per second at peak, making it a bottleneck and a single point of failure. Ordering per conversation spreads the work across as many sequencers as we need while giving users everything they can notice.

8Follow-up questions

How do you keep sequence assignment available when the owner of a conversation's partition fails?

Ownership moves to another process, and the new owner must start from the right number. It reads the highest seq stored for each conversation, or the last committed counter, before assigning new numbers, and it holds a lease or fencing token so the old owner, if it is only slow rather than dead, cannot keep assigning numbers. With Kafka partitions, the consumer group rebalance moves the partition and the new consumer resumes from the stored state. Messages arriving during the handover wait or are retried by clients, which is safe because of the client message ids.

Can we use a hybrid logical clock instead of a sequencer?

A hybrid logical clock gives ids that respect causality between events a server has seen, which is better than raw timestamps. But it does not give gapless numbers, so devices still cannot detect missing messages, and two messages written concurrently on different servers still get an arbitrary order. For a chat conversation, a sequence per conversation is simpler and gives both ordering and gap detection.

What should the sender's device show before the ack arrives?

It shows the message at the bottom as pending, ordered locally by when the user sent it. When the ack arrives with the seq, the device moves it to its correct place by seq, which is usually where it already is.

9Conclusion

In this answer, we saw that timestamp ids order messages by server clocks, which differ by tens or hundreds of milliseconds, so busy groups show replies before questions and devices disagree. Duplicates come from retries stored twice and from overlapping reconnect syncs. Assigning a gapless sequence number per conversation in one place, deduplicating on client message id at write time and by seq on each device, and keeping a per-device cursor with gap filling make all devices converge on the same order, and per-conversation ordering is all users need.

What to say in the interviewAbout 80 seconds aloud

Open with

Both symptoms come from using a timestamp as if it were a sequence. Server clocks differ, often by 50 to 200 milliseconds, so in a busy group a reply accepted later on a server whose clock is behind gets a smaller id and sorts above the message it answers, and devices that received messages in different orders end up disagreeing.

Then, if they want more

The duplicates come from client retries after a lost ack being stored a second time, and from the reconnect sync overlapping messages the device already got by push.

My fix is to assign a sequence number per conversation in one place, either the single owner of the conversation's partition or a conditional increment on a counter row, so order means acceptance order and numbers have no gaps.

I deduplicate at write time on conversation id plus client message id, returning the existing seq for a retry, and on each device by seq. Each device keeps its own cursor, the last contiguous seq, asks for everything after it on reconnect, and if a pushed message skips a number it fetches the gap before displaying.

Then every device converges on the same history. Ordering per conversation is enough; a global order would make the sequencer a bottleneck.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What causes both symptoms, and how would you design message ordering and multi-device...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/a8ffb2f528/