Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Choosing a vector store and scaling it

After this lesson: I can choose a vector store for a workload and size its memory and shards.

60 min lesson2 commandsVector database lab: Qdrant and PostgreSQL with pgvector

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Postgres, a server or a service?

Choosing a vector database means matching a store to the data size, the queries and the team that will run it. Scaling means splitting the data across machines when one is not enough.

A college search of 2 million documents and a shopping site with 2 billion images need different answers. The wrong choice costs months, or a machine that never fills.

By the end of this lesson

Note

I can choose a vector store for a workload and size its memory and shards.

Key words

WordWhat it means
Workloadthe data and the questions a store must serve
Shardone part of a collection, on its own node
Replicaa copy of a shard for reads and safety
Fan-outsending one search to every shard
Managed servicea store somebody else runs for a fee

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Start from the workload, not the brand

Ask four questions. How many vectors, now and in two years? Do searches need filters, words or joins with other tables? Do you already run PostgreSQL or a cluster? Who will run the store at night?

A few million vectors beside existing tables often belong in pgvector. Heavy filtering, quantisation or many machines point to a vector database server. No team to run servers points to a managed service.

Seven stores compared

StoreWhat it isLicence or hostingOften chosen for
pgvectorAn extension for PostgreSQLOpen source, PostgreSQL licenceVectors beside other tables, with SQL
QdrantA vector database serverOpen source, Apache 2.0; also managedFiltered search, quantisation, sharding
ChromaA store inside a Python program or a small serverOpen source, Apache 2.0; also hostedPrototypes and small apps
FAISSA library for similarity search, not a databaseOpen source, MITBuilding your own search engine
PineconeA managed vector databaseA hosted service only, not open sourceTeams with no servers to run
WeaviateA vector database server with keyword search built inOpen source, BSD 3-Clause; also managedHybrid search with little setup
MilvusA distributed vector databaseOpen source, Apache 2.0; also managedVery large collections on many machines

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Match each store to what it is

Have a go

Loading…

Your answers are marked here and are not kept. An account keeps your marks.

Size the memory before you choose

Vectors as 4-byte numbers take vectors times numbers times 4 bytes. An HNSW graph adds about 2 times m links of 4 bytes for each point on layer 0. pgvector's HNSW index also keeps its own copy of each vector, so it is larger than the table.

A hundred million vectors of 768 numbers are about 307 GB as float32. As int8 they are about 77 GB, before the graph.

How much room the lab's data takes

Command
docker exec tools python3 steps/sizes.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: plan a hundred million vectors

Code: python
# Plan the memory for a collection before you buy the machines.
vectors = 100000000
numbers = 768
m = 16
full = vectors * numbers * 4
links = vectors * m * 2 * 4
gb = 1000000000
print("vectors as float32:", full // gb, "GB")
print("HNSW links on layer 0:", links // gb, "GB")
print("vectors as int8:", full // 4 // gb, "GB")
# Change m to 32. What happens to the links, and to the vectors?

Make a free account to save your work and track your progress.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Shards split the data, replicas copy it

When one machine cannot hold the data, the collection is cut into shards. Each point goes to one shard, often chosen from its id, and each shard can live on its own machine.

A search fans out to every shard, each returns its own top k, and the lists are merged. A replica is a copy of a shard: it serves more searches at once and survives a lost machine.

Cut a collection into four shards

Command
docker exec tools python3 steps/shards.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: 20,000 points in 4 shards

ShardPoints
04687
15991
24583
34739
All20000

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think sharding makes each search faster. Every search still visits every shard and waits for the slowest. Sharding lets the data fit; replicas let more searches run at once. For a faster single search, tune the index or quantise.

Towards billions of vectors

At billions of vectors, three moves work together. Quantise the vectors held in memory and keep the full ones on disk for rescoring. Shard across machines and replicate for traffic. Choose an index that can live partly on disk.

Qdrant can keep vectors and its graph on disk. Milvus offers a disk-based index called DiskANN. PostgreSQL can split a table into partitions, each with its own index.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: size and merge

Write four functions for planning a collection.

vector_bytes(n, dims, bytes_each) is the memory of n vectors of dims numbers. graph_bytes(n, m) is the memory of an HNSW graph's layer 0, counted as 2 * m links of 4 bytes for each point. shards_needed(total, per_shard) is the fewest shards of per_shard bytes that hold total bytes (round up). merge_top(lists, k) takes one list per shard, each of [score, id] pairs with the highest score first, and gives the ids of the k highest scores across all shards, highest first. On a tie, the smaller id first.

Code: python
def vector_bytes(n, dims, bytes_each):
    return 0


def graph_bytes(n, m):
    return 0


def shards_needed(total, per_shard):
    return 1


def merge_top(lists, k):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.