CRDTS
Delta-state
Add-Wins-Observed-Removed Map
Data Structures
Concurrent Computing

CRDTS Delta-state Add-Wins-Observed-Removed Map

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Conflict-Free Replicated Data Types (CRDTs) are data structures that enable multiple parties to concurrently update shared data without the need for immediate coordination. Among the various types of CRDTs, the Delta-state Add-Wins-Observed-Removed (AW-OR) Map is particularly powerful due to its efficient replication mechanisms and correctness guarantees in eventual consistency scenarios. This article will explore the functionality, applications, and implementation nuances of the Delta-state AW-OR Map CRDT.

Understanding Delta-state CRDTs

CRDTs are categorized broadly into two types based on their synchronization mechanism: State-based (Convergent) CRDTs and Operation-based (Commutative) CRDTs. Delta-state CRDTs can be regarded as an optimization of state-based CRDTs, designed to minimize the amount of data transferred during updates by only sending changes (delta mutations) instead of full state.

Delta-state AW-OR Map Explained

The Delta-state AW-OR Map CRDT combines the principles of the “Add-Wins” and “Observed-Removed” Set CRDTs. This map permits keys to be mapped to values that are essentially CRDT sets, combining the functionalities of addition and removal into a robust and decentralized structure.

Add-Wins Conflict Resolution

The "Add-Wins" policy in the context of the AW-OR Map dictates that in the event of concurrent updates (typically additions and deletions), the additions will "win" over deletions. This approach means that if there is a conflict between adding an item and removing the same item, the item will remain added.

Observed-Removed (OR) Set

The values in the AW-OR Map are maintained as OR sets, where each value is tagged with a unique identifier. When a value is removed, its identifier is moved to a tombstone list (a list of removed items). The presence of an item in the tombstone list prevents it from erroneously reappearing due to out-of-order message delivery or other inconsistencies.

Technical Implementation

Consider a simple example where we have a map with keys representing users and values as sets of items they own. If User A concurrently adds and User B removes the same item, the final state according to the Add-Wins policy would include the item.

Let's represent the deltas using pseudocode:

python
1def add_item(user, item): 
2    if item not already in map[user]:
3        delta = {user: {add: item}}
4        merge(delta)
5
6def remove_item(user, item):
7    if item in map[user]:
8        delta = {user: {remove: item}}
9        merge(delta)
10
11def merge(delta):
12    for user, changes in delta.items():
13        for action, items in changes.items():
14            if action == 'add':
15                map[user].add(items)
16            elif action == 'remove' and items not in map[user].observed:
17                map[user].remove(items)

In an actual distributed system, the merge function would handle synchronization between multiple nodes, ensuring that all updates are incorporated in a way that respects the CRDT's conflict resolution rules.

Applications

Delta-state AW-OR Maps are advantageous in scenarios like collaborative applications, real-time text editors, and distributed databases where non-blocking data synchronization is essential. They are ideal for environments with intermittent connectivity or where bandwidth is precious.

Key Properties Summary

Here’s a quick summary of the distinctive features of the Delta-state AW-OR Map:

FeatureDescription
Non-blocking writesUpdates can be made without coordination with other nodes.
Network Partition SafetyData consistency is maintained even across network partitions.
Bandwidth OptimizationOnly the changes (deltas) are sent over the network.
MergeabilityDeltas from different nodes can be merged non-destructively.
Eventual ConsistencyAll nodes converge to the same state eventually.

Conclusion

The Delta-state AW-OR Map exemplifies the efficiency and effectiveness of CRDTs in managing distributed data. It elegantly solves complex data synchronization problems, allowing developers to focus more on application logic rather than the intricacies of network communication and data consistency. With advantages like reduced network load, resilience to network partitions, and intuitive conflict resolution, this CRDT structure is invaluable for modern distributed systems.


Course illustration
Course illustration

All Rights Reserved.