Skip to Content

FAISS (Facebook AI Similarity Search)

When to use

Push the limits of in-memory or single-node vector search at billion-scale with a battle-tested C++ library and Python bindings.

Analogy

A barebones vector database. No persistence layer (you handle storage), no metadata (you handle filter). Just the fastest possible similarity search.

Data-flow diagram

  import faiss
  index = faiss.IndexFlatIP(d)        # brute force
  index.add(np.random.random((N,d)).astype('float32'))
  D, I = index.search(Q, k=10)        # distances, indices

Deep explanation

FAISS is the de-facto library for in-process vector search. IndexFlatIP/FlatL2 give brute-force (exact) search; IndexIVFFlat adds clustering for sub-linear time; IndexHNSW adds graph search; IndexPQ adds product quantization for compressed storage. Pick the index that matches your recall/memory target. Combine with DiskANN or storage layering for billion-scale.

Examples

Example 1

import faiss, numpy as np
xb = np.random.random((100000, 1536)).astype('float32')
xq = np.random.random((10,    1536)).astype('float32')
 
index = faiss.IndexFlatL2(1536)   # exact
index.add(xb)
D, I = index.search(xq, k=10)
print('top-10 indices per query:', I.shape)

IndexFlatL2 is brute force; great for ground-truth comparison and small datasets.

Example 2

# HNSW for sub-linear time
index = faiss.IndexHNSWFlat(1536, 32)   # M=32
index.hnsw.efSearch = 64
index.add(xb)
D, I = index.search(xq, k=10)

IndexHNSWFlat with M=32 is decent production default for p99 latency.

Example 3

# IVFPQ: compressed inverted index for billions
m = 16    # number of subquantizers
nbits = 8
quantiser = faiss.IndexFlatL2(1536)
index = faiss.IndexIVFPQ(quantiser, 1536, 100, m, nbits)
index.train(xb)         # IMPORTANT: train before add
index.add(xb)

IndexIVFPQ compresses vectors; memory drops 4-32x at small recall cost.

Common mistake

Forget index.train() on IVFFlat/IVFPQ. untrained indexes return garbage without centroids.

Key takeaway

FAISS = library, not database. Pick Flat for exact; HNSW for default production; IVFPQ for memory-tight billion-scale. Always train before add.

Production Failure Playbook

Failure scenario 1: untrained-ivfpq

Failure scenario 2: metric-mismatch