Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 11.7 — Scaling
RAG and Vector Search · Example 1 of 1

Shards, scatter-gather and replica sums

Scores are shared-word counts (the course's toy retriever), which are comparable across shards. The shard rule is a toy hash.

When the corpus or the traffic outgrows one machine, you split the index into shards (to hold more) and copy it into replicas (to answer more, and to survive failures). Every query then goes to every shard, and the results are merged.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write shard_of(doc_id, shards) returning the sum of the character codes of doc_id modulo shards. Print the shard of D1, D2 and D3 for 3 shards, then for 4 shards.

02

Write merge(parts, k) taking a list of lists of (score, doc_id) pairs and returning the best k pairs by score (highest first, ties by id). Try it on two parts.

Explain it without notes

01

What is the difference between sharding and replication, and which limit does each address?

02

How does scatter-gather search work, and when does it return the wrong top k?

03

How would you size a cluster for 120 questions a second when one replica handles 45?

04

Why does a RAG system's ingestion path need its own scaling plan?

Shards, scatter-gather and replica sums
Sign in to run this example in your browser.

Expected output

shards: 0=D1,D4,D10  1=D2,D8  2=D3,D6,D12
one big index, top 3: D10(7), D12(4), D6(3)
each shard returns its top 3:
  shard 0 returns D10(7), D1(1), D4(1)
  shard 1 returns D8(2), D2(0)
  shard 2 returns D12(4), D6(3), D3(2)
merged top 3: D10(7), D12(4), D6(3) | same as one big index: yes
each shard returns its top 1:
  shard 0 returns D10(7)
  shard 1 returns D8(2)
  shard 2 returns D12(4)
merged top 3: D10(7), D12(4), D8(2) | same as one big index: no
capacity: 120 questions a second, 45 per replica -> 4 replicas per shard (one spare), 12 machines