Multiple Online/Offline clients updating YMap within YArray inconsistent results

Hi

Could somebody please sense check this to see whether this is expected behaviour or not?

Clients A, B, and C all have a copy of the same YDoc which contains a single top level YArray whose elements consist of YMap objects which are simple key, value stores where the values are ordinary JavaScript Strings.

If on each client i set the following values
A (Online) YMap.set(‘name’, ‘111’) change made 13:00
B (Offline) YMap.set(‘name’, ‘112’) change made 13:10
C (Offline) YMap.set(‘name’, ‘113’) change made 13:20

When Client B and C return online snd synchronise their changes with the server and Client A I can get one of any of these values as being the result of the change.
After testing this multiple times, I always get consistent results, so all clients agree it’s either ‘111’, ‘112’ or ‘113’ but there doesn’t seem to be any rhyme or reason (that I can fathom) as to what to expect the result to be.
Sometimes they all agree it’s ‘111’, sometimes, ‘112’ and sometimes ‘113’. In my logical view it should always be ‘113’ (which is ultimately what I want).

Before we start delving into specific implementations, is what I want possible?

Many thanks for your time.

David

Hi David. The value converges because of the CRDT algorithm. This is what allows it to be “conflict-free”. It is deterministic, but it is not based on clock time. In fact, clock time cannot be determined in a decentralized fashion. Whose clock is correct? Clocks of different clients drift, or can be explicitly changed, so a distributed algorithm has to rely on so-called Lamport timestamps that can maintain partial ordering.

If you want to sync based on clock timestamps, you need a centralized server that is the source of truth. Yjs can be used in a client-server configuration, but it treats the server as just another client, not as a single source of truth.

If A edits a value within seconds of B, does it matter whose write wins? If either one of them comes online first and has time to sync, then it will be ordered chronologically. It is only when both are offline or edits occur very close together that the order is non-chronological. It is still deterministic though.

Distributed computing is a bit of a paradigm shift. But if your business rules require chronological ordering, I would recommend a centralized approach.

1 Like

Hi raine

Thanks for your reply. It’s been a little while since I wrote my Yjs implementation and have been testing the interface.

I think I was making the changes on multiple client machines too close together. When I left a reasonable amount of time, it does start to make sense.

Many thanks

Have a great day!

1 Like