Azure Cosmos DB – Comprehensive Cheat Sheet
Overview
Azure Cosmos DB is Microsoft’s fully managed, globally distributed, multi-model database service.
- Key Features: Schema-less, multi-model, global distribution, 99.999% SLA, elastic scalability,
single-digit millisecond latency.
- Supported APIs: SQL (Core), MongoDB, Cassandra, Gremlin, Table API.
Architecture
- Multi-model engine supporting JSON document, key-value, columnar, and graph data.
- Partitioned storage engine ensures horizontal scalability.
- Multi-region replication engine supports active-active (multi-master) deployments.
- Automatic indexing for schema-less queries.
Key Concepts
- **Account**: Top-level namespace.
- **Database**: Logical grouping of containers.
- **Container**: Stores items (documents, rows, graph nodes/edges).
- **Item**: Single JSON document (up to 2 MB).
- **Partition Key**: Determines logical partition placement.
- **RU/s**: Request Units per second measure throughput.
APIs & Practical Examples
1. **SQL (Core API)**
Example:
SELECT * FROM c WHERE c.type = "order" AND c.amount > 100
2. **MongoDB API**
db.orders.find({"amount": {"$gt": 100}})
3. **Cassandra API**
SELECT * FROM orders WHERE customer_id = '1234';
4. **Gremlin API**
g.V().hasLabel('person').has('age', gt(30))
5. **Table API**
Table query via OData syntax.
Consistency Models
1. **Strong**: Linearizability, highest consistency.
2. **Bounded Staleness**: Configurable lag in time or versions.
3. **Session**: Guarantees consistency for a client session (default).
4. **Consistent Prefix**: Reads never see out-of-order writes.
5. **Eventual**: Lowest latency, eventual convergence.
Example:
cosmos_client = CosmosClient(url, key, consistency_level="Session")
Partitioning & Scaling
- Partition Key: Choose high-cardinality, evenly distributed field.
- Logical Partition: Group of items with same partition key value.
- Physical Partition: Backend unit (up to 50 GB).
- Scaling Options:
* Provisioned Throughput (RU/s).
* Autoscale Throughput (scales automatically).
* Serverless (pay per request).
Best Practice: Avoid hot partitions by choosing partition keys like userId or orderId, not country.
Indexing
- Automatic indexing of all properties.
- Indexing Modes: Consistent (default), Lazy, None.
- Index Types: Range, Spatial, Composite.
- Example: Excluding paths from indexing:
{
"indexingPolicy": {
"excludedPaths": [{"path": "/logs/*"}]
}
}
Pricing Model
- Billing based on:
* Provisioned RU/s (fixed or autoscale).
* Storage consumption (per GB/month).
* Multi-region replication adds cost per region.
- Example: 400 RU/s with 50 GB in 2 regions ≈ cost of RU + storage * 2.
Performance Tuning
Tips:
- Monitor RU consumption, minimize cross-partition queries.
- Use `IN` and equality filters with partition key for efficiency.
- Denormalize data for read-heavy workloads.
- Use bulk executor library for large migrations.
- Cache hot data with Azure Cache for Redis.
Example RU optimization query:
SELECT c.id, c.amount FROM c WHERE c.customerId = "1234" AND c.orderDate > "2023-01-01"
Multi-Region Deployment
- Enable replication across Azure regions.
- Options:
* Single-region write.
* Multi-master (multi-region write).
- Automatic Failover: Configurable priority list.
- Conflict Resolution: Last-write-wins (default) or custom logic.
Example (CLI):
az cosmosdb update --name mycosmos --resource-group rg --enable-multiple-write-locations true
Best Practices
■ Choose partition key wisely (high-cardinality, even distribution).
■ Use autoscale for unpredictable workloads.
■ Prefer session consistency for balanced performance.
■ Use stored procedures & triggers for transactions.
■ Monitor with Azure Monitor & Log Analytics.
■ Design for global distribution upfront.
Quick Reference
- Max Item Size: 2 MB.
- RU Cost: 1 KB read ≈ 1 RU, 1 KB write ≈ 5 RUs.
- SLA: 99.999% availability, <10ms latency, guaranteed throughput.
- Security: Keys, Azure AD, Resource Tokens; encryption at rest and in transit.