Azure Cosmos DB
Overview
Azure Cosmos DB is Microsoft’s globally distributed, multi-model, NoSQL database service.
It’s designed for high availability, low latency, and horizontal scalability — across multiple regions if needed.
You can think of it as a serverless database platform built for modern cloud-native and distributed applications.
Key Features
-
Multi-model: Supports several data models:
-
Document (Core SQL API, MongoDB API)
- Key-value (Table API)
- Graph (Gremlin API)
- Column-family (Cassandra API)
- Global distribution: Read/write replicas in any Azure region
- Elastic scaling of throughput (RU/s) and storage
- Automatic indexing without schema or index management
- Millisecond latency guarantees for reads and writes
- 99.999% availability SLA (multi-region)
Advantages
Benefit | Description |
---|---|
Performance at scale | Single-digit millisecond read/write latency worldwide |
Global availability | Geo-redundancy and multi-master write capability |
Flexible schema | Schemaless — great for agile development |
Multiple APIs | Familiar data access with SQL, MongoDB, Cassandra, Gremlin, Table |
Integrated with Azure | Secure with RBAC, Private Link, managed identities, diagnostics |
Change Feed support | Event stream of data changes for real-time processing |
Drawbacks / Considerations
Limitation | Description |
---|---|
Cost | Cosmos DB can be expensive, especially with high RU/s provisioning |
RU-based pricing model | Can be unintuitive — need to plan throughput carefully |
No full SQL capabilities | Not a relational DB — no joins across collections or transactions |
Consistency trade-offs | You must choose between strong and eventual consistency models |
What is RU/s?
- Request Unit (RU) is the currency of throughput.
- Each operation (read, write, query) consumes RUs.
- You provision RU/s based on expected usage, or use serverless mode for pay-per-use.
Conceptual Use Case
Use Cosmos DB when:
- You need globally available, highly responsive data access
- Your data is semi-structured or unstructured
- You want to avoid managing indexes, partitions, or replication
- You are building IoT, real-time analytics, personalization, or e-commerce apps
- You need to serve a globally distributed user base
Example: Using Cosmos DB with SQL API in C
using Microsoft.Azure.Cosmos;
CosmosClient client = new CosmosClient("<connection-string>");
Database database = await client.CreateDatabaseIfNotExistsAsync("MyDatabase");
Container container = await database.CreateContainerIfNotExistsAsync("Products", "/category");
var product = new { id = "1", name = "Mug", category = "Kitchen" };
await container.CreateItemAsync(product, new PartitionKey("Kitchen"));
Global Distribution Example
Imagine a travel app with users in the UK, US, and Japan. With Cosmos DB, you can:
- Replicate data to Azure regions West Europe, East US, and Japan East
- Enable multi-region writes to keep all users fast and in sync
- Use conflict resolution policies if concurrent updates occur
Consistency Models
Azure Cosmos DB offers 5 consistency levels:
Level | Description |
---|---|
Strong | Linearizability (most strict) |
Bounded Staleness | Lag allowed (e.g. 100 writes, 5 seconds) |
Session | Per-client consistency (default) |
Consistent Prefix | Reads never see out-of-order writes |
Eventual | Lowest latency, least consistency |
You choose the trade-off between performance and data freshness.
When Not to Use Cosmos DB
Scenario | Better Option |
---|---|
Complex relational data with joins | Azure SQL Database |
Large-scale data warehousing | Azure Synapse Analytics, Data Lake |
Small workloads needing low cost | Azure Table Storage, SQLite, or Lite DB |
Strict ACID transactional behaviour | SQL Server or PostgreSQL |
Pricing
- You pay for:
- RU/s throughput
- Data storage (GB)
- Geo-replication (per region)
- Backup & restore
- Serverless mode is available for bursty or low-volume apps
Use Azure Cosmos DB Pricing Calculator to plan costs.
Further Learning
Summary Table
Feature | Cosmos DB Offers |
---|---|
Data Model | Document, key-value, column, graph |
API Support | SQL, MongoDB, Cassandra, Gremlin, Table |
Global Scale | Yes, with multi-region replication |
Consistency Levels | 5, from strong to eventual |
Indexing | Automatic and customizable |
Serverless Option | Yes |
Ideal For | Low-latency apps, IoT, mobile, analytics |