IVF Index: Clustering-Based Vector Search & Partitioning › Choosing the Number of Centroids

Michael BrenndoerferJanuary 27, 20265 min read
Language AI HandbookAll chapters
Part 44 of 67Retrieval-Augmented Generation
Chapter 340IVF Index: Clustering-Based Vector Search & Partitioning › Choosing the Number of Centroids
Reading section 6 of 12 · Choosing the Number of Centroids · 5 minSection 6 of 12 · 5 min

Book contents

Section 6 of 12 · 5 min read

IVF Index: Clustering-Based Vector Search & Partitioning › Choosing the Number of CentroidsOne section · Sections in this chapter6 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The number of centroids kk affects both the granularity of the partitioning and the overhead of the centroid comparison step. To develop intuition for what happens when kk is set poorly, it helps to examine the two extremes. The choice involves several considerations.

Too few centroids (e.g., k=10k = 10 for 1 million vectors):

When the number of centroids is much too small relative to the dataset size, each cell becomes enormous. Each cell contains approximately:

nk=1,000,00010=100,000 vectors per cell\frac{n}{k} = \frac{1{,}000{,}000}{10} = 100{,}000 \text{ vectors per cell}

where:

  • n=1,000,000n = 1{,}000{,}000: the total number of vectors in the dataset
  • k=10k = 10: the number of centroids (cells)
  • n/kn/k: the average number of vectors per cell

With such large cells, even a single probe requires scanning 100,000 vectors, which offers minimal speedup over brute force. The cell is too large to narrow the search space enough to help.

Even with nprobe=1\texttt{nprobe} = 1 (searching just one cell), you scan:

1k=110=10% of the dataset\frac{1}{k} = \frac{1}{10} = 10\% \text{ of the dataset}

where:

  • 1/k1/k: the fraction of cells searched when nprobe=1\texttt{nprobe} = 1, approximating the fraction of the dataset examined
  • k=10k = 10: the number of centroids in this example

The centroid comparison step requires only k=10k = 10 distance computations, which is cheap. However, the list scanning step dominates since you must compute distances to 100,000 vectors. The problem is clear: the savings from coarse filtering are negligible because the cells are too coarse to filter effectively.

Too many centroids (e.g., k=100,000k = 100{,}000 for 1 million vectors):

At the opposite extreme, when we create far too many centroids, the cells become tiny and the overhead of simply finding the right cells becomes a bottleneck. Each cell contains approximately:

nk=1,000,000100,000=10 vectors per cell\frac{n}{k} = \frac{1{,}000{,}000}{100{,}000} = 10 \text{ vectors per cell}

where:

  • n=1,000,000n = 1{,}000{,}000: the total number of vectors
  • k=100,000k = 100{,}000: the number of centroids, an excessive number for this dataset
  • n/k=10n/k = 10: the average number of vectors per cell, too small for efficient search

With cells this small, the true nearest neighbors are likely scattered across many cells, requiring a large nprobe\texttt{nprobe} to reach good recall. The reason is geometric: when cells are very small, even a slight offset between the query and the nearest neighbor can place them in different cells. Additionally, comparing the query to 100,000 centroids itself becomes a bottleneck.

The centroid comparison step itself becomes expensive, and you need a very large nprobe\texttt{nprobe} to avoid missing nearby cells that contain true neighbors. The cost of Step 1 (finding nearest centroids) may even exceed the savings from Step 2 (scanning smaller lists) and defeat the purpose of the index entirely.

Out[16]:
Visualization
Log-log plot of IVF query cost components versus number of centroids showing total cost curve with minimum at the optimal k, alongside centroid comparison and list scanning cost components.
Optimization of centroid count for IVF search on a one-million-vector dataset with nprobe=10. The log-log plot decomposes total query cost (solid line) into centroid comparisons (blue dashed line, growing with k) and list scanning (orange dashed line, decreasing as 1/k). Total cost reaches its minimum at k=3,162, equal to sqrt(n times nprobe), where the two components balance. A muted vertical line marks the common k=sqrt(n)=1,000 reference.

The right choice of kk lies between these extremes, at a point where both the centroid comparison cost and the list scanning cost are manageable. In practice, the guidelines for choosing kk are:

  • Small datasets (n<100,000n < 100{,}000): k=256k = 256 to 1,0241{,}024
  • Medium datasets (n≈106n \approx 10^6): k=1,024k = 1{,}024 to 4,0964{,}096
  • Large datasets (n≈108n \approx 10^8 to 10910^9): k=16,384k = 16{,}384 to 65,53665{,}536

FAISS documentation recommends starting with kk between 4n4\sqrt{n} and 16n16\sqrt{n} and tuning from there based on your recall and latency requirements.