A glowing database cube in a network of points, titled What is a Vector Database

What Is a Vector Database? A Beginner’s Guide (and How to Pick One)

If you have been reading about RAG, you have already met the vector database, even if it was hiding under a friendlier name like “the store.” In my beginner guide to RAG, it is the place that holds the embeddings and hands back the closest chunks. This post stays with that store itself.

I want to answer a few practical questions. What is a vector database? How is it different from the databases we already use? What do embeddings, similarity search and indexes actually mean in plain language? And when do you need a dedicated product, instead of a Postgres extension or a list of vectors in memory?

What a vector database actually is

A vector database is a store built to answer one kind of question well. Given this item, which stored items are most similar in meaning? The items are usually text, though they can also be images, audio or code. Each one is saved as a vector, a long list of numbers produced by an embedding model, together with the original content and some metadata, such as a source file or a tenant id.

That is the whole job, really. You put vectors in. You get the nearest neighbours out, often with filters on the metadata. Everything else, indexes, hybrid search, scaling, is there to make that job fast and reliable once the collection grows.

How it differs from a traditional database

A traditional database is excellent at exact values. “Give me order 1024.” “Find the user whose email is this.” It stores rows or documents with named fields, and you query it with precise conditions. A vector database is built for a fuzzier question. “Which paragraphs are closest in meaning to this complaint?” The words in the complaint and the words in the handbook may not match at all.

You still need both. I would not store invoices only as embeddings, and I would not try to do semantic search with a plain WHERE clause on full text alone. The two kinds of store solve different problems, and in a real app they often sit side by side.

Side by side comparison of a traditional database, which answers exact queries, and a vector database, which finds similar meanings.
A regular database matches exact values. A vector database matches meaning.

Embeddings and vectors, in plain words

An embedding model reads a piece of text and returns a vector. Think of it as a point in a large space. Passages that mean similar things end up close together, even when they use different words. “Reset my password” can sit near a paragraph that only says “terminating your subscription,” if the model has learned that those ideas are related.

The important practical rule is simple. You must use the same embedding model for the items you store and for the question you ask. Mix two models, and you are comparing points from two different maps. I also wrote about the token budget that surrounds all of this in my guide to tokens and context windows.

Similarity search, kept simple

Once the question is a vector, the database scores it against stored vectors and returns the top matches. That scoring is similarity, or its close cousin, distance. Three measures show up all the time.

Cosine similarity looks at the angle between two vectors, not how long they are. It is a common default for text, because two passages can have different lengths and still mean the same thing. Dot product is closely related. If the vectors are normalised, meaning they have been scaled to the same length, cosine and dot product rank items in the same order. Euclidean distance is the straight-line gap between two points. It is easy to picture, and some image or audio embeddings use it more naturally than text does.

You do not need to memorise the formulas to use a vector database. You do need to use the measure the embedding model was trained for, and to stay consistent. If a library talks about distance instead of similarity, remember that a smaller distance is a closer match.

Why indexes exist, and what HNSW and IVF are doing

If you only have a few thousand vectors, you can compare the question with every one of them. That exact scan is sometimes called a flat index, and it is perfectly fine for a demo. As the collection grows, a full scan gets slow, so the database uses an approximate nearest neighbour index. It looks at a fraction of the vectors and accepts a small chance of missing a slightly better neighbour, in exchange for speed.

HNSW, short for Hierarchical Navigable Small World, is the index I see most often as a default, including in pgvector. The idea was described by Yury Malkov and Dmitry Yashunin in a 2016 paper. It builds a layered graph. The top layers are sparse, with long links, so a search can travel a long way quickly. The bottom layer holds every vector, with shorter links, and that is where the fine search finishes. A useful picture is a road map with highways on top and local streets at the bottom.

IVF, the inverted file index, works more like sorting mail into bins. During a training step, vectors are grouped into clusters. At query time, the database finds the nearest few cluster centres, then searches only inside those bins. You can look at more bins for higher accuracy, or fewer bins for more speed.

For a first project, I would accept the default index, usually HNSW, and only start tuning when I have real questions to measure against. Indexes are a trade. A little recall for a lot of speed. They are not a reason to delay the first working prototype.

Metadata filtering and hybrid search

Metadata is the ordinary information you store next to a vector, such as a source, a product, a date or a tenant. Filtering means “only search inside this customer’s documents” or “ignore anything older than a year.” If you skip this, a chat box can retrieve the wrong team’s files. That is not a clever failure. It is a permissions hole.

Hybrid search adds a classic keyword search, often something in the BM25 family, and merges it with the vector results. Vector search is good at meaning and weaker at exact tokens. It can miss an error code, a SKU or a person’s exact name. If your users paste identifiers, hybrid search is usually worth adding before you reach for anything more exotic.

When you actually need a dedicated vector database

You do not need one on day one. In the RAG example I used a list of vectors in memory and ranked them with cosine similarity. That is enough to learn the idea, and it dies when the process dies, which is fine for a tutorial.

Three panel diagram comparing an in-memory or local store, a vector extension in Postgres, and a dedicated vector database.
Start simple, then move only when the current store is getting in the way.

The next step, for many app teams, is a vector extension inside Postgres, usually pgvector. If your application data already lives there, you keep one database to back up, one set of credentials, and ordinary SQL for filters. I used this approach in the Spring AI RAG tutorial with PgVector and Ollama, which builds on the Spring AI chatbot tutorial.

A dedicated vector database starts to make sense when retrieval is a real product concern of its own. Maybe you need richer payload filters or hybrid search than you want to assemble by hand. Maybe Postgres is fine for the rest of the app, but the vector workload is growing noisy. Maybe you want a managed service so you are not also running the search cluster. “A few million vectors” is not a magic line, and I will not invent one. Measure latency and recall on your own questions, then move because something is actually hurting, not because a blog post made the dedicated option sound inevitable.

A generic look at popular options

I am naming these as examples of the same idea, not as a ranked list, and I am sticking to traits that are well documented by the projects themselves. I have not benchmarked them for this post.

pgvector is an extension for PostgreSQL, released under the PostgreSQL license. It adds vector types and similarity operators to the database you may already run. That is its main appeal. One system, familiar backups, and SQL next to your other tables.

Chroma is an open source store, licensed under Apache 2.0, that can run inside a Python process or as a server. Teams often reach for it when they want a short path from a notebook to a working retrieval step.

Qdrant is an open source vector database, also under Apache 2.0, that you can self-host or run as a managed cloud. It is often chosen when people want a dedicated engine with strong payload filtering and a path to hybrid search, without giving up self-hosting.

Milvus is an open source system, Apache 2.0, with a managed offering from Zilliz. It is built for large collections and for teams that want a purpose-built cluster, including a lighter local option for development. Weaviate is open source under the BSD-3-Clause license, with a self-hosted and a cloud option. It is known for optional built-in vectorization modules and for hybrid search that combines keyword matching with vectors.

Pinecone is a managed, proprietary service. You do not self-host it. The usual reason to pick it is to avoid running the search infrastructure yourself. That convenience is the product, and it also means your operations and pricing follow their cloud, not a database you already own.

How I would choose one

I would start with the stack I already have. If the work is a prototype, I would keep vectors in memory or use a small embedded store. If the app already uses Postgres, I would try pgvector next and see whether it stays comfortable. I would only add a dedicated database when I can name the reason, such as filtering, hybrid search, a scaling limit I have actually hit, or a clear wish to have someone else run the cluster.

Two other checks matter more than brand names. Can you filter on metadata so the wrong documents never enter the prompt? And can you export or self-host if you later need to move? A store that holds your company’s knowledge should not become a trap.

If you are running models on your own machine while you experiment, my Ollama vs LM Studio comparison is a useful companion, because the embedding model and the store have to agree with each other.

A tiny illustrative snippet

Here is a small pgvector-style query, plus a few lines of Python that show the idea of ranking by cosine similarity. I have not run this snippet in the environment that wrote this post. I did not install Postgres or any vector database here, so please treat it as an illustration, not a tested recipe.

-- Illustrative SQL for Postgres with pgvector. Untested in this post.
-- <=> is cosine distance. A smaller distance is a closer match.
SELECT id, content, source
FROM documents
WHERE tenant_id = :tenant
ORDER BY embedding <=> :query_embedding
LIMIT 5;
# Illustrative Python, also untested here.
# In a real app the embeddings would come from a model, and the rows from a store.

def cosine(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    na = sum(x * x for x in a) ** 0.5
    nb = sum(x * x for x in b) ** 0.5
    return dot / (na * nb) if na and nb else 0.0

def top_k(query_embedding, rows, k=5):
    # each row is {"id": ..., "embedding": [...], "text": ...}
    ranked = sorted(rows, key=lambda row: cosine(query_embedding, row["embedding"]), reverse=True)
    return ranked[:k]

The SQL version is what I would grow toward in an application. The Python version is what I would use to understand the ranking before I involve a server.

Where this sits with RAG

A vector database is a tool. RAG is a pattern that often uses that tool. If you want the wider picture, start with the RAG explainer, then come back here when you are ready to pick a store. If you want the same idea in a Java stack, the Spring AI PgVector walkthrough is the next practical step.

Frequently Asked Questions

What is a vector database in simple terms?

It is a database that stores embeddings, which are lists of numbers that capture meaning, and finds the items closest to a question. You usually also store the original text and some metadata next to each vector.

Is a vector database the same as RAG?

No. RAG is the pattern of retrieving relevant passages and then asking a language model to answer from them. A vector database is one common place to store and search those passages. You can learn RAG with an in-memory list of vectors, as I did in the beginner guide.

Do I need a dedicated vector database to start?

Usually not. An in-memory store is enough to learn. If you already run Postgres, pgvector is a natural next step. Reach for a dedicated product when you have a clear reason, such as filtering, hybrid search, scale or a wish to avoid running the cluster yourself.

What is the difference between cosine similarity and Euclidean distance?

Cosine cares about the angle between two vectors, which is useful for text of different lengths. Euclidean distance is the straight-line gap between two points. If vectors are normalised to the same length, cosine and dot product will rank results in the same order. Use the measure your embedding model expects.

What are HNSW and IVF?

They are approximate indexes that avoid comparing a question with every stored vector. HNSW walks a layered graph of neighbours. IVF groups vectors into clusters and searches only the nearest clusters. Both trade a little accuracy for speed once the collection grows.

Can I put private documents in a vector database?

Yes, but the database will retrieve whatever it is allowed to search. Use metadata filters, and preferably a separate index, so one user cannot see another user’s files. Treat the store like any other system that holds sensitive text.

Content Protection by DMCA.com
Spread the love
Scroll to Top
×