Inside FAISS: Billion-Scale Similarity Search
TL;DR Highlight
IVF skips most vectors and PQ compresses the rest, so a billion fit in RAM
Who Should Read
Backend developers using FAISS for RAG or semantic search who are unsure which index fits their memory and latency limits
Core Mechanics
- Brute force is impossible at a billion vectors
- A billion SIFT vectors (D=128, 4-byte floats) need 512 GB of RAM.
- Every query requires a billion distance computations.
- IVF scans only promising cells
- K-Means (clustering) splits the space into Voronoi cells.
- A query computes distances only in its own cell and a few neighbors.
- PQ shrinks each vector to 8 bytes
- A 128-D vector is cut into m=8 pieces, and each piece stores only the index of one of 256 centroids.
- That takes 512 bytes down to 8, a 64x compression.
- Splitting keeps the codebook at 128 KB
- A flat codebook for 64-bit codes needs 2^64 centroids, which is impossible to store, train, or search.
- Per-piece codebooks store only 2,048 centroids, 128 KB in total, small enough for L2 cache.
- Distances become table lookups and adds
- ADC keeps the query at full precision and precomputes per-piece distance tables, and it is the FAISS default.
- Each candidate costs m lookups and m-1 additions, with no multiplications or square roots.
- IVFPQ encodes residuals
- It compresses the difference from the coarse centroid instead of the raw vector.
- Residuals cluster near zero, so the same byte budget reconstructs better.
- On GPU, memory bandwidth is the bottleneck
- Copying the lookup table to shared memory cuts access from about 400 cycles to about 25.
- Sub-millisecond latency belongs to the GPU implementation.
Community Reactions
- Opinion that the original paper was not peer reviewed
- A commenter said reading such papers is a waste of time these days.
- They still praised the visualization.
- Experience that FAISS is great but packaging is lacking
- A commenter found FAISS great for certain use cases.
- They said it is not updated to work with most packages outside faiss-cpu.
- Question about why Meta cut maintenance
- A commenter asked whether it was due to speed or priorities.
- No answer was given.
- Praise for the interactive site
- Another comment thanked the author and called it a phenomenal interactive website.
How to Apply
- Compute your current index memory first
- Calculate n×D×4 bytes for your vector count n and dimension D to get Flat index memory.
- If it exceeds half your server RAM, shortlist IVF or IVFPQ.
- Compare Flat vs IVF on speed and recall
- Build IndexFlatL2 and IndexIVFFlat on about 100k real embeddings.
- Vary nprobe over 1, 8, 64 and record search time and agreement with Flat results.
- Switch to IVFPQ if memory is short
- Create an index with the factory string 'IVF1024,PQ8' and confirm codes are 8 bytes per vector.
- Compare against Flat results to decide if the accuracy drop is acceptable.
- Document your RAG search settings as dials
- Write one line each for latency, memory, and recall targets.
- Tune nprobe and PQ byte count to match them.
Try It
# Run in a terminal (pip install faiss-cpu numpy)
import time
import numpy as np
import faiss
d, n, nq, k = 64, 200_000, 100, 5
rng = np.random.default_rng(0)
xb = rng.random((n, d), dtype="float32")
xq = rng.random((nq, d), dtype="float32")
flat = faiss.IndexFlatL2(d)
flat.add(xb)
t = time.time(); _, I0 = flat.search(xq, k)
print("Flat", round(time.time() - t, 4), "s")
quant = faiss.IndexFlatL2(d)
ivf = faiss.IndexIVFFlat(quant, d, 1024)
ivf.train(xb); ivf.add(xb)
for nprobe in (1, 8, 64):
ivf.nprobe = nprobe
t = time.time(); _, I = ivf.search(xq, k)
rec = np.mean([len(set(I[i]) & set(I0[i])) / k for i in range(nq)])
print("IVF nprobe", nprobe, round(time.time() - t, 4), "s recall", round(rec, 3))
pq = faiss.index_factory(d, "IVF1024,PQ8")
pq.train(xb); pq.add(xb)
pq.nprobe = 8
_, I = pq.search(xq, k)
print("IVFPQ recall", np.mean([len(set(I[i]) & set(I0[i])) / k for i in range(nq)]))Terminology
Related Papers
FutureBench: A Renewable Framework for Evaluating Knowledge-Context Conflict in LLMs
Adding 'verify and prioritize the passage' to RAG prompts raised false-passage adoption; a knowledge-priority warning fixed it.
Faster Retrieval for Better, More Efficient RAG
Small neural nets learned query routing, hitting the same recall up to 8x faster in IVF search
Memorilla: Latent Semantic Memory for LLMs
Compressing documents into 16 memory tokens beat full-context accuracy on FactKG.
What the Author Wrote, What the Agent Reads: Silent Cue Loss in Enterprise Document Ingestion
Preserving red text and highlights in docx ingestion lifted marked-requirement F1 from 0.150 to 0.690.
Multimodal Reranking Is a System One Decision
One multiple-choice question to a VLM over 16 candidates reranks them, beating a dedicated 2B reranker.
You could have invented transformers from memory
Viewing linear, MLP and attention layers as memory stores: capacity goes ~64 to ~2,700 to over a million
Pointing AI at archives found a forgotten meteorite, lost rhinos, and more
Embedding search plus a cheap filter model surfaced unrecorded events in 400 years of archives
EmbeddingGemma 2: An open, lightweight multimodal embedding model
One open 740M model embeds text, images, audio and video into a single shared vector space