FAISS

Also known as: Facebook AI Similarity Search

On this page 8

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.

IndexMethodMemory per vectorTypical use
IndexFlatL2 / IndexFlatIPExact brute-force searchFull size, 4 bytes per dimensionSmall sets, ground truth for evaluation
IndexIVFFlatInverted file with k-means cellsFull size plus list overheadMillions of vectors with high recall
IndexIVFPQInverted file with product quantizationOften 8 to 64 bytesHundreds of millions to billions of vectors
IndexHNSWFlatHierarchical navigable small world graphFull size plus graph linksLow-latency CPU search, frequent queries
IndexScalarQuantizer8-bit or 4-bit per dimensionOne quarter or one eighth of full sizeModerate compression with good accuracy
Common FAISS index families

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.

ParameterApplies toEffect of increasing it
nlistIVF indexesMore, smaller cells; faster scans but more training data needed
nprobeIVF indexes at query timeHigher recall, slower queries
M (PQ)Product quantizationMore bytes per vector, better accuracy
M (HNSW)Graph indexesMore links, higher recall and memory
efSearchHNSW at query timeHigher recall, slower queries
Key tuning parameters and their effects

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 is often compared with other ANN libraries and with full vector databases. The comparison below highlights where it fits.

ToolTypeStrengthLimitation
FAISSLibraryBroad index choice, GPU support, compressionNo built-in server or rich filtering
HNSWlibLibrarySimple, fast graph searchGraph indexes only, full-size vectors
AnnoyLibraryMemory-mapped read-only treesLower recall at equal speed
Vector databaseServicePersistence, filtering, access controlOperational overhead, less tuning freedom
FAISS and alternative vector search options

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.