Azure Cosmos DB
Relational databases like Azure SQL work excellently for structured data with fixed schemas and complex queries. But modern applications — social networks, e-commerce platforms, IoT systems, gaming — deal with data that is massive in scale, globally distributed, and needs response times under 10 milliseconds anywhere in the world. Azure Cosmos DB is Microsoft's fully managed, globally distributed NoSQL database designed precisely for these extreme requirements.
What is Azure Cosmos DB?
Cosmos DB is a NoSQL database service that stores data in a flexible, schema-free format. It guarantees single-digit millisecond response times at any scale, distributes data globally across multiple Azure regions automatically, and maintains 99.999% availability (less than 5 minutes of downtime per year).
Cosmos DB vs SQL Database
| Feature | Azure SQL Database | Azure Cosmos DB |
|---|---|---|
| Data Model | Relational (tables, rows, columns) | Multiple models (JSON documents, key-value, graph, column) |
| Schema | Fixed — must define schema upfront | Flexible — each document can have different fields |
| Scaling | Vertical (bigger server) or horizontal (read replicas) | Automatic horizontal scaling to any size |
| Global Distribution | Geo-replication available but complex | Turnkey multi-region replication with one click |
| Latency Guarantee | Varies | Under 10ms reads and writes at 99th percentile |
| Best For | Financial records, HR systems, ERP applications | High-scale web apps, IoT, real-time, globally distributed apps |
Cosmos DB Resource Model
Azure Cosmos DB Account
│
└── Database: ecommerce-db
│
├── Container: products ← Like a table or collection
│ Partition Key: /categoryId
│ ├── Item: {id:"001", name:"Laptop", categoryId:"electronics", price:65000}
│ ├── Item: {id:"002", name:"Phone", categoryId:"electronics", price:25000}
│ └── Item: {id:"003", name:"Shirt", categoryId:"clothing", price:999, size:"M"}
│
└── Container: orders
Partition Key: /customerId
├── Item: {id:"ord-1", customerId:"c-100", total:65000, items:[...]}
└── Item: {id:"ord-2", customerId:"c-101", total:1998}
Notice that the products container has items with different structures (the clothing item has a "size" field that electronics items don't). This flexible schema is a core NoSQL advantage.
APIs Supported by Cosmos DB
One of the most powerful features of Cosmos DB is that it supports multiple database APIs. The same underlying Cosmos DB service speaks the language of multiple popular databases — so existing applications built for MongoDB, Cassandra, or Gremlin can use Cosmos DB without rewriting the application code.
| API | Data Model | Best For |
|---|---|---|
| NoSQL API (native) | JSON documents | New cloud-native applications — recommended default |
| MongoDB API | BSON documents | Migrating existing MongoDB applications to Azure |
| Cassandra API | Wide-column (CQL) | Migrating Apache Cassandra workloads |
| Gremlin API | Graph (vertices and edges) | Social networks, recommendation engines, fraud detection |
| Table API | Key-value (like Azure Table Storage) | Migrating Azure Table Storage apps needing global scale |
| PostgreSQL API (distributed) | Relational + NoSQL scale | PostgreSQL apps needing horizontal sharding |
Partition Key — The Most Important Design Decision
Cosmos DB scales by distributing data across physical partitions. The partition key is a property in each document that Cosmos DB uses to distribute data evenly. Choosing the wrong partition key creates hot partitions — where one partition handles all the traffic while others are idle — killing performance and wasting money.
Good vs Bad Partition Key Examples
Container: orders (millions of orders)
BAD Partition Key: /status
Values: "pending", "shipped", "delivered"
Problem: Only 3 possible values → All "pending" orders go to one partition
→ Hot partition, performance degrades under load
GOOD Partition Key: /customerId
Values: "c-001", "c-002", "c-003", ... "c-1000000"
Benefit: Millions of unique values → Even distribution across partitions
→ Each partition gets roughly equal load
Request Units (RU/s)
Cosmos DB measures throughput in Request Units per second (RU/s). One RU represents the cost of reading a 1 KB document. Write operations cost more RUs than reads. Complex queries cost more RUs than simple point reads.
- Provisioned Throughput: Reserve a fixed number of RU/s. Predictable cost and performance. Can be auto-scaled between a minimum and maximum RU/s.
- Serverless Mode: Pay per RU consumed, with no reserved capacity. Ideal for development and workloads with infrequent or unpredictable traffic.
Global Distribution
Cosmos DB can replicate data to any number of Azure regions with a single click. Once configured, reads and writes are served from the nearest region to the user — providing globally fast performance without complex replication logic in the application.
Consistency Levels
When data is replicated across regions, there is a trade-off between consistency (always reading the latest data) and performance (faster reads that may return slightly old data). Cosmos DB offers five tunable consistency levels:
| Level | Guarantee | Performance |
|---|---|---|
| Strong | Always reads the latest committed data — no stale reads | Highest latency — not recommended for multi-region writes |
| Bounded Staleness | Reads lag behind writes by at most K versions or T seconds | Good for global apps needing near-real-time consistency |
| Session | Within a session: reads see all writes made by the same session | Default and recommended for most applications |
| Consistent Prefix | Reads never see out-of-order writes — always see writes in sequence | Good for social feeds and activity logs |
| Eventual | Reads may return stale data — will eventually converge to latest | Lowest latency, highest availability — for non-critical data |
Change Feed
Cosmos DB Change Feed is a persistent log of all changes (inserts and updates) made to a container, in the order they occurred. Applications can read the change feed to react to data changes in real time — for example, triggering an Azure Function to send a notification whenever a new order is placed in the orders container.
Key Takeaways
- Azure Cosmos DB is a fully managed, globally distributed NoSQL database with sub-10ms latency guarantees.
- It supports multiple APIs: NoSQL, MongoDB, Cassandra, Gremlin, Table — enabling migration of existing NoSQL workloads.
- The partition key is the most critical design decision — it determines how data is distributed and whether the system scales efficiently.
- Throughput is measured in RU/s — provision a fixed amount or use serverless mode for variable workloads.
- Five consistency levels (Strong to Eventual) allow trading consistency for performance depending on the use case.
- Change Feed provides a real-time stream of all changes to trigger downstream processing.
