A Beginner's Guide to Vector Database Principles
How the RAG Pipeline Connects Every Piece
A vector database stores meaning as numbers so you can search by intent, not exact keywords.
π― System Scenario & Requirements
System Goal
A vector database stores meaning as numbers so you can search by intent, not exact keywords.
Actors & Components
- Application Workers
- Volatile Memory Tier
- Persistent Disk Tier
- Worker Process
- In-Memory Cache
- Primary Database
Functional Requirements
- TLDR: A vector database stores meaning as numbers so you can search by intent, not exact keywords.
- That is why "reset my password" can find "account recovery steps" even if the words are different.
- Write path: chunk documents β embed each chunk β upsert vector + metadata into the ANN index.
Constraints & Assumptions
- Common failure modes:
β‘ Execution Stages & Decision Trace
Stage 1 β Deep Dive: ANN Index Structures
ANN (Approximate Nearest Neighbor) indexes make vector search fast at scale by trading a tiny amount of recall for dramatically lower query latency: Pinecone and Weaviate default to HNSW.
Chroma uses HNSW via hnswlib .
Deep Dive
Scenario context
A vector database stores meaning as numbers so you can search by intent, not exact keywords. That is why "reset my password" can find "account recovery steps" even if the words are different.
flowchart TD
A[Raw Documents] --> B[Chunking]
B --> C[Embedding Model]
C --> D[Vector + Metadata]
D --> E[ANN Index]
Q[User Query] --> R[Query Embedding]
R --> E
E --> S[Top-k Cβ¦
This walkthrough focuses on **How the RAG Pipeline Connects Every Piece**.
Before making a design choice, clarify:
- The most common production pattern is Retrieval-Augmented Generation (RAG) , where the vector database acts as the LLM's long-term memory.
- Without the vector database the LLM only knows what was in its training data.
- With it, the model can cite your private knowledge base, product catalog, or today's incidents.
**Concepts in play:** vector-databases-rag, database-indexing, sorting-and-searching-algorithms, vector-embeddings-and-similarity
## Stage 1 β Deep Dive: ANN Index Structures
- ANN (Approximate Nearest Neighbor) indexes make vector search fast at scale by trading a tiny amount of recall for dramatically lower query latency: Pinecone and Weaviate default to HNSW.
- Chroma uses HNSW via hnswlib .
- pgvector supports both HNSW and IVF.
- π¬ Internals Vector databases index high-dimensional embeddings using Approximate Nearest Neighbor (ANN) algorithms.
*Applies [**vector-databases-rag**](/concept/vector-databases-rag) to the decision above.*
**Observable outcome:** π¬ Internals Vector databases index high-dimensional embeddings using Approximate Nearest Neighbor (ANN) algorithms.
## Stage 2 β Real-World Application: Semantic Search for a Support Knowledge Base
- Scenario: Your support team has 50,000 help articles.
- Customers type questions in natural language and expect the right article β even when wording does not match any article title.
- Step 1 β Index: Chunk each article into 400-token segments.
- Embed each chunk with text-embedding-ada-002 .
*Applies [**database-indexing**](/concept/database-indexing) to the decision above.*
**Observable outcome:** Embed each chunk with text-embedding-ada-002 .
## Stage 3 β Your First Semantic Search with Chroma in Python
- Chroma is the fastest way to try a vector database locally β no signup, no cluster, one pip install .
- What happens under the hood: Chroma embeds the documents and query using all-MiniLM-L6-v2 , stores them in an HNSW index, and returns the nearest vectors by cosine distance.
- To go to production, swap chromadb.Client() for Pinecone or Weaviate and use text-embedding-ada-002 .
*Applies [**sorting-and-searching-algorithms**](/concept/sorting-and-searching-algorithms) to the decision above.*
```python
import chromadb
client = chromadb.Client()
collection = client.create_collection("support-docs")
# Index two documents (Chroma embeds them with its built-in model)
collection.add(
documents=[
"How to reset your account password via email link",
"Steps to recover access when two-factor authentication is lost",
],
ids=["doc-1", "doc-2"],
)
# Query with a natural-language question
results = collection.query(
query_texts=["I can't log in, forgot my credentials"],
n_results=2,
)
for doc, dist in zip(results["documents"][0], results["distances"][0]):
print(f"[score {1 - dist:.3f}] {doc[:60]}...")
Observable outcome: To go to production, swap chromadb.Client() for Pinecone or Weaviate and use text-embedding-ada-002 .
Stage 4 β ChromaDB, Pinecone, Weaviate, and pgvector: Picking the Right Vector Store
- ChromaDB is an open-source embedded vector database built for local development and rapid prototyping β zero infrastructure required.
- Pinecone is a managed cloud vector database with serverless scaling.
- Weaviate is an open-source vector search engine with native hybrid (BM25 + vector) search.
- pgvector is a PostgreSQL extension that adds vector storage and ANN search without leaving your existing relational database.
# --- ChromaDB + sentence-transformers (local prototype, no signup needed) ---
# pip install chromadb sentence-transformers
import chromadb
from sentence_transformers import SentenceTransformer
encoder = SentenceTransformer("all-MiniLM-L6-v2")
client = chromadb.PersistentClient(path="./chroma_store")
collection = client.get_or_create_collection("knowledge-base")
docs = [
"Password reset sends a one-time link to your registered email address.",
"Two-factor authentication can be disabled from your account security settings.",
]
embeddings = encoder.encode(docs).tolist()
collection.upsert(documents=docs, embeddings=embeddings, ids=["doc-1", "doc-2"])
query_vec = encoder.encode(["I forgot my login credentials"]).tolist()
results = collection.query(query_embeddings=query_vec, n_results=2)
for doc, dist in zip(results["documents"][0], results["distances"][0]):
print(f"[similarity {1 - dist:.3f}] {doc[:80]}")
# --- pgvector (stays inside Postgres β zero new infrastructure) ---
# pip install psycopg2-binary pgvector
import psycopg2
from pgvector.psycopg2 import register_vector
conn = psycopg2.connect("dbname=support user=postgres")
register_vector(conn)
with conn.cursor() as cur:
cur.execute("CREATE EXTENSION IF NOT EXISTS vector")
cur.execute(
"CREATE TABLE IF NOT EXISTS docs "
"(id serial PRIMARY KEY, content text, embedding vector(384))"
)
vec = encoder.encode("Password reset guide").tolist()
cur.execute("INSERT INTO docs (content, embedding) VALUES (%s, %s)",
("Password reset guide", vec))
# Cosine similarity search: <=> is the pgvector cosine distance operator
query_vec = encoder.encode("forgot credentials").tolist()
cur.execute(
"SELECT content, 1 - (embedding <=> %s) AS similarity "
"FROM docs ORDER BY embedding <=> %s LIMIT 5",
(query_vec, query_vec)
)
for row in cur.fetchall():
print(f"[similarity {row[1]:.3f}] {row[0]}")
conn.commit()
Observable outcome: pgvector is a PostgreSQL extension that adds vector storage and ANN search without leaving your existing relational database.
Stage 5 β Three Things That Catch Every Vector Database Beginner
- You cannot search across mixed embedding models.
- If you index with text-embedding-ada-002 and later query with bge-base-en , the vectors live in incompatible geometric spaces β ANN search returns garbage.
- Use the same model for both indexing and querying, and track which model version was used for each document batch.
- Filtering happens in metadata, not in the vector space.
Observable outcome: Filtering happens in metadata, not in the vector space.
End-to-end outcome
The most common production pattern is Retrieval-Augmented Generation (RAG) , where the vector database acts as the LLM's long-term memory.
Alternatives considered
Decision Guide: When to Reach for a Vector Database: Decision Guide
Decision Guide: When to Reach for a Vector Database: Decision Guide
Failure and degraded modes
Trade-offs & Failure Modes: Vector DB vs. Elasticsearch vs. Relational
- Detection: Common failure modes:
- User or operator impact: Common failure modes:
