Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • About
  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

👥10,105visitors

Built for engineers who want to go deep.


← System Design Fundamentals

Scalability Fundamentals

  • Horizontal vs Vertical Scaling
  • Load Balancing
  • Caching Strategies

Databases at Scale

  • CAP Theorem
  • Database Sharding
  • Replication & Consistency
  • Consistent Hashing
Chaturmind
← System Design Fundamentals

Scalability Fundamentals

  • Horizontal vs Vertical Scaling
  • Load Balancing
  • Caching Strategies

Databases at Scale

  • CAP Theorem
  • Database Sharding
  • Replication & Consistency
  • Consistent Hashing
HomeLearnSystem DesignSystem Design FundamentalsDatabases at Scale
✓ FreeAdvanced· 7 min read

Replication & Consistency

Leader-follower, multi-master, eventual consistency — make distributed data reliable.

Published September 21, 2026


Replication & Consistency

Replication means keeping copies of the same data on several machines. It serves three purposes:

  • Availability and durability: if one machine dies, another has the data.
  • Read scaling: many copies can serve reads in parallel.
  • Lower latency: copies can sit close to users in different regions.

Replication is easy while data never changes. Every hard problem comes from changes: copies are updated at slightly different moments, so at any instant they may disagree. How a system handles that gap defines its consistency behaviour, and it's one of the most important trade-offs in distributed design.

Advertisement

Single-leader (primary–replica) replication

One node, the leader, accepts all writes. It records changes in a replication log (PostgreSQL's WAL, MySQL's binlog, MongoDB's oplog) that followers apply in the same order. Reads can go to the leader or the followers.

 writes ─▶ Leader ──log──▶ Follower 1 ─▶ reads
              └────log──▶ Follower 2 ─▶ reads
  • ✅ Simple, and it's the default in PostgreSQL, MySQL, MongoDB replica sets and Redis. There are no write conflicts, because only one node accepts writes.
  • ✅ Read capacity scales by adding followers.
  • ❌ Write throughput is limited to one node. Sharding is the next step (see Database Sharding).
  • ❌ Failover is subtle: detect that the leader is dead, pick the most up-to-date follower, redirect clients, and make sure the old leader, if it comes back, doesn't still accept writes (split brain). That's usually prevented with consensus and fencing (epochs or terms).

Synchronous vs asynchronous replication

SynchronousAsynchronous
The leader acknowledges a write after…at least one follower has itwriting it locally
Write latencyHigher (a network round trip)Lower
On leader crashNo acknowledged write lostRecent acknowledged writes can be lost
AvailabilityA slow or dead follower can block writesFollowers can't block the leader

Common middle grounds:

  • Semi-synchronous: one follower is synchronous, the others asynchronous.
  • Majority acknowledgement: MongoDB's w: "majority", and consensus-based systems (Raft or Paxos: etcd, CockroachDB, Spanner). A write is durable once a majority has it, so any future leader elected by a majority has it too.

Replication lag and the anomalies it causes

With asynchronous followers, reads from a follower may be stale by milliseconds, or by minutes under load. Three classic problems follow:

1. Read-your-writes. A user updates their profile, refreshes, and the read goes to a lagging follower, so the old profile appears. Fixes:

  • Read the user's own data from the leader.
  • Or read from the leader for a short window after they write.
  • Or track the write's log position, and only use followers that have caught up to it.

2. Monotonic reads. Two refreshes hit two different followers, and the newer comment appears and then disappears. Fix: route each user consistently to the same follower (by hashing the user ID), or track the last seen position.

3. Consistent prefix. A reader sees an answer before the question it replies to, because the two writes reached different partitions in different orders. Fix: keep causally related writes in the same partition, or track causal dependencies.

// Read-your-writes by routing: recently-writing users read from the leader
public Profile getProfile(String userId) {
    Instant lastWrite = recentWrites.get(userId);            // e.g., a short-TTL cache entry set on write
    boolean fresh = lastWrite != null && lastWrite.isAfter(Instant.now().minusSeconds(10));
    return (fresh ? leaderRepo : replicaRepo).findById(userId);
}

Multi-leader replication

Several nodes accept writes, typically one per data centre, and replicate to each other asynchronously.

  • ✅ Local write latency in every region, and each region keeps accepting writes if another is cut off.
  • ❌ Write conflicts: the same record changed in two regions at once.

Conflict resolution options:

  • Last-write-wins by timestamp: simple, but silently discards one update, and clock skew makes "last" unreliable.
  • Application-level merge: keep both versions and let the application or user resolve them.
  • CRDTs (conflict-free replicated data types): counters, sets and text structures whose merges are mathematically guaranteed to converge.
  • Avoiding conflicts: route all writes for a given record to its "home" region.

Collaborative editors and offline-capable mobile apps are forms of multi-leader replication.

Leaderless (Dynamo-style) replication

Any replica accepts reads and writes (Cassandra, DynamoDB's underlying design, Riak). The client or a coordinator sends each write to N replicas and waits for W acknowledgements, and reads query R replicas and take the newest version.

  • If W + R > N, every read quorum overlaps every write quorum, so a read contacts at least one replica with the latest acknowledged write. With N = 3, W = 2, R = 2 is the typical "quorum" setting.
  • W = 1 or R = 1 gives lower latency and higher availability, at the cost of staleness.
  • Stale replicas are repaired by read repair (fixing old copies noticed during reads) and anti-entropy processes that compare replicas with Merkle trees.
  • Sloppy quorums and hinted handoff keep writes flowing during failures by temporarily storing them elsewhere, which weakens the W + R > N guarantee during the outage.

Quorums reduce staleness, but don't provide full linearizability by themselves. Concurrent writes and partial failures can still produce anomalies. For true linearizability, use consensus.

Consistency models, from strongest to weakest

ModelGuarantee
LinearizableBehaves like a single copy: once a write completes, every later read sees it
SequentialEveryone sees operations in the same order, though not necessarily in real time
CausalCausally related operations appear in order everywhere. Unrelated ones may not
Read-your-writes / monotonic readsSession guarantees for a single client
EventualIf writes stop, replicas eventually converge. No timing guarantee

Stronger models cost latency and availability, because they need coordination. Choose per data type: account balances and uniqueness constraints need strong guarantees, while likes and view counts are fine with eventual consistency. See CAP Theorem for the formal trade-off.

Follow-up questions this topic invites — and their answers

Q: Why not always use synchronous replication? A: Every write waits for a network round trip to a replica, and if the synchronous replica is slow or down, writes stall. Majority-based acknowledgement gives durability without depending on any single replica.

Q: How do you avoid split brain during failover? A: Use a consensus-based election (Raft, or a coordination service such as ZooKeeper or etcd), so only one leader can hold a majority. Attach an epoch or term number to writes, so that storage and clients reject writes from a deposed leader (fencing).

Q: What does W + R > N guarantee, and what doesn't it? A: It guarantees that read and write quorums overlap, so a read normally contacts a replica holding the latest acknowledged write. It doesn't prevent anomalies from concurrent writes, sloppy quorums or partially failed writes, so it isn't the same as linearizability.

Q: How do you give users read-your-writes consistency with read replicas? A: Route a user's reads to the leader for a short period after they write, or for their own data only. Or record the log position of their last write, and only read from replicas that have applied it.

Q: When is multi-leader replication worth its complexity? A: When you need low-latency writes in several regions, or offline operation (mobile, collaborative editing), and the data either rarely conflicts or has a natural merge rule. Otherwise, single-leader with regional read replicas is much simpler.

Advertisement

Previous

Database Sharding

Next

Consistent Hashing

AI Tutor

Lesson: Replication & Consistency

Quick actions

AI responses can be inaccurate. Verify critical information.