Can YJS "merge" two documents that were created on different client machines concurrently?

I find the CRDT space very fascinating.
As far as I understand now, once you create a document in YJS, you can share updates with other clients in realtime and all (or most) conflicts are resolved gracefully.
That is pretty cool.

However, I do not understand how YJS would function in the following scenario.

Let’s say we create a new YJS document for a hashmap - representing an entity (e.g. building)
As it represents a specific building, we assign a unique identity to it “Sydney Opera House”

Now let’s say, that two client machines (both offline for a sec) create a new YJS document for the same building (“Sydney Opera House”).

Now assume that the app can detect that two documents exist for the same building: doc1 and doc2
Can we now merge those documents (doc1 and doc2) using YJS?

Yes, as long as both clients use the same room name they will merge all updates on sync. The documents will converge due to the eventually consistent integration of updates by the CRDT.

If their updates involve completely different keys, then there will be no conflicts and all properties will be synced to both docs. If there is a conflicting key, it will basically be determined by last-write-wins (the client with the higher clock), but both clients will get the same doc state in the end.

3 Likes

Thank you so much for your reply! :slight_smile: