Best Practices for Atlas Vector Search Performance / Learn

Please note, this video is best viewed with 1080p quality selected. You can use the player bar to navigate to chapters within the video.

Chapters

0:00 Introduction

1:30 Elements of High Performance

2:13 Performance Monitoring

4:11 Reduce Vector Dimensions

7:55 Ensure Enough Memory

10:17 Avoid Indexing During Queries

11:12 Warm the Filesystem Cache

12:16 Exclude Vector Fields from the Results

13:07 CPU Bottlenecking

13:50 Learn More


Resources


Video Transcript (English)

Hi, and welcome to MongoDB Learning Bytes, where you can learn about MongoDB in 20 minutes or less. I'm Prakul Agarawal, a senior product manager for machine learning at MongoDB. In the world of AI and machine learning, Vector Search has become a game changer for building intelligent applications. By now, you are likely familiar with Atlas Vector Search and how it helps you create GenAI applications.

But as your project scales, you might find yourself asking, how can I make my Vector Searches faster? Is there a way to reduce cost without sacrificing performance? What tweaks can I make to handle more data and queries? If you have been pondering these questions, you're in the right place.

In the next few minutes we'll dive into the art and science of fine tuning your Atlas Vector Search for peak performance. In this Learning Byte, we'll explore the key elements that influence Vector Search performance; practical strategies for monitoring and measuring your optimizations; tips for optimizing your vector embedding; techniques to enhance indexing efficiency; and methods to improve your query performance. Whether you are building a semantic search tool, a RAP tool, recommendation engine, or any application leveraging vector similarities, these optimization techniques will help you make your project more efficient.

Let's dive in.

MongoDB's Vector Search utilizes approximate k nearest neighbor, or kNN, algorithms to efficiently identify the k closest points for a given query vector. This is powered by HNSW, or Hierarchical Navigable Small Worlds, which is a graph-based methodology for fast retrieval. Due to this, there are performance factors that differ from those of traditional queries due to the unique nature of approximate Vector Search.

Specifically, MongoDB Vector Search provides approximate kNN queries by enabling the efficient indexing and retrieval of vector embeddings. These are the elements we will be focusing on optimizing. Before we discuss the specific optimizations, it's important to discuss performance monitoring. The key to any optimization effort is measuring performance.

After all, how can you know that you improved something if you don't know where it stands? Fortunately, MongoDB Atlas has built-in tools to help you monitor performance and chart your progress. Atlas has integrated metrics that can be leveraged to understand the system's performance. The metrics allow you to view a graph of the specific attributes value plotted over a period of time.

For instance, you can find metrics corresponding to the CPU usage, which tells you how the CPU utilization is changing over time for your database instance. To find the metrics, click on the Metrics tab within your AWS view. Then you can choose various options corresponding to the time granularity for reporting and the time period between which the metrics should be shown. The three key metrics for Atlas Vector Search are search page faults, system memory, and system CPU.

Search page faults is the average rate of page faults on a process per second over a selected sample period. It represents when your index falls out of memory and it has to access the disk. System memory represents a number of bytes of system memory available for running new applications without swapping. This can be used to find out if the index is loaded into file system cache.

System CPU represents the utilization of the underlying CPU instances. The system CPU helps understand if there is bottlenecking on serving requests. These metrics will be helpful as we look for common issues and monitor system performance. You can learn more about these metrics linked in the resources below, but for now, we'll move on to the optimization best practices.

Reduce vector dimensions. One of the easiest ways to reduce the complexity of retrieving vectors is to reduce the number of dimensions in the vector embeddings. Some embedding models come with the default number of vector dimensions that can be adjusted up or down. While adjusting these values higher can provide more exact matches in densely populated regions, it comes with some performance trade offs.

For one, the size of the embeddings is increased, which results in additional storage cost and embedding time. When retrieving, you will also notice increased latency and compute usage in order to compare the additional vectors. By adjusting the number of vectors down, you will reduce the size of the embeddings and the time to find matches. However, it is important to create a baseline of retrieval performance before adjusting the number of vectors so you can measure the impact on your accuracy and latency.

Early embedding models improved query results by using more dimensions to represent data. While this enhanced performance, it also reduced the efficiency of downstream tasks, like search and classification. In response, researchers developed embedding models that could effectively reduce size without significantly compromising performance. These are called as matryoshka embedding models and are designed to ensure that they remain effective while using fewer dimensions.

OpenAI text-embedding-v3-large is a popular embedding model which uses this technique. And the size of the embeddings can be varied between 256 and 3,072. Some popular embedding models used today still don't use this method including, OpenAI's ada-v2 and mini-lm-v6. Let's take a look at the impact of changing the number of vector embedding dimensions and its effect on retrieval accuracy.

Linked is a table listing common embedding models, the number of dimensions used for embeddings, and the average MTEB score. As you can see, using fewer dimensions results in lower MTEB score, but the tradeoff may be worth it, depending on your use case. Further text embedding v3 large, the dimensions can range from 256 to 3,072. Using fewer dimension results in some retrieval loss, but comes with significantly lower storage cost.

So you're trying to determine what trade off is worth it, depending on your use case. MTEB is an industry standard benchmark data set generally used to evaluate embedding models performance. For 256 dimensions, the average MTEB score is 62. And for 3,072 dimensions, it is 64.6.

And obviously, the higher is better. The same score for ada-v2 at 1,536 dimensions is 61. As you would recollect, ada-v2 is the older embedding model from OpenAI. So essentially, using text-embedding-v3-large at 256 dimensions gives you a better retrieval performance than the OpenAI's ada-v2 at 1,536 dimensions.

However, the reduction in the associated storage is huge, so a general recommendation would be to use the text-embedding-v3-large at lower dimensions. But this score is based on MTEB data set, and you would want to run some benchmarking on your own data set to understand the performance. Another tip is when evaluating your results, you can make use of the exact Vector Search to isolate the source of your inaccuracies.

Ensure enough memory.

MongoDB Vector Search uses hierarchical navigable small worlds for efficient $vectorSearch queries. HNSW is a layered graph algorithm that works efficiently when the vector data is held in memory. To achieve optimal performance, you must ensure the data nodes have enough RAM to hold the entire index and vector data in memory. If you do not have enough RAM to hold the indexes in memory, then some percentage of the vector index will need to be read from the disk at query time.

This will trigger a large number of page faults, which can lead to a significant slowdown in performance. To calculate how much memory is required for a vector index, multiply the number of vectors by the number of vector dimensions times 4 bytes. Suppose we want to store 100,000 vectors, each of 1,536 dimensions. To estimate the amount of memory required, we would multiply 100,000 by 1,536 and multiply that by 4 to get the number of bytes, which is 614.4 million bytes, which approximates to 0.6 GB, so we would need to ensure that the data nodes we choose have enough RAM. This would give us an idea of choosing the right tier of data nodes or Atlas Search Nodes. In a coupled architecture, which means the default MongoDB Atlas model of running your database and Search Nodes on the same node or infrastructure, the MongoDB storage takes 50% of the node's available memory, leaving roughly 50% leftover for the Vector Search index and the Java Virtual Machine, or JVM, where the search process runs.

We recommend using Atlas Search Nodes for production. These nodes allow for workload isolation without data isolation. And the 90% of their RAM allocation can be used to store the vector data in indexes in memory, with the leftover being used for the JVM. Search Nodes can be configured at any point for your cluster, allowing for independent scaling of the Vector Search workload from the general database as well as high availability for your search queries.

We cover this more in the Learning Byte for deployment option for GenAI apps with MongoDB. 

Avoiding indexing vectors when running search queries. Documents are indexed when new entries are made or when existing entries are updated. This indexing process is computationally intensive and uses the computing resources from the search queries.

When indexing and searching happens simultaneously, the performance of Vector Search querying suffers. Therefore, it's best to avoid indexing large volumes of data when vector search is actively used for querying. An example of indexing a large volume of data is if you decide to reindex all the documents. For example, when changing the embedding model used to produce a vectors, it's best to reindex the new documents into a new index, rather than updating the one that's currently in use.

It's a good practice to stream updates to your index, rather than bulk updating them.

Warm up the file system cache. When using the coupled architecture and performing search operations for the first time on a new index, the latency is very high for the initial queries. This is because the file system cache is empty, meaning that the RAM only contains the HNSW graph, but no vector data.

While traversing through the HNSW graph, the initial queries will need to perform random disk seeks to fetch the vector data. To warm the cache, you need to execute sample queries on your index, which can cause all the vector data to be loaded into memory. However, this cache warming process must be repeated after large writes or when your vector index is rebuilt, as the data is not yet mapped to memory. If you are using a dedicated Search Node, it will automatically include a prewarmed cache when initialized, so we definitely recommend the use of dedicated Search Nodes.

After the cache warming is complete, the RAM usage and disk read IOPS will be elevated. This can be viewed by the Atlas metrics that we discussed earlier.

Exclude vector fields from the results. You can reduce the query latency by minimizing the fields returned as part of your search queries.

For a given query, vector search results will contain all the fields in a document, including the vector embedding field. The vector embeddings may be large and impact the query latency due to the increased network travel time the result payload takes. To avoid this scenario and improve query performance, use the $project stage to select only the fields you want to return in the results. This is a great way to reduce unnecessary data movement and free up resources.

We recommend excluding the vector search field and any other fields that you don't need in the $project stage. You can learn more about the $project stage in the resources linked below.

CPU bottlenecking. As mentioned earlier, vector search queries are computationally expensive and use of the CPUs available on the instance.

In the previous section on monitoring, we talked about the system CPU metric. Looking at it is a good way to figure out if the lack of CPUs are causing a slowdown. You can scale the instance so that it has more CPUs available to increase the query throughput and reduce latency. In a coupled architecture, this means upgrading to an instance with a higher number of VCPUs.

In a decoupled architecture, this means adding additional dedicated search nodes to account for the queries being round robin to the different nodes.

In this Learning Byte, we have explored crucial strategies for optimizing your Atlas Vector Search applications. Let's recap the main points. For performance monitoring, always measure performance with metrics to guide your optimization efforts.

When optimizing the embedding model, consider reducing vector dimensions to balance the accuracy and performance. When optimizing index, ensure you have sufficient memory for vector indexes. Also, avoid indexing while running search queries when possible. Regarding query performance tuning, make sure to warm up the file system cache.

Exclude vector fields from query results when possible. And address CPU bottlenecking by scaling resources. Remember, optimization is an ongoing process. Experiment with different configurations to find the optimal balance for your specific use case, but continue to monitor the performance and apply these techniques as your application evolves.

Thank you for joining us in this MongoDB Learning Byte. Keep exploring, and happy optimizing.