Sub topic 14
Updates, Deletes, Sharding and Other Operational Concerns
This chapter assumes you know how HNSW and IVF indexes work, what recall means, and how metadata filtering interacts with an index. It covers what happens after the first load: data changes, the index ages, the dataset outgrows one machine, and you have to keep the system running.
Why operations differ from a static benchmark
The earlier chapters built an index once over a fixed set of vectors and measured recall. A production system never stays in that state. Documents are edited, users delete their data, new embedding models are released, and traffic grows. Most approximate indexes were designed for fast reads over a mostly fixed dataset, so writes are where the trade-offs show up.
Four questions come up repeatedly:
- How does an insert become searchable, and how fast?
- What does a delete or update actually do to the index?
- How do you spread data across machines without losing recall?
- How do you change the index or the embedding model without downtime?
Inserts and freshness
Each index type handles new vectors differently.
- HNSW inserts a vector by searching the graph for its neighbors and linking it in. This works online, but each insert costs about as much as a search with
efConstruction, so heavy write loads are expensive. - IVF assigns the new vector to its nearest centroid and appends it to that list. This is cheap. The centroids were trained on the old data, though, so if the distribution shifts, some lists grow large and recall drops.
- Product quantization encodes the new vector with the existing codebooks. Like IVF, it assumes the training data was representative.
Many systems also use a write buffer. New vectors go into a small in-memory segment that is searched by brute force. A background process periodically merges the buffer into the main index. This keeps writes fast, and a query searches both the main index and the buffer, then merges the results.
Freshness is the delay between a write being accepted and it appearing in search results. Some databases make writes visible immediately. Others make them visible after a refresh interval or after a flush. Check which guarantee your system gives before you build a feature that depends on read-after-write behavior, such as a user uploading a document and searching for it right away.
Deletes and updates
Deletion is harder than insertion in graph indexes. If you remove a node from an HNSW graph, you may cut the paths that other nodes used to reach each other, and search quality falls.
Most systems avoid this with tombstones. A tombstone is a mark that says "this vector is deleted". The vector stays in the index, and the search skips it when building results. The space is reclaimed later by a compaction or rebuild step.
An update is usually a delete followed by an insert. If you change the text of a document, you must compute a new embedding and replace the old vector. Changing only metadata may be cheaper, depending on the system, because the vector and graph do not change.
Tombstones have costs you should watch:
- Deleted vectors still occupy memory and disk until compaction runs.
- Searches still traverse tombstoned nodes in HNSW, so latency can rise.
- With a filter or a small
k, a search may visit many deleted entries and return fewer thankresults. - If you must delete data for legal reasons, a tombstone is not enough. You need to confirm the data is physically removed after compaction, including from backups and replicas.
Compaction and index rebuilds
Over time, the index accumulates tombstones, small segments, and drift between the data and the trained structures. Two maintenance jobs address this.
Compaction merges small segments and drops tombstoned vectors. It uses CPU, memory and disk bandwidth, so schedule it away from peak traffic or limit its resources.
Rebuilds recreate the index from the raw vectors. You need one when:
- IVF centroids no longer match the data. A common sign is that recall falls over time at fixed
nprobe. - You change index parameters such as HNSW
MorefConstruction, which cannot be changed in place. - You change the distance metric or the vector dimension.
- You switch to a new embedding model. Vectors from different models live in different spaces and must never be mixed in one index.
Always keep the raw vectors, or the source text, in durable storage. If you only have a compressed index, you cannot rebuild it.
Changing the embedding model
A new model means re-embedding the whole corpus. Do it without downtime using a blue-green approach:
- Create a new collection or index for the new model.
- Backfill it by re-embedding all documents in batches.
- Send new writes to both collections while the backfill runs.
- Compare quality on a test set of queries, using the recall and relevance measures from the evaluation chapter.
- Switch query traffic to the new collection, then delete the old one after a safe period.
Embedding the corpus is often the largest cost in the migration, more than the indexing itself. Estimate it before you commit.
Sharding and replication
When a dataset no longer fits in one machine's memory, or one machine cannot handle the query rate, you split the work. These are separate problems with separate tools.
- Sharding splits the data into parts, and each shard holds a subset of the vectors. It addresses data size.
- Replication keeps copies of each shard. It addresses query throughput and availability.
With random or hash-based sharding, a query is sent to every shard. Each shard returns its local top k, and a coordinator merges the lists into a global top k. This is exact with respect to the per-shard results, since the global top k is always contained in the union of the shard top-k lists.
import heapq
def merge_shard_results(shard_results, k):
"""shard_results: list of lists of (distance, vector_id), one per shard.
Smaller distance is better. Returns the global top k."""
all_hits = (hit for shard in shard_results for hit in shard)
return heapq.nsmallest(k, all_hits)
shards = [
[(0.10, "a1"), (0.40, "a2")],
[(0.05, "b1"), (0.50, "b2")],
[(0.20, "c1"), (0.30, "c2")],
]
print(merge_shard_results(shards, 3))
# [(0.05, 'b1'), (0.1, 'a1'), (0.2, 'c1')]The cost is that every query touches every shard, so latency is set by the slowest shard, and adding shards does not reduce work per query unless each shard is also smaller. Each shard's index is smaller and faster, but the fan-out and merge add overhead. Tail latency (the slowest few percent of requests) grows with shard count, because one slow shard slows the whole query.
An alternative is partitioning by a meaningful key, such as tenant, region or IVF cluster. A query that includes the key is routed to one shard. This cuts work a lot, but it can make shards uneven. One large tenant can overload a shard, which is called a hot shard.
| Strategy | Query fan-out | Balance | Best for |
|---|---|---|---|
| Hash or random sharding | All shards | Even | One large shared corpus |
| Partition by tenant or key | One or few shards | Can be skewed | Multi-tenant apps, filtered queries |
| Cluster-based (IVF-style) | A few shards | Depends on data | Very large corpora with tolerance for lower recall |
Cluster-based routing reduces fan-out, but it inherits the IVF recall problem: a true neighbor in an unprobed shard is missed.
Memory, disk and cost
Index choice drives the hardware bill.
- A full-precision HNSW index needs the vectors and the graph in RAM for good latency. Memory is roughly
n * d * 4bytes for float32 vectors, plus graph links of aboutn * M * 2 * 4bytes or more. - Quantization cuts vector memory by a large factor, at a cost in recall that you recover by re-scoring top candidates with the original vectors.
- Disk-based indexes keep compressed vectors in RAM and full vectors on SSD. They cost less per vector but have higher and more variable latency.
For example, 100 million vectors of 768 dimensions in float32 take about 307 GB for the vectors alone. Plan the capacity before you choose, not after the first out-of-memory error.
Consistency, backup and monitoring
Replicas may lag the primary. A query sent to a lagging replica can miss recent writes or still return deleted items. If deleted content must never appear, route sensitive queries to the primary or check deletions against the source database before returning results.
For backups, take regular snapshots of the index, and also keep the raw data and metadata. Test a restore. A snapshot you have never restored is an untested assumption.
Monitor these signals:
- Query latency at p50, p95 and p99, not only the average.
- Recall on a fixed set of test queries, checked on a schedule against an exact baseline. Recall can degrade silently as data changes.
- Index size, tombstone ratio and segment count.
- Memory use, and the lag between primary and replicas.
- Ingestion rate and the time until writes become searchable.
Common mistakes
- Treating a delete call as proof that data is gone, when it has only been tombstoned.
- Never measuring recall after the initial build, so drift goes unnoticed.
- Adding shards to fix latency when the cause is a single hot shard or a slow disk.
- Keeping only the index and not the source data, which makes rebuilds and model changes impossible.
- Tuning for average latency and ignoring tail latency.
Exercise
A team deletes 30% of the vectors in an HNSW collection. Memory use stays the same and p99 latency rises slightly. Explain why, and name the maintenance step that fixes it.
Show answer
The system marks the deleted vectors with tombstones instead of removing them from the graph, so they still use memory and searches still traverse their nodes. Running compaction (or rebuilding the index) removes them physically and restores memory use and latency.
Exercise
A corpus is split across 8 hash-based shards, and each query asks for the top 10. What does each shard return, and why is the merged result correct with respect to what the shards searched?
Show answer
Each shard returns its own top 10. Any vector in the global top 10 must also be in the top 10 of the shard that holds it, so it appears in the union of the 80 candidates. The coordinator picks the 10 smallest distances from that union.
Exercise
You are moving from embedding model A to model B, both with 768 dimensions. Why can't you simply insert the new vectors into the existing index, and what is the safer procedure?
Show answer
Vectors from different models are in different spaces, so distances between them are meaningless, and recall would degrade silently. Build a new collection for model B, backfill it by re-embedding the corpus, dual-write new data, validate quality on test queries, then switch traffic and delete the old collection.