Traditional databases are good at finding information through exact values, conditions, and structured queries. But AI applications often need something different: finding information based on meaning rather than matching words.
For example, a user searches for "How can I reduce application response time?" A traditional keyword search might look for documents containing those exact words. But a relevant document titled "Techniques for improving API latency" might never appear.
Vector databases help solve this problem by storing numerical representations of information and finding items that are similar in meaning.
They have become an important part of Retrieval-Augmented Generation (RAG), semantic search, recommendation systems, and other AI applications.
Understanding how they work requires looking at four things: embeddings, similarity search, indexing, and database selection.
1. What Is a Vector Database?
A vector database stores and searches vector embeddings, which are numerical representations generated by machine learning models.
Unlike a traditional database that primarily retrieves records using exact values or defined conditions, a vector database can retrieve records based on their similarity to a query.
Consider a knowledge base containing thousands of technical documents. A developer asks:
"Why is my API taking too long to respond?"
The database might retrieve documents discussing slow database queries, high request latency, or inefficient network communication, even when they don't contain the exact words from the question.
This is possible because the database compares numerical representations of the query and stored documents rather than relying only on matching text.
A vector database generally stores more than embeddings. Each record can also contain the original content, an identifier, and metadata such as document category, source, timestamp, or access permissions.
2. Understanding Embeddings
Embeddings are the foundation of vector search.
An embedding model converts information, such as text, images, or audio, into a list of numbers called a vector.
For example, consider these two sentences:
- "How do I improve API performance?"
- "Ways to reduce application latency."
Although the sentences use different words, they communicate similar ideas.
An embedding model processes each sentence and produces a numerical representation containing hundreds or thousands of dimensions, depending on the model.
Conceptually, the output might look like this:
[0.021, -0.134, 0.458, 0.087, ...]
These numbers represent learned features of the input. When two pieces of information are semantically related, their embeddings may be positioned closer together in the vector space.
The important point is that embeddings do not simply convert each word into a number. They capture patterns learned during model training, allowing the representation to reflect contextual relationships.
In an AI application, the same embedding model is generally used to generate vectors for stored documents and incoming queries.
If the embedding model changes, the stored vectors may need to be regenerated to keep retrieval consistent.
3. How Similarity Search Works
Once documents have been converted into embeddings, the application can search for information by comparing vectors.
Suppose your knowledge base contains 100,000 document chunks. A user submits a question.
The retrieval process looks like this:
- Convert the user's question into an embedding.
- Compare the query vector with stored document vectors.
- Calculate similarity or distance scores.
- Rank the matching records.
- Return the most relevant results.
The system commonly retrieves the top K results, where K represents the number of records requested.
For example, with K set to 5, the database returns the five closest matches according to the selected similarity metric.
Three common metrics are:
Cosine similarity
Measures the angle between two vectors. It is commonly used for semantic text search and focuses on directional similarity.
Euclidean distance (L2)
Measures the straight-line distance between two vectors. Smaller distances indicate closer vectors.
Dot product
Measures the product of corresponding vector dimensions. Depending on the embedding model and normalization, it can be used as a similarity measure.
The correct metric depends on how the embedding model produces its vectors. Using an incompatible metric can reduce retrieval quality.
For normalized embeddings, cosine similarity and dot-product ranking can produce equivalent results. https://developers.openai.com/api/docs/guides/embeddings
4. Why Vector Databases Need Indexes
Imagine storing ten million embeddings.
Without an index, a straightforward similarity search may need to compare the query against every stored vector.
This is called exact nearest-neighbor search. It gives accurate results but becomes computationally expensive as the dataset grows.
Vector databases use specialized indexing algorithms to make retrieval faster.
Two widely used approaches are HNSW and IVF.
HNSW (Hierarchical Navigable Small World)
HNSW builds a multi-layer graph connecting nearby vectors.
Think of it as navigating a city using different levels of maps. The upper levels help you make larger jumps toward the target, while the lower levels help you find closer matches.
Instead of comparing every vector, the search algorithm follows connections through the graph to find promising candidates.
HNSW generally offers a strong balance between search speed and recall, but it requires additional memory and can take longer to build.
IVF (Inverted File Index)
IVF takes another approach.
It groups vectors into clusters based on their similarity. During a search, the algorithm identifies the clusters most likely to contain relevant results and searches only a selected number of them.
This reduces the number of vectors that need to be examined.
However, searching fewer clusters can miss relevant results. Increasing the number of clusters examined can improve recall but also increase query time.
The trade-off: Approximate nearest-neighbor indexes improve search efficiency, but they can return slightly different results from exact search. Developers need to balance latency, memory usage, and retrieval accuracy.
For example, pgvector supports both HNSW and IVFFlat indexing, each with different performance and resource characteristics. https://github.com/pgvector/pgvector
5. How Vector Databases Fit into RAG
One of the most common applications of vector databases is Retrieval-Augmented Generation.
A RAG application needs to retrieve relevant information before sending context to an LLM.
The workflow generally looks like this:
Step 1: Document ingestion
Collect information from PDFs, websites, databases, documentation, or internal knowledge sources.
Step 2: Chunking
Divide large documents into smaller sections. This allows the system to retrieve relevant passages instead of entire documents.
Step 3: Embedding generation
Convert each chunk into a vector using an embedding model.
Step 4: Vector storage
Store embeddings alongside their original text and metadata in a vector database.
Step 5: Query retrieval
Convert the user's question into an embedding and search for similar document chunks.
Step 6: Response generation
Send the retrieved information to the LLM as additional context so it can generate an answer grounded in the available sources.
The vector database is responsible for retrieving relevant information. It does not generate the final answer itself.
This distinction is important when designing a production RAG pipeline.
6. Vector Search Is Not Always Enough
Semantic similarity is powerful, but it does not solve every search problem.
Consider a developer searching for an exact error code:
ERR_CONNECTION_RESET
A semantic search system might retrieve documents about general network failures without prioritising the exact error code.
For technical documentation, product catalogs, and enterprise search, exact keyword matching can be just as important as semantic similarity.
This is why many production systems use hybrid search.
Hybrid search combines:
- Dense vector search for semantic relationships.
- Keyword search, often using BM25, for exact terms and identifiers.
The results are then combined into a unified ranking.
For example, Weaviate and Qdrant provide hybrid retrieval capabilities, allowing applications to combine semantic and lexical search. https://docs.weaviate.io/weaviate/concepts/search/hybrid-search
A reranking model can also be added after retrieval to evaluate the shortlisted results more carefully and improve their relevance.
7. Which Vector Database Should You Choose?
There is no universally best vector database. The right choice depends on your existing infrastructure, data volume, filtering requirements, and operational constraints.
Here are four options worth evaluating.
PostgreSQL with pgvector
A practical choice when your application already uses PostgreSQL.
It allows developers to store relational data and embeddings together, use SQL for metadata filtering, and perform vector similarity search without operating a separate database.
It is particularly attractive when an application has moderate retrieval requirements and benefits from existing PostgreSQL infrastructure.
Qdrant
A vector-focused database designed around similarity search and structured metadata filtering.
It supports HNSW indexing, payload indexes, hybrid retrieval, and different deployment options.
It is worth considering when vector retrieval and metadata filtering are central to the application's architecture.
Weaviate
An open-source vector database supporting vector search, keyword search, hybrid retrieval, and structured data.
It can be useful for semantic search and RAG applications that need integrated retrieval capabilities.
Milvus
An open-source vector database designed for scalable similarity search across large vector collections.
It offers different deployment modes, including lightweight local development and distributed deployments for larger workloads.
It is worth evaluating when handling large vector datasets is an important part of the system design.
Quick selection guide
| Requirement | Starting point |
|---|---|
| Existing PostgreSQL application | pgvector |
| Dedicated vector search and filtering | Qdrant |
| Integrated hybrid retrieval | Weaviate |
| Large-scale distributed vector search | Milvus |
These are starting points, not performance rankings. Benchmark your actual workload before committing to a database.
8. What to Evaluate Before Production
Selecting a vector database is only the beginning. The retrieval system must perform reliably under real application conditions.
Pay attention to:
- Retrieval accuracy: Measure whether relevant documents consistently appear in the top results.
- Latency: Evaluate query response times, especially at higher traffic levels.
- Metadata filtering: Ensure queries respect tenant boundaries, document permissions, and other access requirements.
- Scalability: Understand how storage, memory, and indexing costs change as the dataset grows.
- Data updates: Test how the system handles document changes, deletions, and embedding regeneration.
- Hybrid search: Check whether exact keyword matching improves results for your use case.
For RAG applications, retrieval accuracy matters as much as query speed. A fast database that consistently retrieves irrelevant context will not produce reliable AI responses.
Conclusion
Vector databases provide the retrieval infrastructure behind many modern AI applications.
Embeddings make it possible to represent information numerically, similarity search finds related information, and specialized indexes make retrieval practical across large collections of vectors.
But choosing a database should not begin with popularity or benchmark claims. It should begin with the application's actual requirements.
For some systems, PostgreSQL with pgvector is sufficient. Others may benefit from dedicated solutions such as Qdrant, Weaviate, or Milvus.
The goal is not simply to store vectors. It is to retrieve the right information, efficiently and reliably, when an AI application needs it.



