FAISS (Facebook AI Similarity Search) is an open-source C++ library with Python bindings for efficient similarity search and clustering of dense vectors, developed by Meta’s research group. Among AI frameworks & tools, FAISS is the standard low-level engine for approximate nearest neighbor (ANN) search, the operation behind semantic search, recommendation candidate retrieval, deduplication and retrieval-augmented generation.
FAISS is a library, not a database: it builds and queries in-memory vector indexes but does not provide a server, user management, metadata filtering at scale or replication out of the box. Many vector databases and frameworks such as LangChain and LlamaIndex either wrap Faiss directly or reuse its indexing algorithms.
How FAISS finds similar vectors
FAISS answers one question: given a query vector, which stored vectors are closest to it under a chosen metric. It supports L2 (Euclidean) distance and inner product, and cosine similarity is obtained by normalizing vectors to unit length before using inner product.
Exact search compares the query with every stored vector, which is simple but costs time proportional to the collection size. FAISS speeds this up in two complementary ways: it partitions the space so that only a few regions are scanned, and it compresses vectors so that more of them fit in memory and distances are cheaper to compute.
All index types share the same small interface: train learns centroids or codebooks from a sample, add inserts vectors, and search returns the k nearest identifiers with their distances. Indexes can be combined through a compact index factory string, such as IVF4096,PQ64, which describes the partitioning and compression in one line.
Main FAISS index types
Choosing the right index is the central design decision when using FAISS. The table compares the most widely used families.
| Index | Method | Memory per vector | Typical use |
|---|---|---|---|
| IndexFlatL2 / IndexFlatIP | Exact brute-force search | Full size, 4 bytes per dimension | Small sets, ground truth for evaluation |
| IndexIVFFlat | Inverted file with k-means cells | Full size plus list overhead | Millions of vectors with high recall |
| IndexIVFPQ | Inverted file with product quantization | Often 8 to 64 bytes | Hundreds of millions to billions of vectors |
| IndexHNSWFlat | Hierarchical navigable small world graph | Full size plus graph links | Low-latency CPU search, frequent queries |
| IndexScalarQuantizer | 8-bit or 4-bit per dimension | One quarter or one eighth of full size | Moderate compression with good accuracy |
Product quantization (PQ) splits each vector into sub-vectors and encodes each part as a byte that points to a learned centroid. A 768-dimensional float vector occupies about 3 KB uncompressed, while a PQ code of 64 bytes stores it roughly 48 times smaller, at the cost of approximate distances.
Tuning speed, memory and recall
Every approximate FAISS index trades recall against latency and memory, and a few parameters control that trade-off. Recall is normally measured as recall at k against results from a flat index.
| Parameter | Applies to | Effect of increasing it |
|---|---|---|
| nlist | IVF indexes | More, smaller cells; faster scans but more training data needed |
| nprobe | IVF indexes at query time | Higher recall, slower queries |
| M (PQ) | Product quantization | More bytes per vector, better accuracy |
| M (HNSW) | Graph indexes | More links, higher recall and memory |
| efSearch | HNSW at query time | Higher recall, slower queries |
A common rule of thumb sets nlist near the square root of the collection size, for example about 4,096 cells for 16 million vectors, and trains with at least 30 to 256 vectors per centroid. The decisive step is always to measure recall on real queries rather than rely on defaults.
GPU acceleration and scaling
FAISS includes a CUDA-based GPU implementation for flat, IVF and several quantized indexes, which can be 5 to 20 times faster than an optimized CPU version for large batch queries. GPU memory is the limiting factor, so billion-scale collections usually rely on compression or sharding across several GPUs.
On CPUs, FAISS uses SIMD instructions and multithreading through BLAS and OpenMP. Indexes can be serialized to disk and memory-mapped, and sharded or replicated index wrappers distribute work across devices within one process; distribution across machines must be built by the application.
FAISS compared with related tools
FAISS is often compared with other ANN libraries and with full vector databases. The comparison below highlights where it fits.
| Tool | Type | Strength | Limitation |
|---|---|---|---|
| FAISS | Library | Broad index choice, GPU support, compression | No built-in server or rich filtering |
| HNSWlib | Library | Simple, fast graph search | Graph indexes only, full-size vectors |
| Annoy | Library | Memory-mapped read-only trees | Lower recall at equal speed |
| Vector database | Service | Persistence, filtering, access control | Operational overhead, less tuning freedom |
Teams commonly use FAISS directly when they need maximum control and throughput in a batch or embedded setting, and choose a vector database when they need live updates, metadata filters and multi-tenant access.
Limitations and common mistakes
- Metric mismatch: using L2 on embeddings trained for cosine similarity without normalization lowers result quality.
- Undertrained IVF indexes: training on too few vectors produces unbalanced cells and poor recall.
- Deletion cost: removing vectors is supported only for some indexes and can be slow; rebuilding is often simpler.
- Distribution drift: centroids learned on old data degrade as new embeddings arrive from a changed model.
- Unmeasured recall: deploying an approximate index without comparing it with exact search hides silent accuracy loss.
FAISS also stores only numeric identifiers, so the application must maintain a separate mapping from IDs to documents, products or images, and keep it consistent with the index.
In plain words
AI models turn text, images and sounds into lists of numbers. Similar things get similar numbers. FAISS is a fast finder for those number lists.
Imagine a huge warehouse of items with no labels. You hold one item and ask for the ten most similar ones. Checking every shelf would take forever. FAISS groups the shelves smartly and shrinks the items. It gives a very good answer in milliseconds, even with billions of items.
A real-world example
A legal research team builds a retrieval-augmented generation assistant over about 40 million paragraphs of court decisions. Each paragraph is embedded into a 768-dimensional vector, which would need roughly 120 GB of memory as raw floats.
The engineers choose an IVF16384,PQ96 index in FAISS, train it on two million sample vectors, and reduce the index to about 4 GB, small enough for one server. With nprobe set to 32, a query takes a few milliseconds, and recall at 10 against a flat index reaches about 93 percent on a test set of real lawyer questions.
To recover lost precision, the assistant retrieves the top 100 candidates and re-ranks them with exact vectors stored on disk. The result is a system that answers questions quickly on modest hardware while keeping citation quality close to exact search.