█

AI/TLDR

Buy me a coffee
Buy me a coffee

Annoy

Approximate nearest neighbor search with memory-mapped, shareable index files

Vector DatabasesOpen source
Language
Python
$pip install --user annoy
IN SHORT

What is Annoy?

Annoy (Approximate Nearest Neighbors Oh Yeah) is a C++ library with Python bindings, built at Spotify, that finds points in a vector space close to a given query point. It sits in the Vector Databases category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python. To get started: install from PyPI.

Overview

Annoy (Approximate Nearest Neighbors Oh Yeah) is a C++ library with Python bindings, built at Spotify, that finds points in a vector space close to a given query point. It builds a forest of trees over your vectors and answers approximate nearest-neighbor queries quickly.

What sets it apart is the index format: Annoy writes a read-only file that is memory-mapped (mmapped) into memory, so many processes can share the same data with a small footprint. Building the index is separated from loading it, so you build once and then pass the file around to load instantly in production, batch jobs, or other services.

It fits the embedded / in-process vector search category: there is no server to run. You add your vectors, build the index, save it to disk, and load it directly inside your application. Spotify uses it for music recommendations over millions of high-dimensional track and user vectors where memory usage matters.

What it does

  • Supports angular (cosine), Euclidean, Manhattan, Hamming, and dot (inner) product distance metrics
  • Indexes are static files that are mmapped into memory, so multiple processes can share one index
  • Index creation is decoupled from lookup: build once, then load the file quickly anywhere
  • Small memory footprint, designed for large high-dimensional datasets
  • Can build the index directly on disk so datasets that do not fit in RAM can still be indexed
  • Native Python bindings plus a header-only C++ library you include with annoylib.h

Getting started

Install Annoy from PyPI, then build an index, save it to disk, and load it to run nearest-neighbor queries.

Install from PyPI

Pull the latest Python package down from PyPI. For C++, clone the repo and include the header instead.

bashbash
pip install --user annoy

Build and save an index

Create an AnnoyIndex with a vector length and metric, add your items, build a forest of trees, and save the result to a file.

pythonpython
from annoy import AnnoyIndex
import random

f = 40  # Length of item vector that will be indexed

t = AnnoyIndex(f, 'angular')
for i in range(1000):
    v = [random.gauss(0, 1) for z in range(f)]
    t.add_item(i, v)

t.build(10) # 10 trees
t.save('test.ann')

Load and query

Loading just mmaps the saved file, so it is fast. Then ask for the nearest neighbors of an item.

pythonpython
u = AnnoyIndex(f, 'angular')
u.load('test.ann') # super fast, will just mmap the file
print(u.get_nns_by_item(0, 1000)) # find the 1000 nearest neighbors

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

When to use it

  • Serving item or user recommendations from precomputed embedding vectors, as Spotify does for music
  • Sharing one read-only index across many processes or workers to keep memory usage low
  • Distributing a prebuilt index file to production servers, Hadoop, or batch jobs that load it on startup
  • Indexing a dataset too large for RAM by building the Annoy index directly on disk

How Annoy compares

Annoy alongside other open-source vector databases tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
Supabase★ 116kManaged Postgres backend whose Vector toolkit (pgvector) stores, indexes, and queries embeddings next to transactional data.
Redis Cloud★ 77.8kFully-managed Redis with built-in vector search, offering low-latency similarity and hybrid queries over any embeddings.
Milvus★ 46.8kA distributed vector database for storing and searching billions of embeddings at scale, with multiple index types and Kubernetes-native deployment.
FAISS★ 42kA library from Meta for efficient similarity search and clustering of dense vectors, with both exact and approximate indexes.
Qdrant★ 35.8kA Rust-based vector search engine that stores embeddings with rich payload filtering for semantic search and recommendation systems.
Chroma★ 29.8kA developer-focused vector database designed for quickly building retrieval and RAG features with a simple Python and JavaScript API.
pgvector★ 23.9kA PostgreSQL extension that adds a vector data type and similarity search so you can store and query embeddings inside an existing Postgres database.
Annoy★ 14.3kApproximate nearest neighbor search with memory-mapped, shareable index files