Vector databases: storing embeddings and searching them by similarity
Vector databases store and index high-dimensional vector embeddings, enabling fast similarity search. They use specialized indexing and approximate nearest neighbor algorithms to find similar items. This article explains core concepts, indexing methods, and practical applications.
What is a vector database?
A vector database is a system designed to store, manage, and index high-dimensional vector data, as described by IBM's vector database overview. In such a database, each data point is represented as an array of numbers, and these arrays can be compared and grouped based on similarity. This design supports low-latency queries, making it suitable for AI applications.
Unlike traditional relational databases that rely on exact keyword matches, vector databases capture semantic meaning. For example, a search for "smartphone" can return results like "cellphone" or "mobile device" even if those exact words do not appear, because vectors represent learned characteristics and relationships geometrically.
Vector databases are growing in popularity due to their role in generative AI. According to AWS's explanation of vector databases, they provide the ability to store and retrieve vectors as high-dimensional points, with additional capabilities like data management, fault tolerance, authentication, and a query engine.
How vector embeddings work
Vector embeddings are numerical representations of data points, typically arrays of floating-point numbers, that translate unstructured data like text, images, or audio into a format machine learning models can process, as explained by Google Cloud's vector database guide. Through this mapping to a vector space, embeddings convey semantic meaning: items with similar content are located near one another, while those that are different lie farther away.
Embedding models learn to map input data into a high-dimensional vector space, often using a task-specific loss function to shape the space. For instance, the words "cat" and "dog" might be represented as 3-dimensional vectors that are close together, reflecting their semantic relationship, as illustrated in IBM's overview.
These embeddings can be generated for various content types, including text, images, and audio, allowing for multimodal search. For example, a user could search for images using a text description, because both are represented in the same vector space.
Indexing and search algorithms
Efficiently querying large sets of vectors requires specialized indexing and search strategies. Vector databases use nearest neighbor search (KNN) to identify the k vectors closest to a query vector, using distance metrics such as dot product, cosine similarity, or Euclidean distance, as noted by Google Cloud's guide.
Because calculating exact distances to every vector can be computationally expensive, databases use approximate nearest neighbor (ANN) algorithms. These algorithms trade a small amount of accuracy for significant speed improvements, an acceptable compromise for most semantic search applications.
Common ANN algorithms include Hierarchical Navigable Small World (HNSW) and Inverted File Index (IVF). The HNSW algorithm, described in the arXiv paper on HNSW graphs, builds a multi-layer graph structure that enables logarithmic complexity scaling. IVF, on the other hand, partitions vectors into clusters for faster search.
Vector indexes organize data so that clusters of nearby vectors are grouped together, using structures like lists, graphs, or trees. Each index type has its own balance of lookup speed, memory usage, and build time, allowing developers to choose based on their needs.
Use cases and applications
Vector databases are used in a variety of AI applications, including retrieval-augmented generation (RAG), where they serve as an external knowledge base for large language models. By retrieving relevant context before generating a response, they help reduce hallucinations and provide factually accurate answers, as explained by Microsoft's vector search overview.
Other use cases include real-time recommendation engines, semantic and multimodal search, and anomaly detection. For example, vector databases can power ecommerce product suggestions by representing user behavior and product attributes as vectors, or detect fraud by identifying outliers in a vector space of normal transactions.
Vector databases also enable hybrid search, combining vector similarity with keyword search and metadata filtering. This allows users to refine results based on criteria like price or category, as described in the AWS and Google Cloud sources.
Implementation examples
One popular open-source implementation is pgvector, an extension for PostgreSQL that adds vector similarity search. According to the pgvector GitHub repository, it supports exact and approximate nearest neighbor search, multiple vector types, and various distance functions including L2, inner product, and cosine distance.
pgvector integrates with PostgreSQL's existing features, such as ACID compliance, point-in-time recovery, and JOINs, allowing developers to store vectors alongside other data. It supports indexing with HNSW and IVFFlat, and can be installed on Linux, Mac, and Windows.
For large-scale applications, GPU-accelerated similarity search can be used. The arXiv paper on billion-scale similarity search with GPUs describes a design that enables building a k-NN graph on 1 billion vectors in less than 12 hours, demonstrating the scalability of vector search with appropriate hardware.
| Feature | IBM | AWS | Google Cloud | Microsoft | pgvector |
|---|---|---|---|---|---|
| Stores high-dimensional vectors | Yes | Yes | Yes | Yes | Yes |
| Supports exact nearest neighbor search | Not stated | Not stated | Not stated | Not stated | Yes |
| Supports approximate nearest neighbor (ANN) | Yes | Yes | Yes | Yes | Yes |
| Index types mentioned | Not specified | HNSW, IVF | Lists, graphs, trees | Nearest neighbors algorithm | HNSW, IVFFlat |
| Distance functions | Not specified | Cosine | Dot product, cosine, Euclidean | Not specified | L2, inner product, cosine, L1, Hamming, Jaccard |
| Hybrid search support | Not stated | Yes | Yes | Yes | Not stated |
| Metadata filtering | Not stated | Yes | Yes | Yes | Not stated |
| Open source | No | No | No | No | Yes |
Frequently asked questions
What is a vector embedding?
A vector embedding is a numerical representation of data, such as text or images, as an array of numbers. It captures semantic meaning so that similar items are close together in vector space.
How does a vector database differ from a traditional database?
Traditional databases use exact keyword matches, while vector databases use similarity search based on vector distances. This allows them to find semantically related items even if exact terms do not match.
What is approximate nearest neighbor search?
Approximate nearest neighbor (ANN) search is a technique that finds vectors close to a query vector without checking every item. It trades a small amount of accuracy for much faster search speeds, using algorithms like HNSW or IVF.
What are common use cases for vector databases?
Common use cases include retrieval-augmented generation (RAG), recommendation systems, semantic and multimodal search, and anomaly detection.
Can vector databases be used with existing SQL databases?
Yes, extensions like pgvector add vector support to PostgreSQL, allowing vectors to be stored alongside traditional data and queried with SQL.