Vector Search in Go

- Backend Engineer
- November 2026
- Completed
In numbers
- Queries per second on 12 goroutines, recall@10 0.985
- 118,993
- Throughput of 12 goroutines against one, with no locks
- 8.75×
- Graph build on 8 workers, down from 0.79 s on one
- 0.11s
- Throughput of an exact scan, on 100,000 vectors
- 41×
About the Project
Vector Search in Go is the index behind semantic search, written from scratch to show where concurrency pays off and where it does not. It is HNSW, the graph that most vector databases use, in pure Go with nothing outside the standard library.
The build inserts vectors from several goroutines at once, with one mutex per node and an exclusive lock only when the top layer of the graph grows. Once the build ends, the index freezes: searches take no locks at all, and any later insert panics, so a reader can never meet a writer. Each search borrows its scratch space from a sync.Pool, and a small HTTP server answers POST /search behind a semaphore sized to the number of cores. Exact search sits next to it, as ground truth and as the baseline.
On BEIR SciFact, 5,183 abstracts embedded with bge-small at 384 dimensions, search throughput grew 8.75 times from 1 goroutine to 12, and 8 workers built the graph in 0.11 seconds instead of 0.79 with almost the same recall. Splitting one query across 8 graphs made it slower, so the index parallelizes between queries, not inside one. I built it in November 2026 as the Go concurrency counterpart to RAG Evals, which searches the same corpus with pgvector. The tests run with the race detector, including inserts and searches on the same index at the same time.
How it works
The path a request takes, step by step.
- Embeddings5,183 SciFact abstracts, bge-small, 384 dims
- Parallel buildGoroutines insert with one mutex per node
- FreezeThe graph stops changing; later inserts panic
- SearchConcurrent and lock-free, scratch from a sync.Pool
- HTTP APIPOST /search behind a semaphore
Decisions
The choices that shaped it, and what each one cost.
Freeze the index instead of locking reads
- A built graph never changes, and even a read lock writes to a shared cache line. Freezing it lets every search run without locks.
- No inserts after the build; new vectors mean a rebuild.
Parallelize between queries, not inside one
- Splitting one query over 8 graphs made it slower (71 → 93 µs) and lowered recall, because each shard repeats the beam search.
- One query is no faster on more cores; only throughput grows.
Pure Go float32, no SIMD
- The code stays readable and portable; the dot product is unrolled by eight instead.
- Quantizing to int8 would cut memory traffic 4×, and it is not measured yet.
Clustered synthetic data, not uniform
- Real embeddings cluster, and uniform random vectors make every index look worse than it is.
- The 100k figures are still synthetic and were run on a busy machine, so they are a lower bound.
