educative.io

How does the messenger maintain the sequencing of the messages?

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.

Can anyone help me understand this solution? I don’t get it

6 Likes

Hi ma.Bikram, we are looking into the query you posted and will get back to you as soon as possible. Thank you for reaching out to us.

So we need the sequence number to determine the ordering of the messages. Ideally, we don’t need a sequence number, the time-of-creation of a message should be sufficient to determine the ordering of the messages. But because of the scenario, describe right above this text you posted, we need to have a sequence number to figure out exact ordering. Here is that scenario from the chapter:

The scenario where the server timestamp cannot determine the exact order of messages would look like this:

  1. User-1 sends a message M1 to the server for User-2.
  2. The server receives M1 at T1.
  3. Meanwhile, User-2 sends a message M2 to the server for User-1.
  4. The server receives the message M2 at T2, such that T2 > T1.
  5. The server sends message M1 to User-2 and M2 to User-1.

So User-1 will see M1 first and then M2, whereas User-2 will see M2 first and then M1.

This means we will store extra two numbers with every message that will tell the exact ordering for both the client. For example, a message could have a sequence number 51 for one client and 52 for the other client.

Hope this answered you question.

1 Like

Can we use client timestamp?

@Design_Gurus
Can someone please clarify it in detail ? I am unable to still understand how just generating the sequence number can help with ordering. In the example as said that user1 will see M1 followed by M2 whereas user2 will see M2 followed by M1. Now if each user also generates a sequence number or timestamp for each of it’s message to each of the client (separately). Then in our case user1 will send message M1 with sequence <1 (user1 seq), 0(user2 seq) > and user2 will send message M2 with sequence <0 (user1 seq), 1(user2 seq) >. So when both the message arrives at user1 and user2 they will have:
M1 <1, 0>
M2 <0, 1>

Now let’s say user1 sends more messages M3 <2, 1> and M4 <3, 1> then each of client will have following msgs. So in this case what will be the display order for both the users ?
M1 <1, 0>
M2 <0, 1>
M3 <2, 1>
M4 <3, 1>

If we use seq number of user1 then order will be M2, M1, M3, M4 whereas with seq number of user2 order will be M1, M2, M3 and M4 ? Please advice.

Hi.
Could someone explain the problem and resolution with example? Someone also asked, it really is not clear.
Thanks.

4 Likes

@changchingchi - No we can’t. There are two clients involved (as there will be two people chatting), both of them will have different timestamps.

@Sorabh, the sequence you presented is right. Both the users will see different order of messages. That’s why. also discussed above, we need to store the message ordering for each client separately.

@Design_Gurus: I guess, you are looking at this issue as -
First, User2 receives message with counter <1, 0> //(User1 counter, User2 counter)
Second, User2 sends and User1 receives message with counter<1,1>
Third, User1 sends and User2 receives message with counter <2,1> here messenger application will check User1’s previously received message (when User1 counter is 1) and then keeps new message with User1 counter 2.

Please correct me if I am wrong.

  1. So User-1 will see M1 first and then M2, whereas User-2 will see M2 first and then M1.
  2. To resolve this, we need to keep a sequence number with every message for each client.

The problem is in the wording of the solution. It reads as though the “resolution” will stop 1 from occurring entirely. In reality it appears that you’re suggesting (2.) that we have to store them separately and the ordering isn’t actually a problem from a design aspect.

2 Likes

no, it doesn’t answer the question, give some examples please with timestamps and sequence numbers in place

how come it works, please explain

Can someone explain why timestamp is not sufficient. For example if server receives messages accordingly :

User1 sends M1 to server for User2

Message Table

user_uid             message_id        timestamp   
-----------------------------------------------------------------------------------------
 (user1)123               m1                       T1                     
 (user2)356               m1                       T1                     
 (user1)123               m2                       T2                      
 (user2)356               m2                       T2                     

So when user1 client do below query:

select *
form message
where user_id = “123”
order by timestamp

same with user 2

select *
form message
where user_id = “356”
order by timestamp

please correct if I am wrong.Preformatted text

1 Like

@Design_Gurus
Can you comment on complexity to sort the messages at client using above method for group chat?

Is this vector clock?

@Design_Gurus Can you give example for sequence and timestamp? Its confusing… i don’t understand whats the point of FAQ when we don’t get proper answer ?

5 Likes

I think something like vector timestamp or View Synchrony might work for causal ordering.
All users see the same view at all time.

View Synchrony is used as a very tight consistency model for stock exchanges or air traffic controls.
If that is too strict, we can just use vector timestamps here for causal ordering. This also helps to determine concurrent message events (that can go in any order).

@Design Gurus,
I am confused at understanding problem itself,User 1 sees m1 then m2 and user2 sees m2 and then m1.Now u said in book
"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."
See the highlighted part. If they are still getting different view of message sequence then what are we trying to solve.Are we trying that both should see M1 followed by M2 or both should see their different views.If they needs to see different view, then what is the problem with user 1 sees M1,M2 and user2 sees M2,M1, And if we need same view for both then why you mentioned in highlighted part that it will show different view of the message sequence.

Both users will see different view, and that view needs to be consistent for each user. To make the view consistent, we need to store the ordering of messages for each user separately.

Took a lot of time to uderstand. Thanks. I never though of such scenerio.