02 / ENGINEERING CASE STUDY · Infrastructure
Distributed Vector Database
I built a C++20 vector search engine from the storage layer up, including distance kernels, an HNSW index, and distributed query coordination.
AT A GLANCE
- The problem
- Search high-dimensional vectors across shards while balancing speed, search quality, and memory use.
- What I built
- I implemented the HNSW index, SIMD distance kernels, and coordinator/worker search in C++20.
- The result
- Correctness tests cover 768D search, merging results from two local shards, and snapshot recovery.
01 / CONTEXT
The problem
Similarity search is easy to call through an API, but building the engine exposes the tradeoffs underneath it: search quality versus latency, memory layout versus portability, and local indexing versus distributed coordination. I built this project to understand those pieces by implementing them.
02 / SYSTEM DESIGN
Architecture
One query, shard-local searches, one ranked result
- Writes: hash(vector_id) % shard_count
- Each worker: aligned storage + local HNSW
- Transport: Protobuf contracts over gRPC
03 / TRADEOFFS
Engineering decisions
HNSW: spend search effort where it matters
I used HNSW to narrow the search through a multilayer proximity graph, then expand a bounded candidate set at the base layer. Search breadth controls how much work the engine does. The next step is to measure recall alongside latency: a fast answer is only useful if it retrieves good neighbors.
HNSW implementationSIMD with a portable fallback
I aligned vector buffers to 64 bytes for AVX-512 loads and added runtime dispatch for the L2 and cosine kernels. Supported x86 machines use AVX-512; other machines use scalar code. That lets me develop on a Mac while keeping the accelerated path available on compatible hardware.
Runtime distance dispatchHash writes; search every shard
I route single and batch inserts by hashing vector IDs, then send each query to all workers concurrently. This keeps write routing simple, but changing the shard count requires moving data. Cluster membership and rebalancing are still outside the current implementation.
Coordinator and worker interfacesExplicit RPCs and a simple top-k merge
I defined the search and write contracts in Protobuf and kept the merge straightforward: each shard returns up to k candidates, then the coordinator sorts the combined list and keeps the top k. Merge cost grows with the number of shards and candidates. The result is still approximate because each shard uses HNSW.
gRPC coordinator implementation04 / DEBUGGING
Aligned storage did not mean aligned queries
05 / VALIDATION
Evidence & measurement
The checked-in tests cover search correctness, local shard coordination, buffer alignment, and snapshot recovery.
768-dimensional vectors
The engine tests use the default 768D configuration, including a 128-vector synthetic nearest-neighbor fixture. That checks correctness; a performance benchmark needs a larger, representative dataset.
View supporting reference64-byte alignment
The storage test checks buffer alignment and padded stride; distance tests exercise L2 and cosine behavior.
View supporting referenceDistributed behavior
Tests with two local shards cover candidate merging, sorted results, and batch routing. Measuring networked gRPC throughput is still a separate task.
View supporting referenceRecovery and build scope
A snapshot round-trip test saves vectors and restores a searchable index. My documented testing used the scalar backend on Mac; I have not yet built and tested the gRPC targets in that environment.
View supporting referenceBenchmark notes
These reported figures do not have published benchmark runs. The repository does not document whether they came from the same configuration; they should not be read as results from a three-worker gRPC benchmark.
| Figure | What I need to document |
|---|---|
| <2 ms ANN latency | Dataset size, hardware, SIMD backend, index parameters, k, recall, latency percentiles, and warm-up policy. |
| 10K+ queries / second | Client concurrency, shard count, network setup, test duration, and latency and recall at that load. |
06 / NEXT ITERATION
What I’d improve next
Code links point to revision 4342b3b of my vector database.