0

I am implementing an LWW map and in my design, all added key-value pairs have timestamps as is expected from LWW. That works for me until the same key is added in two replicas with different values at the same time. I can't understand how to make the merge operation commutative in this scenario.

Example:

Replica1 => add("key1", "value1", "time1")
Replica2 => add("key1", "value2", "time1")
Merge(Replica1, Replica2) # What should be the value of key1 in the resulting map?
unitSphere
  • 241
  • 3
  • 17
  • Doesn't "Last Write Wins" literally mean that you simply take the value with the most recent timestamp and discard other values for this key? – Konstantin Strukov Aug 22 '22 at 11:23
  • Correct. However the question is about concurrent writes, i.e the timestamp is the same – unitSphere Sep 01 '22 at 02:36
  • 1
    In case when two concurrent writes provided the same timestamp: 1. If your replicas are identified by unique IDs (which is the case for most of the CRDTs), use that IDs as a second level of comparison (higher ID wins). 2. If no IDs where provided, you can always try to compare by values themselves ("greater" value wins) - this exactly how Shelf CRDT works. – Bartosz Sypytkowski Oct 15 '22 at 12:49

1 Answers1

0

Let's see what last write wins means in terms on causality. Let's say two clients C1 and C2 changed the same data D (same key) to values D1 and D2 respectively. If they don't ever talk to each other directly, you can pick D1 or D2 as the last value, and they will be OK.

But, if they talk to each other like, C1 changed the value to D1, informed C2, which as the result changed it to D2. Now, D1 and D2 have causal dependency, D1 happens before D2, so if your system picks D1 as the last value in merge, you have broken the last write wins guaranty.

Now coming to your question, when two clients make two requests in parallel, those two requests no way can have causal dependency, as both were inflight together, so any value you pick is fine.

vasha
  • 96
  • 2
  • Thanks for your response. I have opted to store both updates so I won't break the commutativity principle of LWW CRDT. – unitSphere Oct 17 '22 at 03:29