Catalog My learning Blog
Scalability Patterns: Load Balancing, Caching, Sharding · Phase 3 · Lesson 16 · 11 min read

Replication vs Sharding: How They Work Together

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: Sizing a Grid and Closing the Read-Your-Writes Gap
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Distinguish what replication scales from what sharding scales, explain why production systems need both rather than choosing between them, and reason about replication lag, read-your-writes consistency, and the availability arithmetic of a sharded and replicated fleet.

Concept Explanation

Replication and sharding are routinely discussed as alternatives, and they are not. They scale orthogonal dimensions and solve different problems, and a production system of any size uses both. Replication makes copies of the same data on multiple machines, which scales read throughput and provides redundancy, since another copy can take over when one fails. Sharding splits different data across machines, which scales write throughput and total dataset size, since each machine holds and accepts writes for only part of the whole. Replication does not increase write capacity, because every write must eventually be applied on every replica, so the write throughput of the system is still bounded by what a single machine can absorb. Sharding does not provide redundancy, because losing a shard loses that portion of the data outright unless it is also replicated.

That gives a clear decision rule. If reads are the bottleneck, add replicas, which requires no change to the data model, no shard key, and no query restrictions. If writes or dataset size are the bottleneck, shard, and accept the constraints the previous lessons described. If availability matters, which it always does, replicate regardless. In a full production topology every shard is itself a small replicated cluster with a primary and one or more replicas, so the fleet is a two-dimensional grid: shards along one axis, replicas along the other. A system with 16 shards and 3 copies each runs 48 database instances, and both numbers are tuned independently for different reasons.

The central difficulty replication introduces is lag. Asynchronous replication, which is the default in most systems because synchronous replication makes every write pay the latency of the slowest replica, means a replica is always some amount of time behind its primary. Usually that is milliseconds, but it grows under write bursts, during large batch operations, and while a replica is catching up after a restart. The user-visible consequence is read-your-writes violations: a user updates their profile, the write commits on the primary, the subsequent read is routed to a replica that has not received it yet, and the user sees their old data and concludes the save failed. The standard mitigations are to route reads to the primary for a short window after a write by that user, to track the write position and require any serving replica to have caught up past it, or to serve the value from the client session rather than re-reading it. Sticky routing to one replica also helps within a session by providing monotonic reads, which prevents the even more confusing case where consecutive reads appear to travel backwards in time because they hit different replicas.

Replication topologies differ in what they permit. Single-primary replication, where one node accepts writes and the rest are read-only copies, is by far the most common because it avoids write conflicts entirely; its limits are that write capacity is bounded by one node and that failover requires electing a new primary, with the attendant risk of split brain if two nodes both believe they are primary. Multi-primary replication allows writes at several nodes, which helps with geographic write latency, and pays for it with conflict resolution, since two nodes can accept conflicting updates to the same row and something must decide the outcome. Quorum-based replication, used by Dynamo-style systems, writes to W replicas and reads from R of N, and configuring W plus R greater than N guarantees that any read overlaps any write and therefore sees the latest value, which is a tunable knob between consistency and availability rather than a fixed property.

The availability arithmetic is worth doing explicitly, because it is not intuitive. Sharding without replication makes availability worse: with 10 shards each independently available 99.9 percent of the time, a request needing any one specific shard is fine, but the probability that all 10 are simultaneously up is 0.999 to the tenth power, about 99 percent, so a system-wide operation is an order of magnitude less reliable than a single database. Adding replicas fixes this by making each shard itself highly available, so the per-shard failure probability drops enough that the product over all shards remains acceptable. The general lesson is that as you add shards, per-shard availability must increase to keep total availability constant, which means replication is not optional at scale but a consequence of sharding.

DimensionReplicationSharding
Scales read throughputYes, nearly linearly with replica countOnly incidentally, by splitting the working set
Scales write throughputNo; every write lands on every copyYes, nearly linearly with shard count
Scales dataset sizeNo; every copy holds everythingYes; each shard holds a fraction
Provides redundancyYes; that is its primary purposeNo; losing a shard loses that data
Complicates the data modelNoSubstantially, through the shard key
IntroducesReplication lag and stale readsCross-shard queries and distributed transactions

Worked Example: Sizing a Grid and Closing the Read-Your-Writes Gap

The first part sizes the two axes independently from measured limits, which is how the decision should actually be made. The second shows the three standard ways to prevent a user from reading their own write back as stale.

Code
# --- Sizing the two axes from measured numbers, not intuition ---
# Measured: one instance sustains 8,000 reads/sec or 3,000 writes/sec,
# and holds 2 TB comfortably.
# Required: 240,000 reads/sec, 21,000 writes/sec, 26 TB of data.

# Shards are determined by writes and size (replication cannot help):
#   writes: 21,000 / 3,000 = 7        -> at least 7
#   size:   26 TB   / 2 TB  = 13      -> at least 13
#   shards = 16 (round up, power of two, leaves headroom)

# Replicas are determined by reads and by the availability target:
#   reads per shard: 240,000 / 16 = 15,000/sec
#   15,000 / 8,000 = 1.875            -> 2 read-serving copies
#   plus 1 for redundancy during a failure or a rebuild
#   copies = 3 per shard (1 primary + 2 replicas)

# Fleet = 16 shards x 3 copies = 48 instances.
# Note the two axes were sized from DIFFERENT constraints. A team that
# only knows "we need more capacity" cannot choose between them.

# --- Availability arithmetic ---
# Unreplicated, per-instance availability 99.9%:
#   P(all 16 shards up) = 0.999^16 = 98.4%   <-- WORSE than one database
# Replicated 3x, shard up unless all 3 copies are down:
#   P(shard down) = 0.001^3 = 1e-9
#   P(all 16 shards up) = (1 - 1e-9)^16 ~ 99.9999998%
# Sharding without replication actively reduces availability. This is the
# arithmetic that makes replication mandatory at scale rather than nice.

# --- Read-your-writes: three ways to close the gap ---

# 1. Read-from-primary window (simplest, coarse)
on_write(user_id):
    session.primary_until = now() + 3s
route_read(user_id):
    return primary if now() < session.primary_until else pick_replica()
# Cost: a burst of primary reads after every write. Fine at low write rates.

# 2. Write-position tracking (precise, the right answer at scale)
on_write(user_id):
    lsn = primary.commit_lsn()        # monotonic log position
    session.min_lsn = lsn
route_read(user_id):
    r = pick_replica_with(applied_lsn >= session.min_lsn)
    return r or primary               # fall back if none has caught up
# Cost: the client must carry a token; the router must know replica
# positions. Serves reads from replicas the instant they are fresh enough.

# 3. Monotonic reads via sticky replica (prevents time travel)
route_read(user_id):
    return replica[hash(session_id) % replica_count]
# Does NOT give read-your-writes, but does guarantee a session never sees
# data go BACKWARDS, which is the more confusing of the two symptoms.
A two-dimensional database fleet with shards along one axis and replicas along the other, annotated to show which axis absorbs write throughput and dataset size and which absorbs read throughput and redundancy, plus a timeline of a write committing on a primary and a read hitting a lagging replica.
A two-dimensional database fleet with shards along one axis and replicas along the other, annotated to show which axis absorbs write throughput and dataset size and which absorbs read throughput and redundancy, plus a timeline of a write committing on a primary and a read hitting a lagging replica.

Real-World Examples

  • MySQL and PostgreSQL both default to asynchronous replication for exactly the reason described here, and both offer a semi-synchronous or synchronous commit mode that trades write latency for the guarantee that at least one replica has the change before the client is acknowledged. The existence of the tunable in both engines, and the fact that the asynchronous mode is the default, reflects that most workloads prefer fast writes plus a small lag window over slow writes plus zero lag.
  • Amazon Dynamo 2007 paper introduced the quorum formulation now widespread in Cassandra and similar systems, where a write is acknowledged by W replicas and a read consults R of N, and configuring W plus R greater than N guarantees the read set overlaps the write set. The paper is explicit that this is a tuning knob rather than a fixed property, and that Amazon chose different settings for different services based on whether that service prioritized read latency, write latency, or durability.
Key TakeawaysSharding without replication makes your system less available, not more. Ten shards at 99.9 percent each give roughly 99 percent availability for any operation that needs all of them, which is worse than the single database you started with. Every shard must be its own replicated cluster, and as shard count grows, per-shard availability has to grow with it just to hold total availability steady.

Review Questions & Answers

Because every write must ultimately be applied to every replica in order to keep them consistent, so a replica does not absorb write work, it duplicates it. Adding a replica increases the total write work in the system rather than dividing it, and the write capacity of the whole cluster remains bounded by what the primary can accept and what each replica can apply. If a replica cannot keep up with the primary write rate, it falls progressively further behind and eventually becomes useless for reads as well, since its data is too stale to serve. The only way to increase write capacity is to have different machines accept different writes, which means either sharding, so each shard owns a disjoint set of rows, or multi-primary replication, which accepts writes in several places and pays for it with conflict resolution.

The write committed on the primary, and the subsequent read was routed to a replica that had not yet applied that change, so it returned the pre-write value. This is ordinary asynchronous replication lag rather than a bug, and it is most likely to be observed exactly here because the read follows the write by milliseconds. The simplest fix is a read-from-primary window: after a write, route that user reads to the primary for a few seconds. It is easy to implement and requires nothing from the database, but it sends a burst of read load to the primary after every write, which does not scale if writes are frequent. The more precise fix is write-position tracking: capture the log position of the commit, carry it in the session, and route reads only to replicas that have applied at least that position, falling back to the primary when none has. It requires the router to know replica positions and the client to carry a token, but it serves reads from replicas as soon as they are genuinely fresh enough, which preserves the read scaling the replicas were added for.

They relieve different constraints, so a single notion of capacity cannot determine both. Shard count is set by the constraints replication cannot help with: total write throughput divided by what one instance can absorb, and total dataset size divided by what one instance can hold. Replica count is set by read throughput per shard divided by what one instance can serve, plus whatever additional copies the availability target and the need to survive a failure during a rebuild require. A team that measures only aggregate requests per second has no way to choose between the two axes, and will typically add the one they are more comfortable with, which is how systems end up sharded when they needed replicas, carrying all the modelling constraints of sharding for a problem replicas would have solved without any.

When the number of replicas written to plus the number read from exceeds the total replica count, the read set and the write set must overlap in at least one replica, so the read is guaranteed to encounter at least one copy holding the latest value, and with versioning it can identify that copy. Relaxing the condition, for example writing to one replica and reading from one out of three, removes that guarantee: a read can be served entirely by replicas that have not yet received the write, so it returns stale data. What is bought in exchange is latency and availability. Lower write quorums mean a write acknowledges without waiting for slow replicas and succeeds even when some are unreachable, and lower read quorums mean reads are faster and survive more failures. The choice is per-operation in most systems, so a service can use a strict quorum for the few operations that need it and a relaxed one everywhere else.