Course contentsShow
← System Design
Lesson 1386 of 1,91934. Data Replication StrategiesFree lesson

Merge Strategies for Common Data Types

Practical approaches to merging conflicts in sets, counters, and documents.

Merge Strategies for Common Data Types

What you'll learn: How to systematically merge conflicting updates to sets, counters, and documents when they occur on different replicas.

Why This Matters

When multiple replicas accept writes independently, conflicts are inevitable. Different data types need different merge strategies because their semantics differ—you can't apply the same logic to a shopping cart (set) as you would to a view counter (number).

Merge Strategies by Data Type

Sets (Collections)

When two replicas add/remove items independently, union the additions and apply all deletions. If replica A adds {item1} and replica B adds {item2}, the merged set becomes {item1, item2}. Some systems track tombstones to ensure deleted items stay deleted even if re-added elsewhere.

Analogy: Two people editing a shared grocery list on paper copies. When reconciling, include everything anyone added, remove anything anyone crossed off.

Counters

Simple addition doesn't work (both replicas incrementing from 5 would merge to 6, not 7). Use increment deltas: track +3 and +2 separately, then sum all deltas. CRDTs like G-Counter implement this by maintaining per-replica counters that are summed on read.

Analogy: Instead of saying "the score is 8," say "I added 3 points" and "I added 5 points"—then anyone can compute the total correctly.

Documents (Key-Value Maps)

Merge field-by-field using per-field LWW or version vectors. If user A updates name and user B updates email, both changes succeed. For the same field, apply timestamp-based or causal ordering. Some systems let applications provide custom merge functions for specific fields.

Analogy: Editing different sections of a shared document—most changes don't actually conflict, only simultaneous edits to the same paragraph need resolution.

Key Takeaway: Effective merge strategies exploit data type semantics—sets merge via union, counters via delta accumulation, and documents via field-level reconciliation—avoiding data loss while preserving user intent.