Copies, shards, and the route a request takes

Trace how a distributed store places rows, chooses replicas, and trades write acknowledgement for replica freshness. Distributed data and replication · Databases.

A distributed database can look like one table while its rows live on several machines. That illusion is useful only when the system can answer two questions for every request: which shard owns this key, and which replica is safe to use? Keep those questions separate and the design becomes easier to reason about.

One logical table, several owners

Start with an invented profiles table whose key is user_id. Partitioning, often called sharding, divides the rows into disjoint ownership groups. Shard A might own users 1–1,000 and shard B users 1,001–2,000. A request for user 1,240 is routed to B because the partition map says B owns that key. Partitioning spreads data and work; it does not create a second copy of user 1,240.

Replication answers a different question. A shard can have a primary copy and one or more replicas. The replicas hold the same shard's data so the service can survive a node failure, serve more reads, or place a copy closer to users. A deployment can therefore have four logical shards and three replicas per shard: twelve physical shard replicas, but four logical ownership groups. A replica is not another shard merely because it is on another machine.

One logical table, several owners

Read the steps as text

  1. The profiles map assigns user_id 1..1000 to shard A and 1001..2000 to shard B. The request is for user 1240; B1 is at version 18 and B2 at version 17.
  2. The range map is now used before choosing a replica; replica freshness remains a separate fact.
  3. For the same range request, compare the placement choices. Range keeps nearby keys together but a hot interval can concentrate load; hashing can scatter the scan. A directory can move a hot key but requires routing metadata.
  4. A directory can reassign a hot key, but the router needs the current lookup entry to find its new owner. Range and hash still use their own placement rules.
  5. For this example B2 is the required synchronous replica. With B2 still at version 18, asynchronous success can already be returned; synchronous success waits for B2 to acknowledge version 19. The storage boundary is a separate contract.
  6. Ownership, placement strategy, and replica acknowledgement are separate boundaries. A reachable copy still needs the stated freshness contract.

The partition key should match the requests the service must route. Range placement keeps nearby keys together, which helps ordered scans and range queries, but a popular range can become a hotspot. Hash placement spreads keys more evenly when the hash function and key distribution cooperate, but a range query may touch many shards. A directory or lookup service can move ownership flexibly, but requests that need a directory lookup or current directory metadata depend on that routing state. None is universally best: the query shape and failure plan decide which cost matters.

Route before you read

Imagine a profile request arriving while shard B's primary is healthy and its replica R2 is one version behind. A primary-oriented route may read current data at higher cost. A nearest-replica route may answer faster but return the older version. The route is not a decoration around storage; it chooses which copy can satisfy the requested read contract.

Resharding makes this explicit. When ownership moves from B to C, the map needs a transition rule: which node accepts new writes, which copy is authoritative, and when the old owner can stop serving. A health check that merely says “node is up” does not prove that its replica has the required data. The route needs both ownership and freshness information.

The acknowledgement boundary

A write can update a primary in memory, append a durable record, and then propagate to replicas. Under synchronous replication, the service waits for the configured replica acknowledgements before reporting success. The exact number and storage boundary belong to the system's contract. Under asynchronous replication, the primary may acknowledge first and send the update later. The write can be accepted while another replica still serves the previous value; that interval is replication lag.

Async replication can improve latency and availability, but a primary failure during the lag window can lose the only current copy unless another recovery policy preserves it. Sync replication reduces that exposure only to the extent that the chosen acknowledgements and failure domain cover it. Neither word means “every node is always identical.” State the replica set, acknowledgement rule and failure model before claiming durability or freshness.

Delivery events after a shard move

Read the steps as text

  1. Customer C77 is moving from shard D to E. E1 has event version 42 and E2 has version 41; the tracking contract permits at most one version of staleness only when that result is declared.
  2. The map identifies E as the target. Both copies answer health probes, but E2 at version 41 cannot serve the strict version-42 read. A repair transfers the missing version before E2 can qualify.
  3. Repair has copied the authoritative event to E2 and the handoff fence now records version 42 for the new owner.
  4. The repair now records E2 at version 42 and the ownership fence points reads to E. The route must carry both facts.
  5. A handoff is complete only when ownership and freshness agree. A stale read can be allowed only under an explicit contract.

A second dataset: the delivery ledger

A delivery service stores events by customer_id. Suppose a tracking report spans customer_ids C77 through C88. Range placement keeps nearby customer histories together but can create a hot range for a large customer. Hash placement spreads customer keys, but that multi-customer range query can touch many shards. A directory can move a hot customer to a dedicated shard, but the move must coordinate reads, writes and the handoff point. If each shard has a lagging read replica, the service must decide whether a tracking page can show a stale event or must route to the primary.

The same reasoning works for a failure. If a replica is unavailable, the system may route to another replica, reject the read, or return a value with an explicit freshness bound. Those are product choices backed by a data contract, not automatic consequences of “using replication.”

Recap

  • Partitioning splits ownership; replication creates copies of each ownership group.
  • Range, hash and directory placement trade locality, spread and routing complexity.
  • Synchronous and asynchronous acknowledgements expose different lag and failure windows.
Why this stops before consistency theory

This lesson names the route and copy that produce a read. The next lesson asks what a client is promised when those copies disagree, including partition-qualified CAP reasoning and session guarantees. A distributed transaction can span shards, but atomic commit is a separate coordination boundary.

Next: Consistency promises when replicas disagree.

Practise the routing habit with Hash Map Fundamentals: a map gives one key-to-value lookup, while a distributed route must also state ownership, replica freshness and failure behavior.