Overview
Fromkora-vector/src/lib.rs:1-12:
Vector similarity search for the Kōra cache engine. This crate provides an in-process, approximate nearest neighbor (ANN) index based on the HNSW algorithm. Components are designed to live inside a single shard with no cross-thread synchronisation requirements.
Features
- HNSW algorithm for fast approximate nearest neighbor search
- Three distance metrics: Cosine, L2 (Euclidean), Inner Product
- Configurable parameters: M (connections per layer), ef_construction (search width)
- Lazy deletion without graph repair for performance
- Single-threaded design for zero-lock access within shards
Distance Metrics
Fromkora-vector/src/distance.rs:1-35:
Cosine Distance
1 - cosine_similarityBest for normalized embeddings where magnitude doesn’t matter.L2 Distance
Euclidean distanceBest for geometric data where absolute positions matter.
Inner Product
Negative dot productBest for maximum inner product search (MIPS).
All metrics are normalized so lower values mean more similar vectors. Use the same metric for indexing and querying.
Commands
Store Vectors
Query Similar Vectors
Delete Vectors
HNSW Index Architecture
Fromkora-vector/src/hnsw.rs:1-16:
Implements the algorithm from Malkov & Yashunin (2018) for approximate nearest neighbor search with logarithmic query time and high recall. Key design decisions:
- Single-owner, no locking. The index is meant to live inside one Kōra shard worker and is accessed through
&mut self/&self. No interior mutability or atomics are needed.- Lazy deletion. Nodes are marked as deleted without removing them from the graph, keeping neighbour connectivity intact and avoiding expensive edge repair.
- Deterministic level generation. A simple xorshift64 PRNG seeded at construction produces reproducible layer assignments.
Parameters
Maximum number of connections per node per layer. Higher values improve recall but increase memory and build time.Recommended values: 8-32
Search width during index construction. Higher values improve index quality but slow down inserts.Recommended values: 100-400
Search width during queries. Higher values improve recall at the cost of latency.Recommended values: 50-200
Usage Examples
Semantic Document Search
Product Recommendations
User Similarity
Implementation Details
Insert Performance
Fromkora-vector/src/hnsw.rs:129-164:
Search Performance
Time Complexity: O(log N × ef_search) Space Complexity: O(N × M × layers) where average layers ≈ log(N)Distance Computation
Fromkora-vector/src/distance.rs:37-68:
Current implementations use scalar loops. SIMD intrinsics can be added for specific architectures when profiling warrants it.
Performance Tips
Choose the right metric
Choose the right metric
- Cosine: Best for text embeddings (already normalized)
- L2: Best for image features, spatial data
- Inner Product: Best for learned embeddings optimized for MIPS
Tune M and ef_construction
Tune M and ef_construction
Normalize embeddings for Cosine
Normalize embeddings for Cosine
Pre-normalize vectors before indexing to avoid repeated normalization:
Batch inserts when possible
Batch inserts when possible
While each insert is independent, batching reduces network round-trips:
Monitor index size
Monitor index size
Memory usage ≈
N × D × 4 bytes + N × M × layers × 8 bytesFor 1M vectors (128-dim, M=16):Benchmarks
Fromkora-vector/benches/hnsw.rs:
- Insert
- Search
| Vectors | Dimensions | M | ef_construction | Time/Insert |
|---|---|---|---|---|
| 10K | 128 | 16 | 200 | 0.8 ms |
| 100K | 128 | 16 | 200 | 1.2 ms |
| 1M | 128 | 16 | 200 | 1.8 ms |
Next Steps
Document Database
Combine vector search with JSON documents
Change Data Capture
Stream vector updates to downstream systems
Benchmarks
See detailed performance metrics
API Reference
Complete VEC* command reference