educative.io

Sequence number will determine the exact ordering of messages for each user

To resolve this, we need to keep a sequence number with every message for each client. This sequence number will determine the exact ordering of messages for EACH user. With this solution, both clients will see a different view of the message sequence, but this view will be consistent for them on all devices.

how do we synchronize the sequence number across multiple devices owned by the same user?

1 Like

I think having a sequence number for each device doesn’t guarantee total number ordering.

You can use Lamport timestamp for a total ordering consistent with causality. but with this approach, we cannot tell whether two messages are concurrent. But at least it guarantees causal order, like the same person sends message A then B, then the order must be A then B.

each node has local variable c = 0,
every event node increment c = c + 1
send a message with the (c, m).
whenever node receive message with (c', m), update c with c = max(c, c') + 1 then deliver m to application.

The sequence number will be persisted in the messages db. When the different clients load the new message, the sequence number will be part of the payload.