Azure Cosmos DB
What Is Azure Cosmos DB?
Azure Cosmos DB is a fully managed NoSQL database service from Microsoft Azure. It is designed for applications that need:
- Very fast response times (low latency)
- High availability (always on, across regions)
- Automatic scaling to handle large amounts of data and traffic
- Global distribution (data close to users around the world)
In simple terms, think of Azure Cosmos DB as a super-fast, globally distributed
database that can store different types of data (JSON documents, key-value, graphs, etc.)
and serve millions of users at the same time with very low delays.
Why Was Azure Cosmos DB Created?
Modern apps like social networks, e-commerce sites, gaming platforms, and IoT solutions:
- Need to handle a huge number of users and devices
- Need to be available in many regions or countries
- Need predictable, fast performance
Traditional databases struggle with these needs at large scale. Azure Cosmos DB was created to
solve this by being:
- Globally distributed from day one
- Elastic (easy to scale up and down)
- Multi-model (supports different data types and APIs)
Key Concepts
Multi-Model Database
Azure Cosmos DB can work like several types of databases depending on which API you choose:
- Core (SQL) API – Stores data as JSON documents and queries with a SQL-like language.
- MongoDB API – Works like MongoDB so you can use MongoDB tools and drivers.
- Table API – Key-value storage similar to Azure Table Storage.
- Gremlin API – For graph databases (nodes and edges, used in social networks, recommendations, etc.).
- Cassandra API – For workloads that expect Apache Cassandra behavior.
Global Distribution
You can replicate your data to multiple Azure regions with a few clicks. This means:
- Users connect to the region closest to them
- Faster responses
- Data is still available even if a region fails
Containers and Items
In Cosmos DB, data is organized as:
- Account – Top level (like a project).
- Database – Groups containers.
- Container – Similar to a table or collection; main logical unit for storage and scaling.
- Items – Actual records (JSON documents, key-value pairs, etc.).
Request Units (RUs)
Performance and cost are managed using Request Units (RUs).
An RU is a measure of the resources needed to perform operations like reads, writes, and queries.
You can think of RUs as a “performance budget” per second. The more RUs you provision:
- The more operations per second you can handle
- The higher your throughput
Partitioning
When your data becomes large, Cosmos DB automatically splits it into partitions.
You choose a partition key (a property in your items), and Cosmos DB uses it to distribute data
and load efficiently. Good partition key design is important for performance and scalability.
Features of Azure Cosmos DB
Global Distribution and Multi-Region Writes
- Replicate data across any number of Azure regions.
- Option for multi-region writes so users can write data in their nearest region.
- Automatic failover if a region goes down.
Low Latency and High Availability
- Designed to provide millisecond-level read and write latencies.
- Up to 99.999% availability SLA in some configurations.
Flexible Consistency Levels
Cosmos DB offers five consistency levels to balance between performance and consistency:
- Strong – Most consistent, slower, similar to traditional databases.
- Bounded Staleness – Slightly stale data within specified limits.
- Session – Default; a user sees their own writes immediately.
- Consistent Prefix – Reads never see out-of-order writes.
- Eventual – Fastest; data becomes consistent eventually.
Multi-Model and Multi-API Support
- Document, key-value, graph, and column-family data models.
- APIs for SQL, MongoDB, Cassandra, Gremlin, and Table.
- Lets you use familiar tools and SDKs based on your background.
Automatic Scaling and Elasticity
- Scale throughput up or down programmatically or via the portal.
- Autoscale option to automatically handle variable workloads.
Managed Service
- No need to manage servers, OS, patches, or cluster configuration.
- Built-in backups and security controls (encryption, RBAC, VNET integration, etc.).
Integrated with the Azure Ecosystem
- Works well with Azure Functions, Azure App Service, AKS, Logic Apps, and more.
- Can be used as a core component in serverless and microservices architectures.
When to Use Azure Cosmos DB
- Real-time web apps and APIs needing very low latency.
- Global applications with users across the world.
- IoT and telemetry data ingestion at high scale.
- Personalization, recommendation engines, and user profiles.
- Applications that need flexible schemas (documents) instead of fixed relational schemas.
Azure Cosmos DB vs Azure SQL Database
Azure Cosmos DB and Azure SQL Database are both database services in Azure,
but they are designed for different types of applications. The table below
compares key aspects.
| Aspect | Azure Cosmos DB | Azure SQL Database |
|---|---|---|
| Database Type | NoSQL, multi-model (document, key-value, graph, column-family) | Relational (SQL) database |
| Data Structure | Schema-less JSON documents and other NoSQL models | Structured tables with fixed schemas (rows and columns) |
| Query Language | SQL-like query for JSON + other APIs (MongoDB, Cassandra, etc.) | Transact-SQL (T-SQL) |
| Use Cases | High-scale, globally distributed, low-latency apps; IoT; real-time analytics; user profiles | Business apps, transactional systems, reporting, traditional line-of-business apps |
| Global Distribution | Built-in, multi-region read and write with a few clicks | Geo-replication available, but not as native or flexible for multi-master writes |
| Consistency Levels | Five tunable consistency levels (Strong to Eventual) | Primarily strong consistency within a single primary replica |
| Scaling Model | Automatic partitioning; scale throughput (RUs) and storage independently | Scale by changing compute tiers, read replicas, or sharding manually |
| Latency | Optimized for single-digit millisecond latency at the 99th percentile | Low latency but not tuned specifically for global, ultra-low latency at massive scale |
| Transaction Support | Limited transactional support (mainly scoped to a partition or via stored procedures) | Full ACID transactions across tables and rows |
| Schema Flexibility | Highly flexible; schema-less, easy to change document structure | Schema changes require ALTER operations; more rigid |
| Best For | Modern internet-scale apps, microservices, event-driven and serverless architectures | Traditional business applications, financial systems, apps needing complex joins and strong relational integrity |
| Pricing Model | Based on provisioned or autoscale Request Units (RUs) and storage | Based on compute (DTUs/vCores), storage, and features |
| APIs / Compatibility | Core (SQL) API, MongoDB, Cassandra, Gremlin, Table | SQL Server compatible (T-SQL, ADO.NET, ODBC, etc.) |
How to Think About Choosing Between Them
- If your app needs flexible JSON data, huge scale, global distribution,
and ultra-low latency, Azure Cosmos DB is usually a better fit. - If your app relies on complex joins, transactions, and traditional relational data models,
Azure SQL Database is usually the better choice. - Many real-world solutions use both: Cosmos DB for high-scale, user-facing parts,
and SQL Database for reporting and transactional business logic.