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
| Word | What it means |
|---|---|
| Workload | the data and the questions a store must serve |
| Shard | one part of a collection, on its own node |
| Replica | a copy of a shard for reads and safety |
| Fan-out | sending one search to every shard |
| Managed service | a store somebody else runs for a fee |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsStart 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
| Store | What it is | Licence or hosting | Often chosen for |
|---|---|---|---|
| pgvector | An extension for PostgreSQL | Open source, PostgreSQL licence | Vectors beside other tables, with SQL |
| Qdrant | A vector database server | Open source, Apache 2.0; also managed | Filtered search, quantisation, sharding |
| Chroma | A store inside a Python program or a small server | Open source, Apache 2.0; also hosted | Prototypes and small apps |
| FAISS | A library for similarity search, not a database | Open source, MIT | Building your own search engine |
| Pinecone | A managed vector database | A hosted service only, not open source | Teams with no servers to run |
| Weaviate | A vector database server with keyword search built in | Open source, BSD 3-Clause; also managed | Hybrid search with little setup |
| Milvus | A distributed vector database | Open source, Apache 2.0; also managed | Very large collections on many machines |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsMatch 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
docker exec tools python3 steps/sizes.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: plan a hundred million vectors
# 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 questionsShards 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
docker exec tools python3 steps/shards.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: 20,000 points in 4 shards
| Shard | Points |
|---|---|
| 0 | 4687 |
| 1 | 5991 |
| 2 | 4583 |
| 3 | 4739 |
| All | 20000 |
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 questionsAssignment: 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.
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