Replication skill

Replication means keeping a copy of the same data on multiple machines connected via a network.

by wondelai·MIT license·★ 2,235 Stars on the repo·GitHub ↗

Use now

Files of Replication

wondelai/main1 file
replication.md
Show the full text246 lines

Replication

Replication means keeping a copy of the same data on multiple machines connected via a network. The reasons for replication are: keeping data geographically close to users (reduce latency), allowing the system to continue working even if some machines fail (increase availability), and scaling out the number of machines that can serve read queries (increase read throughput).

Single-Leader Replication

How It Works

One node is designated the leader (primary, master). All writes go to the leader, which writes data to its local storage and sends the change to all followers (replicas, secondaries) via a replication log. Followers apply the changes in the same order.

Client Writes --> Leader --> Replication Log --> Follower 1
                                            --> Follower 2
                                            --> Follower 3

Client Reads  --> Leader OR any Follower
Synchronous vs. Asynchronous Replication
Mode Behavior Trade-off
Synchronous Leader waits for follower confirmation before acknowledging write to client Guaranteed durability on follower; higher write latency; follower outage blocks writes
Asynchronous Leader acknowledges write immediately after local write; replicates in background Lower write latency; leader failure can lose confirmed writes; follower may be stale
Semi-synchronous One follower is synchronous, rest are asynchronous Guarantees data exists on at least two nodes; practical compromise

PostgreSQL, MySQL, and MongoDB all default to asynchronous replication. This means a write that the leader confirms can be lost if the leader crashes before replicating it.

Leader Failover

When the leader fails, a follower must be promoted:

  1. Detect failure: Typically via heartbeat timeouts (e.g., no response for 30 seconds)
  2. Choose new leader: Consensus among remaining nodes, or human intervention
  3. Reconfigure system: Clients must send writes to the new leader; old leader must become a follower when it recovers

Failover dangers:

  • Split-brain: Two nodes both believe they are the leader; both accept writes, causing data divergence
  • Lost writes: If the old leader had unreplicated writes, they are lost (or conflict with new leader's writes)
  • Stale routing: Clients with cached leader addresses continue writing to the old leader

Replication Lag Problems

Asynchronous replication introduces a delay between a write on the leader and its appearance on followers. This lag causes several anomalies.

Read-Your-Own-Writes Violation

Problem: A user writes data (to the leader), then immediately reads it (from a follower that hasn't received the write yet). The user sees stale data and thinks their write was lost.

Solutions:

  • Read from the leader for data the user has recently modified (e.g., always read your own profile from the leader)
  • Track the timestamp of the user's last write; only read from followers that are caught up to that timestamp
  • Client remembers the position in the replication log of its last write and waits for the follower to reach that position
Monotonic Reads Violation

Problem: A user makes two reads in sequence and sees time go backward -- the second read returns older data than the first because it hit a different, more-lagged follower.

Solutions:

  • Pin each user to a specific follower (session affinity) so consecutive reads go to the same replica
  • Track the most recent read timestamp and ensure subsequent reads go to followers at least that current
Consistent Prefix Reads Violation

Problem: Causally related writes appear out of order. A database stores a question and its answer; a reader sees the answer before the question because different partitions have different replication lag.

Solutions:

  • Ensure causally related writes go to the same partition
  • Use causal consistency tracking (vector clocks or Lamport timestamps)

Multi-Leader Replication

Use Cases

Multi-leader (active-active) replication allows writes at multiple data centers:

  • Multi-datacenter operation: Each data center has its own leader; writes are fast locally and replicated asynchronously to other data centers
  • Offline-capable clients: A mobile app with a local database acts as a leader; syncs with server when online (CouchDB, PouchDB model)
  • Collaborative editing: Each user's local state acts as a leader; changes are merged asynchronously
Conflict Resolution

When two leaders accept conflicting writes to the same record, the conflict must be resolved:

Strategy How It Works Trade-off
Last writer wins (LWW) Assign a timestamp to each write; highest timestamp wins Simple but discards concurrent writes; data loss is possible
Merge values Combine conflicting values (e.g., union of sets) Preserves data but only works for certain data types
Application-level resolution Store all conflicting versions; let application code resolve Most flexible but pushes complexity to the application
CRDTs Use data structures mathematically guaranteed to converge Automatic convergence; limited to specific data types (counters, sets, registers)
Conflict Example
Leader A: UPDATE users SET name = 'Alice Chen' WHERE id = 42;
Leader B: UPDATE users SET name = 'Alice Wang' WHERE id = 42;

LWW result: One name wins (other is silently lost)
Merge result: Not meaningful for names
CRDT (LWW-Register): Last timestamp wins, but the conflict is detected
Application resolution: Show user both versions, ask which is correct

Leaderless Replication

How It Works

In leaderless replication (used by Dynamo, Cassandra, Riak, Voldemort), there is no leader. Clients send writes to multiple replicas directly (or via a coordinator node). Reads also query multiple replicas and reconcile differences.

Quorum Reads and Writes

Given n replicas, a write succeeds if acknowledged by w replicas, and a read succeeds if it reads from r replicas. As long as w + r > n, at least one of the read replicas will have the latest write.

Common configurations:

  • n=3, w=2, r=2: Tolerates 1 unavailable node for both reads and writes
  • n=3, w=3, r=1: Fastest reads (only need one node), but writes require all nodes
  • n=3, w=1, r=3: Fastest writes, but reads must query all nodes
  • n=5, w=3, r=3: Tolerates 2 unavailable nodes
Sloppy Quorums and Hinted Handoff

When a node is unavailable, a strict quorum would reject the write. A sloppy quorum instead writes to a different node temporarily. When the original node recovers, the temporary node forwards the data (hinted handoff).

This improves write availability but weakens consistency guarantees -- w + r > n no longer guarantees reading the latest write because the writes may be on nodes outside the usual n.

Read Repair and Anti-Entropy

Stale replicas need to be updated:

  • Read repair: When a read query detects a stale replica (by comparing versions), the client writes the latest value back to the stale replica
  • Anti-entropy process: A background process continuously compares data between replicas and copies missing data. Unlike replication logs, this does not preserve ordering.

CRDTs: Conflict-Free Replicated Data Types

What CRDTs Are

CRDTs are data structures that can be replicated across multiple nodes, where replicas can be updated independently and concurrently without coordination, and which mathematically guarantee eventual convergence.

Common CRDT Types
CRDT Use Case How It Works
G-Counter Counting (only increment) Each node maintains its own counter; total = sum of all node counters
PN-Counter Counting (increment and decrement) Two G-Counters: one for increments, one for decrements; value = P - N
G-Set Set (only add) Union of all elements across all replicas
OR-Set Set (add and remove) Each element has unique tags; remove deletes specific tags
LWW-Register Single value Last write (by timestamp) wins
MV-Register Single value Keeps all concurrent versions; application resolves
CRDT Example: Collaborative Counter
Node A starts: count = 0
Node B starts: count = 0

Node A: increment -> local count_A = 1
Node B: increment -> local count_B = 1
Node B: increment -> local count_B = 2

After sync:
Both nodes: total = count_A + count_B = 1 + 2 = 3

No conflicts, no coordination, mathematically correct.

CRDT Limitations
  • Only work for specific data types (not arbitrary business logic)
  • Can grow in memory over time (tombstones, version vectors)
  • Eventual consistency only -- no guarantee of when convergence happens
  • Complex to implement correctly from scratch; use libraries (Automerge, Yjs, delta-state CRDTs)

Replication Topology Patterns

Single-Leader Topologies
Star:     Leader --> F1, F2, F3, F4    (all followers connect to leader)
Chain:    Leader --> F1 --> F2 --> F3   (each follower replicates to next)

Star is simpler; chain reduces load on the leader but increases replication delay.

Multi-Leader Topologies
All-to-All:   A <--> B <--> C, A <--> C    (every leader replicates to every other)
Star:          A <--> B, A <--> C            (one central leader relays)
Circular:      A --> B --> C --> A            (each sends to the next in a ring)

All-to-all is most fault-tolerant but creates more replication traffic. Circular and star topologies have single points of failure.


Practical Replication Configurations

PostgreSQL Streaming Replication
Primary (leader) --> Streaming Replication --> Standby 1 (sync)
                                           --> Standby 2 (async)
                                           --> Standby 3 (async)
  • Write to primary; read from standbys for scaling
  • Synchronous standby guarantees zero data loss on failover
  • Asynchronous standbys may lag behind by seconds
Cassandra (Leaderless)
Client --> Coordinator Node --> Replica 1 (write)
                            --> Replica 2 (write)
                            --> Replica 3 (write)

Read: Query 2 of 3 replicas (QUORUM), return most recent
  • Replication factor (n) set per keyspace
  • Consistency level (w, r) set per query
  • Tunable consistency: ONE for speed, QUORUM for safety, ALL for strong consistency
Redis Sentinel (Single-Leader with Automatic Failover)
Master --> Replica 1
       --> Replica 2

Sentinel 1, Sentinel 2, Sentinel 3 (monitor master, vote on failover)
  • Sentinels detect master failure via heartbeats
  • Majority of sentinels must agree before promoting a replica
  • Client library queries sentinel for current master address
1# Replication
2 
3Replication means keeping a copy of the same data on multiple machines connected via a network. The reasons for replication are: keeping data geographically close to users (reduce latency), allowing the system to continue working even if some machines fail (increase availability), and scaling out the number of machines that can serve read queries (increase read throughput).
4 
5## Single-Leader Replication
6 
7### How It Works
8 
9One node is designated the leader (primary, master). All writes go to the leader, which writes data to its local storage and sends the change to all followers (replicas, secondaries) via a replication log. Followers apply the changes in the same order.
10 
11```
12Client Writes --> Leader --> Replication Log --> Follower 1
13 --> Follower 2
14 --> Follower 3
15 
16Client Reads --> Leader OR any Follower
17```
18 
19### Synchronous vs. Asynchronous Replication
20 
21| Mode | Behavior | Trade-off |
22|------|----------|-----------|
23| **Synchronous** | Leader waits for follower confirmation before acknowledging write to client | Guaranteed durability on follower; higher write latency; follower outage blocks writes |
24| **Asynchronous** | Leader acknowledges write immediately after local write; replicates in background | Lower write latency; leader failure can lose confirmed writes; follower may be stale |
25| **Semi-synchronous** | One follower is synchronous, rest are asynchronous | Guarantees data exists on at least two nodes; practical compromise |
26 
27PostgreSQL, MySQL, and MongoDB all default to asynchronous replication. This means a write that the leader confirms can be lost if the leader crashes before replicating it.
28 
29### Leader Failover
30 
31When the leader fails, a follower must be promoted:
32 
331. **Detect failure:** Typically via heartbeat timeouts (e.g., no response for 30 seconds)
342. **Choose new leader:** Consensus among remaining nodes, or human intervention
353. **Reconfigure system:** Clients must send writes to the new leader; old leader must become a follower when it recovers
36 
37**Failover dangers:**
38- **Split-brain:** Two nodes both believe they are the leader; both accept writes, causing data divergence
39- **Lost writes:** If the old leader had unreplicated writes, they are lost (or conflict with new leader's writes)
40- **Stale routing:** Clients with cached leader addresses continue writing to the old leader
41 
42---
43 
44## Replication Lag Problems
45 
46Asynchronous replication introduces a delay between a write on the leader and its appearance on followers. This lag causes several anomalies.
47 
48### Read-Your-Own-Writes Violation
49 
50**Problem:** A user writes data (to the leader), then immediately reads it (from a follower that hasn't received the write yet). The user sees stale data and thinks their write was lost.
51 
52**Solutions:**
53- Read from the leader for data the user has recently modified (e.g., always read your own profile from the leader)
54- Track the timestamp of the user's last write; only read from followers that are caught up to that timestamp
55- Client remembers the position in the replication log of its last write and waits for the follower to reach that position
56 
57### Monotonic Reads Violation
58 
59**Problem:** A user makes two reads in sequence and sees time go backward -- the second read returns older data than the first because it hit a different, more-lagged follower.
60 
61**Solutions:**
62- Pin each user to a specific follower (session affinity) so consecutive reads go to the same replica
63- Track the most recent read timestamp and ensure subsequent reads go to followers at least that current
64 
65### Consistent Prefix Reads Violation
66 
67**Problem:** Causally related writes appear out of order. A database stores a question and its answer; a reader sees the answer before the question because different partitions have different replication lag.
68 
69**Solutions:**
70- Ensure causally related writes go to the same partition
71- Use causal consistency tracking (vector clocks or Lamport timestamps)
72 
73---
74 
75## Multi-Leader Replication
76 
77### Use Cases
78 
79Multi-leader (active-active) replication allows writes at multiple data centers:
80 
81- **Multi-datacenter operation:** Each data center has its own leader; writes are fast locally and replicated asynchronously to other data centers
82- **Offline-capable clients:** A mobile app with a local database acts as a leader; syncs with server when online (CouchDB, PouchDB model)
83- **Collaborative editing:** Each user's local state acts as a leader; changes are merged asynchronously
84 
85### Conflict Resolution
86 
87When two leaders accept conflicting writes to the same record, the conflict must be resolved:
88 
89| Strategy | How It Works | Trade-off |
90|----------|-------------|-----------|
91| **Last writer wins (LWW)** | Assign a timestamp to each write; highest timestamp wins | Simple but discards concurrent writes; data loss is possible |
92| **Merge values** | Combine conflicting values (e.g., union of sets) | Preserves data but only works for certain data types |
93| **Application-level resolution** | Store all conflicting versions; let application code resolve | Most flexible but pushes complexity to the application |
94| **CRDTs** | Use data structures mathematically guaranteed to converge | Automatic convergence; limited to specific data types (counters, sets, registers) |
95 
96### Conflict Example
97 
98```
99Leader A: UPDATE users SET name = 'Alice Chen' WHERE id = 42;
100Leader B: UPDATE users SET name = 'Alice Wang' WHERE id = 42;
101 
102LWW result: One name wins (other is silently lost)
103Merge result: Not meaningful for names
104CRDT (LWW-Register): Last timestamp wins, but the conflict is detected
105Application resolution: Show user both versions, ask which is correct
106```
107 
108---
109 
110## Leaderless Replication
111 
112### How It Works
113 
114In leaderless replication (used by Dynamo, Cassandra, Riak, Voldemort), there is no leader. Clients send writes to multiple replicas directly (or via a coordinator node). Reads also query multiple replicas and reconcile differences.
115 
116### Quorum Reads and Writes
117 
118Given `n` replicas, a write succeeds if acknowledged by `w` replicas, and a read succeeds if it reads from `r` replicas. As long as `w + r > n`, at least one of the read replicas will have the latest write.
119 
120**Common configurations:**
121- `n=3, w=2, r=2`: Tolerates 1 unavailable node for both reads and writes
122- `n=3, w=3, r=1`: Fastest reads (only need one node), but writes require all nodes
123- `n=3, w=1, r=3`: Fastest writes, but reads must query all nodes
124- `n=5, w=3, r=3`: Tolerates 2 unavailable nodes
125 
126### Sloppy Quorums and Hinted Handoff
127 
128When a node is unavailable, a strict quorum would reject the write. A sloppy quorum instead writes to a different node temporarily. When the original node recovers, the temporary node forwards the data (hinted handoff).
129 
130This improves write availability but weakens consistency guarantees -- `w + r > n` no longer guarantees reading the latest write because the writes may be on nodes outside the usual `n`.
131 
132### Read Repair and Anti-Entropy
133 
134Stale replicas need to be updated:
135 
136- **Read repair:** When a read query detects a stale replica (by comparing versions), the client writes the latest value back to the stale replica
137- **Anti-entropy process:** A background process continuously compares data between replicas and copies missing data. Unlike replication logs, this does not preserve ordering.
138 
139---
140 
141## CRDTs: Conflict-Free Replicated Data Types
142 
143### What CRDTs Are
144 
145CRDTs are data structures that can be replicated across multiple nodes, where replicas can be updated independently and concurrently without coordination, and which mathematically guarantee eventual convergence.
146 
147### Common CRDT Types
148 
149| CRDT | Use Case | How It Works |
150|------|----------|-------------|
151| **G-Counter** | Counting (only increment) | Each node maintains its own counter; total = sum of all node counters |
152| **PN-Counter** | Counting (increment and decrement) | Two G-Counters: one for increments, one for decrements; value = P - N |
153| **G-Set** | Set (only add) | Union of all elements across all replicas |
154| **OR-Set** | Set (add and remove) | Each element has unique tags; remove deletes specific tags |
155| **LWW-Register** | Single value | Last write (by timestamp) wins |
156| **MV-Register** | Single value | Keeps all concurrent versions; application resolves |
157 
158### CRDT Example: Collaborative Counter
159 
160```
161Node A starts: count = 0
162Node B starts: count = 0
163 
164Node A: increment -> local count_A = 1
165Node B: increment -> local count_B = 1
166Node B: increment -> local count_B = 2
167 
168After sync:
169Both nodes: total = count_A + count_B = 1 + 2 = 3
170```
171 
172No conflicts, no coordination, mathematically correct.
173 
174### CRDT Limitations
175 
176- Only work for specific data types (not arbitrary business logic)
177- Can grow in memory over time (tombstones, version vectors)
178- Eventual consistency only -- no guarantee of when convergence happens
179- Complex to implement correctly from scratch; use libraries (Automerge, Yjs, delta-state CRDTs)
180 
181---
182 
183## Replication Topology Patterns
184 
185### Single-Leader Topologies
186 
187```
188Star: Leader --> F1, F2, F3, F4 (all followers connect to leader)
189Chain: Leader --> F1 --> F2 --> F3 (each follower replicates to next)
190```
191 
192Star is simpler; chain reduces load on the leader but increases replication delay.
193 
194### Multi-Leader Topologies
195 
196```
197All-to-All: A <--> B <--> C, A <--> C (every leader replicates to every other)
198Star: A <--> B, A <--> C (one central leader relays)
199Circular: A --> B --> C --> A (each sends to the next in a ring)
200```
201 
202All-to-all is most fault-tolerant but creates more replication traffic. Circular and star topologies have single points of failure.
203 
204---
205 
206## Practical Replication Configurations
207 
208### PostgreSQL Streaming Replication
209 
210```
211Primary (leader) --> Streaming Replication --> Standby 1 (sync)
212 --> Standby 2 (async)
213 --> Standby 3 (async)
214```
215 
216- Write to primary; read from standbys for scaling
217- Synchronous standby guarantees zero data loss on failover
218- Asynchronous standbys may lag behind by seconds
219 
220### Cassandra (Leaderless)
221 
222```
223Client --> Coordinator Node --> Replica 1 (write)
224 --> Replica 2 (write)
225 --> Replica 3 (write)
226 
227Read: Query 2 of 3 replicas (QUORUM), return most recent
228```
229 
230- Replication factor (n) set per keyspace
231- Consistency level (w, r) set per query
232- Tunable consistency: `ONE` for speed, `QUORUM` for safety, `ALL` for strong consistency
233 
234### Redis Sentinel (Single-Leader with Automatic Failover)
235 
236```
237Master --> Replica 1
238 --> Replica 2
239 
240Sentinel 1, Sentinel 2, Sentinel 3 (monitor master, vote on failover)
241```
242 
243- Sentinels detect master failure via heartbeats
244- Majority of sentinels must agree before promoting a replica
245- Client library queries sentinel for current master address
246 

Discussion