Replicas help reads, not writes

Each primary fans every write out to all in-sync replicas before acking the client, so adding replicas linearly adds read capacity and HA but does not increase write throughput.

Scene 06

Replicas help reads, not writes

  1. Watch
  2. Try it
  3. Predict
  4. Capture
Indexing c…_id=DDIAIndexing c…_id=Distrib…Indexing c…_id=Old Man…Indexing c…_id=Lucene …Indexing c…_id=Cat in …hash(_id)mod 5PARTITIONS · 5Shard 0primary + 1 replicaShard 1primary + 1 replicaShard 2primary + 1 replicaShard 3primary + 1 replicaShard 4primary + 1 replicaREAD THROUGHPUT · CLUSTER GREENload-balanced across every copyReads100k ops/sec █████WRITE THROUGHPUTfan-out to every in-sync replica before ackWrites47k ops/sec █████████
What to watch for

Five primary shards, one replica each. Indexing clients send writes to the primary, which fans them out to every replica before acknowledging. Reads can hit any copy — primary or replica — picked by adaptive_replica_selection. Cluster status is GREEN.

Continue unlocks when the animation finishes.
Implementation

Highlighted lines are the ones running in the diagram right now.

Primary.index
fan out to every in-sync replica, then ack
def index(doc):
shard = hash(doc._id) % num_primary_shards
primary = routing_table.primary_for(shard)
primary.applyLocally(doc)
acks = []
for r in primary.in_sync_replicas:
acks.append(r.send(doc)) # parallel fan-out
wait_all(acks) # block on slowest
return ok(client) # ack now
Coordinator.search
pick any copy per shard via adaptive_replica_selection
def search(query):
for shard in all_primary_shards:
copies = primary + in_sync_replicas
copy = adaptive_replica_selection(copies)
scatter(copy, query)
# reads scale linearly with (1 + num_replicas)

Where this sits in Build a distributed search engine (Elasticsearch / OpenSearch style)

Scene 06 of 12. Replicas linearly add read capacity and HA but do not speed up writes — every primary fans every write out to all in-sync replicas before acking.

All 12 scenes in Build a distributed search engine (Elasticsearch / OpenSearch style) · Every curriculum

Built with Arqly
Every scene in Build a distributed search engine (Elasticsearch / OpenSearch style) builds on the one before it.All 12 Build a distributed search engine (Elasticsearch / OpenSearch style) scenes