Requirements & constraints:
- 1B documents (~1–2 TB embeddings at 768–1536 dims float32; can quantize)
- 100k QPS/day ≈ 1.16 qps average, peak maybe 100–200 qps; P95 latency target 200ms end-to-end (embed + retrieval)
- Freshness: near-real-time updates within minutes
- Support hybrid keyword + vector scoring for precision
High-level architecture:
- Inference layer: client → request router → embedding service → retrieval cluster → fusion/rerank → response
- Storage: cold object store for raw docs, metadata DB (NoSQL), vector index store (SSD-backed)
- Monitoring, autoscaler, model/feature store
Embedding generation pipeline:
- Batch pipeline: nightly/full re-embedding using distributed workers (Spark/KFServing) writing embeddings to object store and enqueueing index-update tasks.
- Streaming pipeline: on create/update, generate embedding via low-latency model server (GPU-backed), write embedding to write-ahead log and metadata DB; enqueue async index update.
- Versioning: store model version and embedding schema to enable reindexing.
Index choice & config:
- Use FAISS IVF + PQ on SSD for storage-efficient approximate search, with HNSW as in-memory graph for high-recall hot shards. Reason: FAISS offers GPU/CPU IVF-PQ for billion-scale, fast disk-based search; HNSW is excellent for low-latency in-memory but memory-heavy for 1B.
- Hybrid: maintain HNSW per hot shard for sub-100ms in-memory queries and fallback to FAISS IVF-PQ on SSD for colder shards.
Sharding & replication:
- Shard by document-id hash into N primary shards sized ~10–50M vectors each (choose N=20–100 depending on memory). Keep hot shards in memory (HNSW), cold shards on SSD (FAISS).
- Replication: 2-3 replicas for availability. Use leaderless read routing or coordinator to query replicas in parallel and reduce tail latency.
- Query fanout: route queries to a subset of shards using routing hints (e.g., metadata filters) to reduce fanout; for full recall, parallel across all shards with async aggregation.
Freshness & index updates:
- Near-real-time: use write-ahead log + background mini-batches to perform incremental inserts into HNSW and IVF buffers; flush to disk periodically.
- Deletions: tombstones in metadata + lazy removal during compaction.
- Reindexing: support rolling reindex jobs per shard to apply PQ or model changes.
Hybrid keyword+vector search:
- Two-step fusion: filter candidates with inverted index (Elasticsearch/Opensearch) using boolean/metadata constraints and keyword score; then perform vector search on filtered subset (prefer local shard-level vector search) or combine top-K from both and rerank with cross-encoder.
- Score fusion: weighted sum or learned reranker.
Caching:
- LRU query-result cache at API layer for hot queries (queries per day distribution heavy-tail).
- Per-shard in-memory cache of recent top-K vectors and distances.
- Embedding model cache on GPU nodes (warm pools) to keep cold-start latency low.
Autoscaling & infra:
- Separate autoscaling for embedding workers (GPU pods) and retrieval nodes (CPU + NVMe). Scale embedding tier by queue depth and p95 latency; retrieval tier scale by CPU, disk IO, and query latency/SLO.
- Use graceful scale-in/out with warm-up: retain minimum hot shards in memory to preserve latency.
- Use circuit breakers and degrade path: if vector tier overloaded, fall back to keyword-only results.
Operational & trade-offs:
- Quantize vectors (FP16 or PQ) to reduce storage and I/O; this trades slight accuracy for cost.
- Use hybrid FAISS (disk) + HNSW (memory) to balance cost vs latency.
- Budget replicas and in-memory capacity to meet 200ms P95 under peak; simulate with load tests.
- Observability: trace embedding time, shard fanout, per-shard latencies, and recall metrics.
This design balances cost and latency for 1B docs, supports near-real-time freshness, and provides a practical hybrid retrieval + rerank pipeline suitable for RAG workloads.