All projects

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.
C++20gRPCProtobufAVX-512
March 2026 — Present

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

Client · search(query, k)
Coordinator · parallel fan-out
Shard 1 · HNSW
Shard 2 · HNSW
Shard 3 · HNSW
Coordinator · sort candidates, return top-k
  • Writes: hash(vector_id) % shard_count
  • Each worker: aligned storage + local HNSW
  • Transport: Protobuf contracts over gRPC
Logical search path. Three workers are shown to illustrate fan-out; the coordinator accepts a configured list of workers.

03 / TRADEOFFS

Engineering decisions

01

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 implementation
02

SIMD 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 dispatch
03

Hash 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 interfaces
04

Explicit 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 implementation

04 / DEBUGGING

Aligned storage did not mean aligned queries

What broke

I aligned stored vectors to 64 bytes, but incoming query buffers could begin at any address. The AVX-512 kernels used aligned loads for both operands, so a valid query could trigger a fault on the accelerated path.

The fix

I changed the L2 and cosine kernels to use unaligned loads. The storage layout stays aligned, while the distance functions accept query buffers without requiring callers to copy them first.

How I check it

The regression test offsets both buffers by one float and compares L2 and cosine results against known answers across 17 dimensions, covering a SIMD block and its scalar remainder. It uses runtime dispatch; testing the accelerated path still requires an AVX-512-capable machine.

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 reference

64-byte alignment

The storage test checks buffer alignment and padded stride; distance tests exercise L2 and cosine behavior.

View supporting reference

Distributed 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 reference

Recovery 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 reference

Benchmark 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.

Reported figures and the context needed to reproduce them
FigureWhat I need to document
<2 ms ANN latencyDataset size, hardware, SIMD backend, index parameters, k, recall, latency percentiles, and warm-up policy.
10K+ queries / secondClient concurrency, shard count, network setup, test duration, and latency and recall at that load.

06 / NEXT ITERATION

What I’d improve next

Measure search quality under load

I want to compare HNSW against an exact scan on fixed datasets, then measure how search breadth changes the tradeoff between recall, latency, and throughput.

Exercise the network boundary

Next, I need to build and test the gRPC services in a reproducible environment, then measure fan-out overhead and behavior under slow or failed workers.

Make recovery and scaling explicit

Persisting graph edges would avoid rebuilding the index on restore. After that, I want to explore replication and shard rebalancing. I built the current version to learn how the pieces fit together; fault-tolerant operation is still future work.

Code links point to revision 4342b3b of my vector database.

NEXT CASE STUDYLiftCircle