Enterprise Cloud Architecture, Zero Trust Cybersecurity, LLMOps & High-Scale Systems

Software Architecture

Eventual Consistency in Distributed Systems: Resolving Concurrent Conflict with CRDTs

November 22, 2026 ยท By writer

In multi-region distributed databases, collaborative applications, and edge computing architectures, maintaining strict synchronous consistency across global geographic regions introduces prohibitive network latency. Embracing eventual consistency allows local nodes to accept read and write operations immediately during network disconnections. However, resolving concurrent, conflicting writes without data loss or human intervention has historically proven difficult. Conflict-Free Replicated Data Types (CRDTs) solve this dilemma mathematically by ensuring that distributed replicas always converge deterministically regardless of message arrival ordering or network delay.

Operation-Based vs State-Based CRDTs

CRDTs achieve automatic conflict resolution through mathematically provable algebraic properties (semilattices: commutative, associative, and idempotent join functions):

CRDT TypeReplication MechanismNetwork OverheadFault Tolerance
State-Based (CvRDT)Transmits full data structure state to peersHigh (Transfers entire state)Resilient to duplicate or dropped network packets
Operation-Based (CmRDT)Transmits mutation operations (e.g., add item)Low (Only transmits payload delta)Requires exactly-once or at-least-once causal delivery
Delta-State CRDTTransmits small delta-mutations since last syncMinimal (Optimized bandwith)Balances low network overhead with partition tolerance

Implementing an LWW-Element-Set (Last-Write-Wins CRDT) in Python

Below is a production Python implementation of a Last-Write-Wins Element-Set CRDT, where additions and deletions are deterministically reconciled via logical timestamps:

# crdt_lww_set.py
import time
from typing import Set, Dict, Any

class LWWElementSet:
    def __init__(self):
        # Element -> Timestamp mappings
        self.add_set: Dict[Any, float] = {}
        self.remove_set: Dict[Any, float] = {}

    def add(self, element: Any, timestamp: float = None):
        ts = timestamp or time.time()
        self.add_set[element] = max(self.add_set.get(element, 0), ts)

    def remove(self, element: Any, timestamp: float = None):
        ts = timestamp or time.time()
        self.remove_set[element] = max(self.remove_set.get(element, 0), ts)

    def contains(self, element: Any) -> bool:
        add_ts = self.add_set.get(element)
        if add_ts is None:
            return False
        remove_ts = self.remove_set.get(element, 0)
        # Element exists if added more recently than removed
        return add_ts >= remove_ts

    def merge(self, other: 'LWWElementSet'):
        # Merge another replica's state deterministically (Commutative & Associative)
        for elem, ts in other.add_set.items():
            self.add_set[elem] = max(self.add_set.get(elem, 0), ts)
        for elem, ts in other.remove_set.items():
            self.remove_set[elem] = max(self.remove_set.get(elem, 0), ts)
  • Deterministic Convergence: Because the merge operation is commutative ($A \cup B = B \cup A$) and associative, all distributed nodes arrive at an identical state regardless of message delivery sequence.
  • Vector Clocks vs Physical Clocks: For applications sensitive to NTP clock drift across physical servers, utilize logical Lamport timestamps or Vector Clocks rather than physical system epoch times.
  • Tombstone Compaction: Periodically garbage-collect stale deletion records (tombstones) once all peer nodes acknowledge receipt to prevent unbounded memory growth.
๐Ÿท๏ธ Topics: