Dealing with write conflicts:

Concurrent writes: Conflicting writes that are unaware of each other at the time of writing.

1. Conflict Avoidance

2. Converging towards a consistent state:

  1. Last Write Wins (LWW) It’s conflict resolution method where each replica stores only the most “recent” value and discards older values. This ensures eventual convergence of replicas to the same value. However, the “recent” value is determined arbitrarily, often using timestamps, because concurrent writes don’t have a natural order. Advantages:

    • Ensures eventual consistency among replicas. Disadvantages:
    • Sacrifices durability by discarding concurrent writes, potentially leading to data loss.
    • May even discard non-concurrent writes due to the arbitrary ordering. Usage Considerations:
    • Suitable for scenarios where data loss is acceptable, like caching.
    • Unsafe for critical data where losing writes is not acceptable.
    • Recommended usage in databases like Cassandra involves treating keys as immutable and using unique keys (e.g., UUIDs) to avoid concurrent updates.

    Example Databases:

    • Cassandra supports only LWW for conflict resolution.
    • Riak offers LWW as an optional feature.
  2. Give each replica a unique ID, and let writes that originated at a higher-numbered replica always take precedence over writes that originated at a lower- numbered replica. This approach also implies data loss.

  3. Somehow merge the values together—e.g., order them alphabetically and then concatenate them.

  4. Record the conflict in an explicit data structure that preserves all information, and write application code that resolves the conflict at some later time (perhaps by prompting the user), eg. Version Vector.

  5. Store siblings and have the user decide later to resolve merge conflicts, let’s say through Application Layer. (Read more about it in the following article).

  6. The DB automatically merges them for us! This is the premise behind CRDTs. Conflict-Free Replicated Data Types - CRDTs (Read more about it in the following article).


3. When are writes concurrent?

Version Vector:

Version vector in the below example will store the writes (or let’s say incremented values after writes from different Leaders). [Counter1 from Leader1, Counter2 from Leader2, Counter3 from Leader3]

Option 1:
Option 2:

The DB automatically merges them for us! This is the premise behind CRDTs(Conflict-Free Replicated Data Types - CRDTs!)