CRDT Types Guide

Choose the right conflict-free replicated data type for your use case

What are CRDTs?

Conflict-free Replicated Data Types (CRDTs) are data structures that can be replicated across multiple nodes in a distributed system and merged automatically without conflicts. They guarantee that all replicas will eventually converge to the same state, regardless of the order of operations or network partitions.

πŸ”„

Convergence

All replicas eventually reach the same state

🚫

Conflict-Free

No manual conflict resolution required

πŸ“‘

Network Partition Tolerant

Works even when nodes are disconnected

Available CRDT Types

πŸ”’

Counters

GCounter (Grow-Only Counter)

A counter that can only be incremented. Perfect for tracking metrics that only increase.

Use Cases:
  • β€’ Event counting (page views, API calls)
  • β€’ Metrics aggregation
  • β€’ Resource usage tracking
let mut counter = GCounter::<DefaultConfig>::new(1);
counter.increment()?;
println!("Value: {}", counter.value()); // 1

PNCounter (Increment/Decrement Counter)

A counter that supports both increment and decrement operations using two internal GCounters.

Use Cases:
  • β€’ Inventory management
  • β€’ Vote counting (upvotes/downvotes)
  • β€’ Resource allocation
let mut counter = PNCounter::<DefaultConfig>::new(1);
counter.increment()?;
counter.decrement()?;
println!("Value: {}", counter.value()); // 0
πŸ“

Registers

LWWRegister (Last-Writer-Wins)

Stores a single value with timestamp-based conflict resolution. The most recent write wins.

Use Cases:
  • β€’ Configuration management
  • β€’ User profile data
  • β€’ System status tracking
let mut reg = LWWRegister::<String, DefaultConfig>::new(1);
reg.set("value".to_string(), 1000)?;
println!("Value: {:?}", reg.get()); // Some("value")

MVRegister (Multi-Value Register)

Stores multiple concurrent values when conflicts occur, allowing application-level resolution.

Use Cases:
  • β€’ Collaborative editing
  • β€’ Sensor calibration
  • β€’ Conflict detection systems
let mut reg = MVRegister::<f32, DefaultConfig>::new(1);
reg.set(1.05, 1000)?;
let values: Vec<f32> = reg.values().cloned().collect();
πŸ“¦

Sets

GSet (Grow-Only Set)

A set that only supports adding elements. Once added, elements cannot be removed.

Use Cases:
  • β€’ Device capability registry
  • β€’ Feature flags
  • β€’ Permanent audit logs
let mut set = GSet::<u32, DefaultConfig>::new();
set.insert(42)?;
println!("Contains 42: {}", set.contains(&42)); // true

ORSet (Observed-Remove Set)

A set that supports both adding and removing elements using unique tags for each operation.

Use Cases:
  • β€’ Shopping cart management
  • β€’ Active user sessions
  • β€’ Dynamic group membership
let mut set = ORSet::<u32, DefaultConfig>::new(1);
let tag = set.insert(42)?;
set.remove(&42, tag)?;
πŸ—ΊοΈ

Maps

LWWMap (Last-Writer-Wins Map)

A key-value map where each key uses last-writer-wins semantics for conflict resolution.

Use Cases:
  • β€’ Distributed configuration stores
  • β€’ Sensor data aggregation
  • β€’ User preference management
  • β€’ Metadata storage
let mut map = LWWMap::<String, f32, DefaultConfig>::new();
map.insert("temperature".to_string(), 25.5, 1000)?;
println!("Temp: {:?}", map.get(&"temperature".to_string()));

CRDT Selection Guide

Choose Based on Operations

Need to count things?

Use GCounter for increment-only or PNCounter for increment/decrement

Need to store single values?

Use LWWRegister for simple cases or MVRegister for conflict detection

Need to manage collections?

Use GSet for add-only or ORSet for add/remove operations

Need key-value storage?

Use LWWMap for distributed key-value data

Choose Based on Use Case

πŸš— Automotive

LWWRegister for sensor data, GCounter for error counts

πŸ€– Robotics

GSet for robot discovery, LWWMap for position tracking

🌐 IoT

LWWMap for sensor readings, GSet for device capabilities

🏭 Industrial

GCounter for production counts, LWWRegister for equipment status

Next Steps

πŸ’»

Try Examples

See practical examples of each CRDT type in action.

View Examples β†’
🌐

Domain Applications

Explore specialized CRDTs for different industries.

Explore Domains β†’