Azure Cosmos DB: Partition Keys, Costs, and Vector Search

December 2, 2026

PUBLISHED BY

Azure Cosmos DB is one of Microsoft’s database services designed for applications that need to scale, respond in milliseconds, and increasingly work with artificial intelligence. But adopting it is not simply a matter of switching database engines: it requires a different approach to data design.

This article explains what Cosmos DB is, why the partition key is the most important design decision, and how costs are calculated in RUs (Request Units), the unit Cosmos DB uses to measure the effort required by each operation.

It also shows how to build a RAG chat, an assistant that first searches through your own documents before responding, using Cosmos DB’s vector search capabilities: a search based on meaning rather than exact words. The figures presented are based on measurements taken from an in-house prototype.

What is Azure Cosmos DB?

Azure Cosmos DB is a managed, distributed database service. It offers several APIs, including NoSQL, MongoDB, Cassandra, Gremlin (graph databases), and Table. This article focuses on Azure Cosmos DB for NoSQL, its native API, which stores JSON documents and uses a query language very similar to SQL.

Its main characteristics can be summarized in three points:

  • It distributes data across partitions: it supports more data and higher traffic without requiring application redesign, provided the partition key is chosen correctly. It can also replicate data across multiple regions.
  • It is a managed service: Azure takes care of servers, patches, and backups.
  • It stores documents and vectors together: it allows searches based on meaning rather than just text matching, which is essential for AI applications.

When should you use Cosmos DB?

Cosmos DB does not replace a relational database such as SQL Server. They are tools designed for different problems and can coexist within the same solution. For example, SQL Server can handle the transactional core, while Cosmos DB manages a catalog or an assistant’s conversation history.

Cosmos DB is often a good choice for:

  • Catalogs where each product has different attributes.
  • Millisecond response times at scale and during demand spikes, such as ticket sales.
  • Telemetry from large numbers of devices continuously sending data.
  • AI assistants that need to store documents and their embeddings (numerical representations of the meaning of a text, explained later) in the same place.

On the other hand, a different tool may be more suitable for:

  • Highly relational data, with many relationships and transactions between entities. In these cases, SQL Server or Azure SQL remain the best options.
  • Heavy analytics workloads, because they compete for the same capacity used by the application.

For analytics, Microsoft recommends mirroring data to Microsoft Fabric, which replicates data in near real time without consuming the container’s RUs. The previous option, Azure Synapse Link, is no longer supported for new projects.

How does data design differ from a relational database?

For those coming from the relational database world, the following equivalences help illustrate the differences:

SQL ServerCosmos DB for NoSQL
Server or instanceAccount
DatabaseDatabase
TableContainer
RowItem (JSON document)
ColumnJSON property
JOIN between tablesData that is read together is stored together
Manually created indexesAutomatic indexing (configurable)

But the most important difference lies in how the data is designed.

In a relational database, data is normalized: customers, orders, and order details are stored in three separate tables and combined using JOINs when read.

In Cosmos DB, design starts with how the application queries the data. If the most common operation is reading an order together with its line items, those items are included in the order document: a single read, with no JOINs.

Which data should be stored together, and which should not?

Storing data together does not mean copying everything everywhere. Microsoft’s data modeling guidelines provide clear criteria:

  • Embed data when the relationship is one-to-few, the data changes infrequently, does not grow indefinitely, and is read together. Order line items are a good example.
  • Reference data (store only its ID) when the relationship is one-to-many without a defined limit, or when the data changes frequently. A customer’s personal information is one example.
  • Many-to-many relationships: instead of an intermediate table, use lists of IDs within documents.

For example, in an online store, an order stores only the customerId, while the customer’s information lives in a single document in its own container. If a customer changes their personal information, only one document needs to be updated, rather than every order associated with that customer.

Why is the partition key the most important design decision?

How data is organized

The hierarchy is simple: account → database → container → items.

Within each container, items are grouped according to the value of their partition key. A practical way to think about it is:

  • The key works like a label.
  • Each key value creates a folder called a logical partition. All orders belonging to the same customer are grouped together.
  • Azure stores these folders in physical partitions that it manages automatically. When necessary, it adds more physical partitions and redistributes the folders.

With this in mind, the official limits are easier to understand:

  • An item can be up to 2 MB in size.
  • Each logical partition can hold up to 20 GB.
  • Each partition, logical or physical, supports up to 10,000 RU/s.
  • A container’s storage is unlimited, and its throughput capacity can reach 1,000,000 RU/s (with the possibility of increasing it through support).

How data is organized: containers, logical partitions within physical partitions, and replication in another region. Customer names are illustrative.

Three reasons to choose it carefully

According to the partitioning documentation, the key matters for three reasons:

  • Performance: a query that includes the partition key goes directly to a single partition. Without the key, the engine has to query all partitions.
  • Cost: a poorly chosen key concentrates traffic in a small number of partitions (“hot partitions”). This can cause 429 errors (too many requests) and waste the provisioned capacity.
  • It is difficult to change: a document’s partition key value is immutable. Changing the key requires creating a new container and migrating the data.

How do you choose a good partition key?

Consider an online store. If the application queries orders by customer, a good partition key is the customer’s ID. The trap is choosing the “obvious” field, such as the product category: if most orders are for electronics, that partition will concentrate almost all the traffic.

Now consider truck telemetry. Thousands of trucks send their location every few seconds, and the application displays the route taken by a specific truck.

  • The date is a poor key: all trucks write data simultaneously to that day’s partition.
  • The truck ID distributes writes and keeps each truck’s route in a single place.
  • Even so, each truck’s partition grows continuously and has a 20 GB limit. For years of historical data, a hierarchical partition key (first the truck, then the month) or TTL (Time to Live: each document is automatically deleted after a specified period) may be more appropriate.

The general rule is to choose a key based on the field most frequently used for queries, with many distinct values that never change. For this reason, in a real-world system, the key is usually a stable identifier rather than a descriptive value such as a name.

Example orders using two different keys: grouping by category concentrates data in one partition, while grouping by customer distributes it. Names are illustrative.

How much does Cosmos DB cost? Request Units (RUs)

What is an RU?

Request Units measure the effort required by each operation. They normalize the use of CPU, memory, and disk reads or writes.

Some measurements from the prototype include:

  • Point read of a document of approximately 1 KB (using its ID and partition key): 1 RU.
  • Query using the partition key (a customer’s orders): approximately 3 RUs.
  • Vector search for the 5 closest chunks within a partition containing around 25 chunks: approximately 5 RUs.

The last cost depends on the volume of data: the more chunks per partition, the more RUs the search consumes. This is why measurements should be repeated using the actual expected data volume.

How is capacity provisioned?

You do not purchase individual RUs. Instead, you provision capacity per second (RU/s). There are three options:

  • Provisioned throughput: a fixed amount of RU/s, suitable for predictable traffic. The minimum per container is 400 RU/s.
  • Autoscale: you define a maximum, and capacity automatically adjusts between 10% and 100% of that value.
  • Serverless: no capacity is reserved, and you pay exactly for what you consume.

How much capacity does a chat with 600 users need?

A concrete example helps put things into perspective: an assistant that answers questions by searching internal documents, like the RAG chat described later in this article.

  • 600 users chatting, each asking approximately one question every 10 seconds, generate around 60 questions per second.
  • If each search costs approximately 5 RUs, the system needs around 300 RU/s. The 400 RU/s minimum is sufficient.
  • A load test simulating these searches confirmed this: no queries were rejected due to insufficient capacity, and consumption remained below 300 RU/s.

The test measured search operations only. If the conversation history is also stored, those writes consume additional RUs and must be measured separately.

According to the official Azure Pricing Calculator, consulted in 2026, 400 RU/s cost approximately USD 28 per month in a single region, with 20 GB of storage. This figure covers Cosmos DB only: the AI model that generates the responses is billed separately.

Can you run logic inside the database?

In addition to storing and querying data, Cosmos DB allows JavaScript code to run on the server, directly within the container. What distinguishes each type of object is when it executes:

  • Stored procedure: executes when explicitly invoked by name and with a partition key. It can read and write multiple documents within a transaction: either all operations succeed or none do.
  • UDF (User-Defined Function): executes within a query and calculates a value for each item without modifying data. For example: SELECT udf.conIVA(c.total) FROM c.
  • Trigger: executes alongside a write operation, either before or after the data is saved. Unlike SQL Server, it is not triggered automatically: the application must explicitly request it for each operation.

Stored procedures and triggers can only access a single partition key. To propagate a change elsewhere, you can use the Change Feed: changes are recorded, and a separate process can read them and react accordingly. This is useful, for example, for sending notifications, maintaining a copy in another container with a different partition key, or recalculating the vector of an edited document.

How can you use Cosmos DB to search by meaning?

Embeddings and vector search

An embedding is a list of numbers that represents the meaning of a text. Two sentences with similar meanings will be close to each other, even if they use different words.

For example, “Which cost center should I charge the expenses for the pilot plant to?” and “Where should I record the expenses incurred by the experimental facility?” will be much closer to each other than either is to “tiramisu recipe.”

Cosmos DB stores these vectors alongside the document and compares them using the VectorDistance function. Setting this up involves two decisions:

  • What the vector looks like (vector policy): where it is stored, its data type, number of dimensions, and distance function. These settings must match the model generating the embeddings. For example, text-embedding-3-small generates 1,536-dimensional vectors, and OpenAI recommends using cosine similarity.
  • How to search efficiently (index): quantizedFlat compares against all vectors in compressed form, while DiskANN performs approximate searches designed for large volumes, with lower latency and fewer RUs. More details are available in the vector search documentation.

It is important to define the vector policy correctly from the beginning: the settings of an already defined vector cannot be modified. To change them, the policy or index must be deleted and redefined.

What is RAG?

RAG (Retrieval-Augmented Generation) is a pattern in which relevant sections of an organization’s own documents are retrieved before generating a response. These sections are then provided to the language model so it can answer using that information rather than relying on its internal knowledge.

A practical example is a chat assistant that answers questions about a company’s purchase request process using its internal manuals:

  1. The manuals are divided into chunks of approximately 500 tokens (the unit models use to measure text, equivalent to a few hundred words). These chunks are converted into vectors and stored in Cosmos DB, using the process as the partition key (why this is sufficient is explained below).
  2. The user’s question is converted into a vector using the same embedding model.
  3. Since the chat is associated with a specific process, Cosmos DB searches only within that partition and returns the 5 closest chunks.
  4. A language model generates the response with instructions to use only those chunks and displays the sources.

Documents are divided into chunks because sending an entire manual is more expensive, may exceed the model’s context limit, and introduces irrelevant information. Microsoft explains this in its RAG chunking guide, which recommends testing different strategies with your own documents.

What did we learn from building a prototype?

A prototype featuring a test online store and a RAG chat based on internal manuals led to several takeaways:

  • Measure before sizing capacity. For 60 queries per second, the official calculator estimated 600 RU/s. In the prototype, with approximately 25 chunks per partition, the load test ran successfully within 400 RU/s, the minimum for a container. With more data, the measurements need to be repeated.
  • Validate using data that no model could know. We included an invented cost center code in the test documents. If the chat returns that exact code, the answer did not come from the model’s internal knowledge.
  • Search returns the closest chunks, whether or not they are relevant. Even when a question makes no sense, the system still returns 5 chunks. This is why sources should always be displayed and, in production, a minimum similarity threshold should be established through testing with real questions.
  • The limits hold up in practice. We confirmed that a stored procedure or trigger cannot read or write outside its partition or in another container, and that a document’s partition key value cannot be modified: Azure rejects the attempt.
  • There is a reason behind the rules. The chat’s “process” key has few distinct values, which goes against the general rule. However, it was sufficient for this test: each process had only a few chunks, and simulated consumption remained below 300 RU/s, well below the 10,000 RU/s limit per partition. With many more documents per process, this choice would need to be reconsidered.

Conclusion

Azure Cosmos DB is a powerful tool when an application needs to scale and combine its data with meaning-based search. However, its performance and cost depend largely on design decisions made at the beginning: how documents are modeled and, above all, which partition key is selected, since changing it later requires migrating the data. The actual cost depends on how each application queries the database.

For those getting started, Cosmos DB offers a free tier and an emulator for local development. And for those working with coding agents, the Azure Cosmos DB team has released the Azure Cosmos DB Agent Kit, a set of skills that brings its best practices for data modeling, partitioning, and indexing to these agents. We explore this approach in our article on [why skills matter in AI-assisted development].

Table of Contents
Share this post

Discover the latest news

Azure Cosmos DB: Partition Keys, Costs, and Vector Search

Azure Cosmos DB is one of Microsoft’s database services designed for applications that need to…

AI in action: smarter agents for developing and managing projects 

In the latest AI in action sessions, Urudata Software’s internal knowledge-sharing series, members of the…

Apache Airflow: A Standard in Data Orchestration

Every day, in almost any company, dozens of processes move data from one place to…

AI in action: new ideas for incorporating artificial intelligence into everyday work 

At Urudata Software, we continue to explore new ways to incorporate artificial intelligence into our…

IA Insider 2.0: How AI Agents Are Transforming the Way Businesses Work

For the second edition of IA Insider, Urudata Software brought together industry experts to discuss…

Research and development: How we choose the right AI model for every solution

Every week, new artificial intelligence models are released. Some promise stronger reasoning capabilities, others deliver…