Aparx Logo
Computer Science & Systems 8 min read • Aug 24, 2026

Understanding Conflict-Free Replicated Data Types (CRDTs) in Distributed Sync

A mathematical and practical guide to CRDTs. How modern offline-first collaborative systems achieve eventual consistency without central lock managers.

AO
Aparx Architecture Team
Aparx Software Engineering
Advertisement

In distributed systems where multiple mobile devices, tablets, and web dashboards edit the same shared business documents offline, central database locks are impossible. When connectivity is restored, how can two diverging copies of data merge without losing edits?

The mathematical breakthrough powering modern collaborative tools is Conflict-Free Replicated Data Types (CRDTs).

Why Traditional Merging Fails

Traditional databases rely on pessimistic locking (`SELECT ... FOR UPDATE`) or optimistic concurrency control (`version = version + 1`). If Device A and Device B both modify line 4 of an invoice while offline, both cannot increment version 1 to version 2 simultaneously. One must be rejected, discarding human work.

CRDTs solve this by ensuring that the merge operation is mathematically Commutative, Associative, and Idempotent:

• Commutative: A merged with B equals B merged with A (The order in which changes arrive does not matter).
• Associative: Grouping operations does not change the outcome (Network packet batching preserves integrity).
• Idempotent: Applying the exact same change multiple times produces identical state (Safe against network retries).

The Two Classes of CRDTs

+-------------------------------------------------------------+
|                        CRDT Typology                        |
+------------------------------+------------------------------+
| State-Based (CvRDT)          | Operation-Based (CmRDT)      |
+------------------------------+------------------------------+
| Full state sent across wire  | Only mutating operations sent|
| Merge function resolves state| Operations must be delivered |
| Robust against lost packets  | Requires reliable messaging  |
+------------------------------+------------------------------+

Implementing a Positive-Negative (PN) Counter in Software

For real-time counters (such as tracking product inventory counts or shared game scores across disconnected nodes), a PN-Counter maintains two distinct internal vector clocks: one for increments (P) and one for decrements (N):

State = (P_vector, N_vector)
Value = SUM(P_vector) - SUM(N_vector)
Merge = Element-wise MAX(P_A, P_B), MAX(N_A, N_B)

Because finding the maximum of two numbers is inherently commutative and idempotent, nodes can exchange states in any sequence across unreliable networks and converge to 100% identical balances with zero coordination locks.

Advertisement

Need an Enterprise System Built?

From offline-first mobile apps to full-scale SaaS platforms, our team can engineer your solution.