If you built an AI app in the early days of LLMs, you probably hit a wall pretty quickly: Large Language Models have no long-term memory. They forget who your user is, they don't know your company's proprietary documentation, and pasting 500-page PDFs directly into prompt context windows is expensive, slow, and prone to context rot.
Enter Vector Databases.
Whether you're building a Retrieval-Augmented Generation (RAG) pipeline, an autonomous AI agent with long-term memory, or a high-performance recommendation engine, vector databases have become core infrastructure.
Here is the complete guide to how vector databases work, the main types available, and why they matter for modern application development.
Traditional databases (like PostgreSQL, MySQL, or MongoDB) store scalar data—strings, integers, JSON objects—and rely on exact matches or range queries. If you search for "laptop"
, a traditional full-text index looks for the exact characters l-a-p-t-o-p
. If your document contains "MacBook"
or "portable computer"
, keyword search misses it completely unless manually tagged.
Vector databases, on the other hand, store high-dimensional vector embeddings.
"MacBook Air" ---> [0.024, -0.198, 0.812, ... 1536 dimensions]
"Portable Computer" ---> [0.021, -0.201, 0.799, ... 1536 dimensions]
"Juicy Apple" ---> [-0.512, 0.884, -0.012, ... 1536 dimensions]
An embedding model (like OpenAI's text-embedding-3
, Cohere, or an open-source model like bge-large-en
) transforms unstructured data—text, images, audio, or video—into a sequence of floating-point numbers.
In this high-dimensional space:
A vector database indexes these numerical arrays so you can run Nearest Neighbor (k-NN) queries to find semantically similar items in milliseconds.
+--------------------------------------------------------------------------+
| THE VECTOR FLOW |
| |
| Unstructured Data ===> Embedding Model ===> High-Dim Vector |
| ("User Query") (e.g., OpenAI) [0.12, -0.45, ...] |
| | |
| v |
| Top K Results <=== Vector DB Index <=== Distance Search |
| (Nearest Neighbors) (HNSW / IVF) (Cosine/Dot Prod) |
+--------------------------------------------------------------------------+
Because calculating exact distances across billions of high-dimensional vectors is computationally impossible in real-time, vector databases use Approximate Nearest Neighbor (ANN) indexing algorithms:
When querying vectors, the database calculates geometric distance using one of three metrics:
1. Cosine Similarity : Measures the ANGLE between vectors (best for normalized text).
2. Dot Product : Measures ANGLE + MAGNITUDE (fastest for pre-normalized vectors).
3. Euclidean (L2) : Measures STRAIGHT-LINE DISTANCE between vector endpoints.
Not all vector databases are built the same. The ecosystem has divided into distinct categories based on operational needs and scale:
| Category | Popular Examples | Best Used For | Trade-offs |
|---|---|---|---|
| Dedicated Vector DBs | |||
| Pinecone, Qdrant, Milvus, Weaviate | Massive scale (10M+ to Billions of vectors), low-latency requirements | Extra infrastructure component to manage | |
| Relational / SQL Extensions | |||
PostgreSQL + pgvector / pgvectorscale |
|||
| Teams already using SQL; moderate scale (<10M vectors) | Shares CPU/RAM resources with primary database | ||
| Search Engine Extensions | |||
| Elasticsearch, OpenSearch | Hybrid keyword + vector search; heavy enterprise logs | Higher memory footprint and query overhead | |
| Embedded / In-Process | |||
| Chroma, LanceDB, FAISS | Local development, edge computing, mobile, zero-copy processing | Limited multi-node horizontal scaling |
Built ground-up specifically for high-dimensional arrays.
pgvector
on Postgres)
The most popular architectural movement is bringing vectors directly into relational data. With extensions like pgvector
or pgvectorscale
, you store embeddings inside a standard SQL column.
-- Creating a table with a 1536-dimensional vector column
CREATE TABLE knowledge_base (
id SERIAL PRIMARY KEY,
content TEXT,
metadata JSONB,
embedding vector(1536)
);
-- Creating an HNSW index for ultra-fast similarity search
CREATE INDEX ON knowledge_base
USING hnsw (embedding vector_cosine_ops);
-- Performing a combined SQL + Vector query
SELECT content, metadata
FROM knowledge_base
WHERE metadata->>'category' = 'engineering'
ORDER BY embedding <=> '[0.012, -0.421, ...]' -- '<=>' is Cosine Distance
LIMIT 5;
Why developers love pgvector: You perform joins, ACID transactions, metadata filters, and vector searches in a single SQL query without running a separate database service.
Embedded databases run inside your application process (like SQLite). LanceDB, built on top of the Apache Arrow columnar format, allows zero-copy retrieval directly from disk or S3 buckets without everything into expensive RAM.
LLMs hallucinate when asked about private enterprise data. RAG solves this by fetching real-time context before passing the query to the model.
import os
from pinecone import Pinecone
from langchain_openai import OpenAIEmbeddings
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
index = pc.Index("enterprise-kb")
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
query = "What is our company policy on remote work setup budgets?"
query_vector = embeddings.embed_query(query)
results = index.query(vector=query_vector, top_k=3, include_metadata=True)
context = "\n".join([match["metadata"]["text"] for match in results["matches"]])
Autonomous agents need short-term (scratchpad) and long-term (episodic) memory. By storing user interactions and agent execution steps in a vector store, agents recall past decisions and preferences seamlessly across long chat histories.
Pure vector search sometimes fails on exact code names, part numbers, or specific legal terms (e.g., looking for "Error 504"
vs "Gateway Timeout"
).
Modern vector databases handle Hybrid Search—combining BM25 keyword scoring with dense vector similarity—to ensure exact name hits are never missed while keeping semantic context intact.
Final Score = (α * Sparse_BM25_Score) + ((1 - α) * Dense_Vector_Score)
When choosing your vector storage strategy, follow this quick playbook: