Sharding a Vector Store: Tenant Key, Split Only the Giants, Merge a Full Top-k, Grow With Fixed Buckets
A vector store holds 800 million chunks for 3,000 tenants and five of them hold 60 percent, all on one shard with 4x the p99 (synthetic). Shard by tenant, split only the giants, merge a full top-k from each shard, and grow with 1,024 fixed buckets so a ninth shard moves 11 percent instead of 89.
What you will learn
- Choose between a tenant shard key and a hash shard key and say what each costs
- Fix a hot shard by splitting only the giant tenants with tenant plus a hash
- Merge results across shards with a full top-k from each shard
- Explain why fan-out hurts the tail and why fixed buckets make resharding cheaper
Before you start
- Database Index for an AI App
- Replication and Replica Lag
Sharding means splitting your data across several machines, so no single machine has to hold all of it. Each machine's slice is a shard. It sounds simple, but the way you split decides which questions stay cheap and which ones get painful.
Five tenants, one hot shard
Our big-PDF analyst lets users upload huge manuals and contracts and ask questions with page citations. After a while the vector store holds 800 million chunks for 3,000 tenants. A tenant is one customer company. All the numbers here are synthetic.
The data is spread over eight shards. But five tenants hold 60 percent of all the chunks, 96 million each, and they all landed on shard three. Now shard three has four times the p99 of the others (p99 is the time that 99 percent of searches beat). Seven shards are relaxed, and one is drowning.
Your vector store holds 800 million chunks for 3,000 tenants, and five tenants hold 60 percent. Design the sharding.
- Shard by tenant, so a normal search touches one shard.
- Split only the giant tenants, by tenant plus a hash.
- When a search does ask several shards, each shard returns its own top k, and you merge them.
- Use many small fixed buckets, so growing moves a slice of the data, not all of it.
One line to remember: keep most questions on one shard.
A library across buildings
A city library got too big for one building, so the books are split across buildings by the author's surname. That rule is the shard key. You want a book by Smith? You walk into one building and you're done: a single-shard query.
You want the best books about tides? Every building has some. So each librarian picks her best ten, and you merge the lists. That's a fan-out with a top-k merge. And one famous author wrote so much that his shelves fill a whole building. That's a hot tenant.
Tenant key or hash key
With a tenant key, all of one tenant's chunks sit together on one shard. When that tenant searches, the query touches one shard. It's fast, and it keeps tenants apart, which also helps isolation (see isolation, cache and failures).
With a hash key, you hash each chunk's id and the chunks scatter evenly over all eight shards. The spread is perfect, but now every search for one tenant asks every shard.
Split only the giants
The five giants are the problem, not the other 2,995 tenants. Those average about 107,000 chunks each. Tiny. So leave the small tenants alone, one shard each, by tenant key. Split each giant by tenant plus a hash of the chunk, so its 96 million chunks spread over four shards.
The bars level out. A giant's search now asks four shards, not eight, and not all on one machine.
Merge a full top-k
Sometimes a search does need several shards. The query goes out to all eight, each shard returns its own top ten by similarity score, and that's 80 candidates. Sort those 80 by score and keep the best ten.
A common mistake is to ask each shard for ten divided by eight, so one or two each. But the best ten might all sit on one shard, and then you lose them. Each shard gives a full top k. You merge, then you cut. And the merge only works if every shard uses the same embedding model and the same distance, so the scores can be compared. The merge itself is a classic coding question, worked through in Top-k Merge and Rank Fusion.
The trap: hash everything
Weak candidates hash everything, because the spread looks perfect. But then every single search fans out to all eight shards, and a fan-out is only as fast as its slowest shard.
Say any one shard is slow 1 percent of the time. If a search asks all eight, the chance that at least one is slow is 1 minus 0.99 to the power of 8, about 7.7 percent. That's roughly eight searches in a hundred instead of one. An even spread can still give you a bad tail. The goal isn't an even chart. It's keeping most searches on one shard. For more on why tails grow, see Latency Math.
What resharding costs
One day eight shards aren't enough. If your rule is hash mod 8 and you change it to mod 9, about 89 percent of the chunks change shard. That's around 700 million chunks on the move.
The better way is fixed buckets. Say 1,024 of them, spread over the shards. A chunk always maps to the same bucket. To add a ninth shard, you move whole buckets to it: here 113 of them, about 11 percent, or around 88 million chunks. And you move them safely: copy a bucket, read from both and compare, and only then switch.
| Growing from 8 to 9 shards | Data that moves |
|---|---|
| hash mod 8 to hash mod 9 | about 89%, around 700 M chunks |
| 1,024 fixed buckets, move 113 | about 11%, around 88 M chunks |
Resharding is a migration, so plan for it on day one.
Why shard last
An index fixes slow lookups on one machine. A replica is a full copy: it takes extra reads and gives you failover. A shard is the only one that splits the data itself, and you need it when the data or the writes are too big for one machine. It brings weight with it: cross-shard queries, hot tenants and resharding. So index first, then replicas, and shard last, when one machine truly can't cope.
The numbers, and the test
Watch p99 and size per shard, not only the average across all of them. An average hides a hot shard. Watch the skew too, the largest shard compared with the median one. If that ratio climbs, a tenant is outgrowing its place.
For the failure test, make one shard slow, or take it away for a few minutes. It passes if tenants on the other shards don't notice, and a fan-out search returns what it has, marked as partial, instead of hanging. One sick shard should hurt a few tenants, not everyone.
- Tenant key or hash key? Tenant key for most tenants. Hash inside a tenant only when that tenant is too big.
- How do you merge top k across shards? Each shard returns its own full top k with comparable scores. Sort and cut once.
- What happens when a tenant outgrows a shard? Split it by tenant plus a hash, and move its buckets with copy, compare and switch, not with a big bang.
Recap
| Decision | What to say |
|---|---|
| Key | Tenant key, so most searches touch one shard |
| Hot tenant | Split only the giants by tenant + hash (here over four shards) |
| Fan-out | Full top k from every shard, same embedding and distance, merge once |
| Tail | 1% slow per shard becomes about 7.7% across eight |
| Grow | Fixed buckets: 11% moves instead of 89% |
Try it yourself
A sixth tenant is growing fast and now holds 40 million chunks on a shard it shares with 400 small tenants. Which two numbers would warn you first, and what would you move, in what order?
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.
