This is a very difficult question, but I will try to answer some form.
My first reflex, having seen your diagram, should think about how distributed databases replicate data among themselves and recover in case one node crashes. This is most often done using gossiping .
Gossip meetings ensure that data is synchronized. Timestamps are stored at both ends, merged on demand, for example, when a node reconnects or simply at a specified interval (publishing mass updates via a socket, etc.).
Database engines, such as Cassandra or Scylla, use 3 messages per round of pooling.
Demonstration:
Data in node A
{ id: 1, timestamp: 10, data: { foo: '84' } } { id: 2, timestamp: 12, data: { foo: '23' } } { id: 3, timestamp: 12, data: { foo: '22' } }
Data in node B
{ id: 1, timestamp: 11, data: { foo: '50' } } { id: 2, timestamp: 11, data: { foo: '31' } } { id: 3, timestamp: 8, data: { foo: '32' } }
Step 1: SYN
It lists the identifiers and the last pop-up timestamps of all documents (feel free to change the structure of these data packets, here I use detailed JSON to better illustrate the process)
Node A -> Node B
[ { id: 1, timestamp: 10 }, { id: 2, timestamp: 12 }, { id: 3, timestamp: 12 } ]
Step 2: ACK
After receiving this package, node B compares the received timestamps with it. For each document, if it's a timestamp older, just put it in the ACK payload if it will contain new data along with the data. And if the timestamps are the same, do nothing - obviously.
Node B -> Node A
[ { id: 1, timestamp: 11, data: { foo: '50' } }, { id: 2, timestamp: 11 }, { id: 3, timestamp: 8 } ]
Step 3: ACK2
Node Updates the document if ACK data is provided, and then sends the latest data to node B for those where ACK data was not provided.
Node A -> Node B
[ { id: 2, timestamp: 12, data: { foo: '23' } }, { id: 3, timestamp: 12, data: { foo: '22' } } ]
Thus, both nodes now have the latest data combined in both directions (in case the client was working offline) - without having to send all your documents.
In your case, your source of truth is your server, but you can easily implement single-user gossip between your clients using WebRTC, for example.
Hope this helps in some way.
Cassandra instructional video
Scylla explanation