What are CRDTs?
Automerge is built on Conflict-free Replicated Data Types (CRDTs), which are data structures that can be replicated across multiple computers, modified independently and concurrently, and then merged back together automatically without conflicts.Why Automerge Uses CRDTs
Automerge provides fast implementations of several different CRDTs to support local-first applications. The objective is to be like PostgreSQL for your local-first app - providing persistence mechanisms that allow you to avoid thinking about hard distributed computing problems.Local-first applications prioritize local data and local computation, with network synchronization happening in the background. This approach gives users:
- Instant responsiveness
- Offline capability
- Data ownership
- Privacy and security
How CRDTs Work in Automerge
CRDTs enable automatic conflict resolution through mathematical properties that guarantee eventual consistency. When two peers make concurrent changes, the CRDT algorithms ensure that both peers will arrive at the same final state when they sync.Core Principles
- Commutativity: Operations can be applied in any order
- Associativity: Grouping of operations doesn’t matter
- Idempotence: Applying the same operation multiple times has the same effect as applying it once
- Merge changes from multiple sources automatically
- Support offline editing with guaranteed convergence
- Enable peer-to-peer collaboration without a central server
CRDT Types in Automerge
Automerge implements several CRDT types to represent different data structures:Maps (Objects)
JavaScript objects are represented as CRDT maps where each key can be independently updated.Lists (Arrays)
Arrays use a list CRDT that preserves insertion order and handles concurrent insertions intelligently.Text
Automerge provides a specialized Text CRDT optimized for collaborative text editing.Counter
Counters are CRDT types that support concurrent increment and decrement operations.Scalar Values
Automerge supports various primitive types:- Strings
- Numbers (with
Int,Uint,Float64for specific numeric types) - Booleans
- Null
- Dates (stored as timestamps)
- Byte arrays (
Uint8Array)
CRDT Advantages
Benefits for Application Developers
- Automatic Merging: No manual conflict resolution code needed
- Offline-First: Work continues seamlessly without network connectivity
- Peer-to-Peer: No central server required for synchronization
- Time Travel: Full history enables viewing any past state
- Auditability: Complete change history for compliance and debugging
Performance Characteristics
Automerge 3 achieved significant performance improvements:- ~10x reduction in memory usage
- Compact binary format for efficient storage and transmission
- Optimized sync protocol for minimal network overhead
Binary Format and Compression
Automerge uses a highly optimized binary format that:- Compresses change history efficiently
- Enables incremental loading and saving
- Supports efficient network synchronization
- Maintains compatibility across versions