Scaling a Vector Index: Shards and Merging
You'll learn to
- -Estimate the memory of a vector index from the count, dimension and bytes per value
- -Explain scatter-gather search over shards and why each shard must return at least k results
- -Compute how fan-out across shards raises the chance a query waits for a slow shard
- -Choose between more shards, compression and replicas
Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.
A vector index that holds a few hundred thousand vectors runs happily on one machine. At a hundred million it does not. The memory needed is the first wall you hit, and the answer, splitting the index into shards, brings a new problem: a query must visit every shard and the slowest one decides how long the user waits.
An Analogy: A Library With Branches
A library too large for one building opens four branches and divides the books among them. To find the best five books on a topic, you ask each branch for its best five, then compare the twenty and keep the top five. You never need to look beyond those twenty. But you cannot start reading until the slowest branch has answered, and if you only asked each branch for its single best, you might miss a branch's second book that beats everyone else's first.
How Big Is the Index?
A hundred million vectors of 768 dimensions stored as 32-bit floats is 307.2 GB. Storing each value as an 8-bit integer is 76.8 GB. Compressing each vector into a 64-byte code, as product quantization does, is 6.4 GB, at some cost in accuracy. The code works out how many 64 GB servers each needs, keeping 30 percent free.
The first block checks the merge. Four shards each return their top 5 of 100 vectors, the 20 candidates are merged, and the final top 5 is exactly the answer a single index gives. The next line shows what goes wrong if you ask each shard for fewer than k: with only one result per shard the answer differs from the true one, because a shard's second best vector can beat the other shards' first. Each shard must return at least k.
Then the memory. In float32, the index needs 7 servers of 64 GB at 70 percent use. Quantized to 8-bit integers it needs 2, and with 64-byte compressed codes it fits in 1. Compression is often a better first move than more shards, because it saves memory and often speeds up each distance calculation, at some loss of recall that you must measure.
The Cost of Fan-Out
If each shard is slow for 1 percent of queries, a query that touches N shards is slow whenever any one of them is, which has probability 1 minus 0.99 to the power N. At 8 shards that is 7.7 percent of queries, at 32 shards 27.5 percent, and at 64 shards 47.4 percent. More shards means the tail gets worse even though every shard is just as healthy. This is the tail latency problem.
- -Use as few shards as the memory and throughput allow, and compress before you split.
- -Set a per-shard timeout and return partial results, flagged as partial, when a shard is slow.
- -Send a hedged request to a replica of a shard that is taking too long.
- -Add replicas of each shard for throughput and availability: shards divide the data, replicas multiply the capacity.
- -Partition by tenant or another metadata field when queries only touch part of the data, so most queries visit one shard.
When someone asks how you would scale a vector database, answer in this order: size the memory, compress, shard if still needed, add replicas for throughput, then deal with tail latency and rebalancing. Consistent hashing, from the HLD course, is the usual way to add shards without moving everything.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!