# Vector Database

> The Vector Database is a database designed to store embeddings and quickly find the most similar vectors.

## What is a vector database and when do I need one?

The database that stores embeddings and performs fast similarity search over them. Used in RAG and semantic search to find the documents closest in meaning to a query.

- Topic: Retrieval
- Type: technology

## Example

Wrong:

```js
await db.createIndex({ name: 'docs', dimension: 768 });
await db.upsert('docs', [{ id, values: await embed(text) }]);
```

Right:

```js
const vector = await embed(text);
await db.createIndex({ name: 'docs', dimension: vector.length, metric: 'cosine' });
await db.upsert('docs', [{ id, values: vector, metadata: { source: url, title } }]);
```

The index size comes from the model's output, and metadata is stored so results can be filtered and cited.

## Common mistakes

- Mixing vectors from two embedding models in one index.

## Key points

- The index dimension matches the embedding model's output size.
- The distance metric of the index matches the one the embedding model was built for.
- Each vector is stored with an ID and metadata for filtering and citations.
- Embeddings are recomputed when the embedding model changes.

## Interview questions

### Do I need a dedicated vector database?

Not always. Many regular databases support vector columns and similarity search, which is enough for small and medium data sets.

### What is approximate nearest neighbor search?

An index method that finds very similar vectors fast by trading a little accuracy for speed, instead of comparing every vector.

### Which distance metric should I use?

The one the embedding model was trained for, often cosine similarity or dot product. Check the model's documentation.

## Related cards

- [Embeddings](https://wonderium.dev/ai/embeddings.md)
- [Cosine Similarity](https://wonderium.dev/ai/cosine-similarity.md)
- [Semantic Search](https://wonderium.dev/ai/semantic-search.md)
- [RAG](https://wonderium.dev/ai/rag.md)
- [Databases](https://wonderium.dev/engineering/databases.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/vector-database
- Last updated: 2026-11-25
