Azure Cosmos DB: Global NoSQL
Explore Cosmos DB's multi-model, multi-region architecture, understand partition keys, and choose the right consistency level for your application's needs.
Azure Cosmos DB: Global NoSQL is a free Cloud & IT Cert Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cloud & IT Cert Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Azure Cosmos DB?
Azure Cosmos DB is Microsoft's globally distributed, multi-model NoSQL database service. It is designed to deliver single-digit millisecond read and write latency at any scale, anywhere in the world. Unlike traditional databases constrained to a single region, Cosmos DB can transparently replicate data to dozens of Azure regions simultaneously.
Multi-Model API Support
Cosmos DB supports multiple database APIs through a single service. You can use the native NoSQL API (JSON documents), the MongoDB API for apps built on MongoDB drivers, the Apache Cassandra API for wide-column workloads, the Gremlin API for graph data, and the Table API for key-value data. Each API exposes Cosmos DB's underlying engine through a familiar interface.
Accounts, Databases, and Containers
The Cosmos DB resource hierarchy consists of three levels. An account is the top-level resource that defines the replication region list and consistency level. Inside an account, you create databases as logical namespaces. Inside each database, you create containers — the unit that holds items (documents) and where throughput is provisioned.
# Create a Cosmos DB account with the NoSQL API
az cosmosdb create \
--name mycosmosaccount \
--resource-group myRG \
--kind GlobalDocumentDB \
--locations regionName=eastus failoverPriority=0 isZoneRedundant=true
# Create a database
az cosmosdb sql database create \
--account-name mycosmosaccount \
--resource-group myRG \
--name myDatabasePartition Keys: The Scaling Foundation
A partition key is a property on every item that Cosmos DB uses to distribute data across physical partitions. Choosing the right partition key is critical: it should have high cardinality (many distinct values), spread write traffic evenly, and appear in the majority of your queries. Poor partition key choices lead to hot partitions that throttle throughput and degrade performance.
// Example container with /userId as partition key
// Items in the container:
{
'id': 'order-001',
'userId': 'user-42',
'product': 'Widget',
'total': 29.99
}
// userId distributes orders across partitionsThroughput: RU/s Model
Cosmos DB charges for throughput in Request Units per second (RU/s). One RU is roughly the cost of reading a 1 KB document by its ID. Writes, queries, and deletes cost more RUs depending on item size and index complexity. You can provision RU/s at the container level or share a pool across all containers in a database, and switch to autoscale mode to let Cosmos DB scale throughput automatically.
# Create a container with 400 RU/s
az cosmosdb sql container create \
--account-name mycosmosaccount \
--resource-group myRG \
--database-name myDatabase \
--name orders \
--partition-key-path '/userId' \
--throughput 400Global Distribution
Cosmos DB's most powerful feature is its ability to replicate data globally with a few clicks. You add regions to your account, and Cosmos DB synchronises all writes across every region automatically. Each region can serve both reads and writes simultaneously in a multi-write (multi-master) configuration, minimising latency for geographically dispersed users.
# Add a second region to the Cosmos DB account
az cosmosdb update \
--name mycosmosaccount \
--resource-group myRG \
--locations regionName=eastus failoverPriority=0 \
regionName=westeurope failoverPriority=1Five Consistency Levels
Cosmos DB offers five tunable consistency levels as a spectrum from strongest to weakest: Strong (linearizable reads, highest latency), Bounded Staleness (reads lag behind writes by a bounded window), Session (default — consistent reads for your own writes within a session), Consistent Prefix (reads never see out-of-order writes), and Eventual (fastest reads, weakest guarantees). Choose based on your application's tolerance for stale data.
Automatic Indexing
By default, Cosmos DB automatically indexes every property in every item you insert, without requiring you to define a schema upfront. This enables flexible ad-hoc queries without index maintenance. You can customise the indexing policy to exclude paths that are never queried (reducing storage cost) or add composite indexes for ORDER BY queries across multiple properties.
// Custom indexing policy example
{
'indexingMode': 'consistent',
'includedPaths': [{ 'path': '/*' }],
'excludedPaths': [
{ 'path': '/largeTextBlob/?' },
{ 'path': '/_etag/?' }
]
}Change Feed for Event-Driven Apps
The change feed is a sorted log of all create and update operations on a Cosmos DB container. Applications subscribe to the change feed to trigger downstream processing — for example, updating a search index, sending notifications, or writing to a materialised view. Change feed is the foundation for event-driven and CQRS patterns in Cosmos DB architectures.
Time-to-Live (TTL) for Expiring Data
Cosmos DB supports Time-to-Live (TTL), a property you set on a container or individual items to automatically delete them after a specified number of seconds. This is ideal for session data, cache entries, audit logs with regulatory retention windows, or any data with a known expiration. TTL deletions are performed as background tasks and do not consume provisioned RU/s.
// Enable TTL on a container (TTL in seconds)
az cosmosdb sql container update \
--account-name mycosmosaccount \
--resource-group myRG \
--database-name myDatabase \
--name sessions \
--ttl 3600Pricing and Serverless Tier
Cosmos DB offers two billing modes. Provisioned throughput bills you per RU/s-hour regardless of actual usage, suited to steady predictable workloads. Serverless mode bills per RU consumed and GB stored, with no minimum cost, making it cost-effective for development, prototyping, and traffic spikes. Serverless Cosmos DB is currently limited to a single region.
Quick Check
Test your understanding of Microsoft Azure Fundamentals (AZ-900) concepts from this lesson.
Lesson Recap
In this lesson you learned: Cosmos DB is a globally distributed, multi-model NoSQL service, partition keys determine how data is sharded and must be chosen carefully, and five consistency levels let you balance latency and data freshness. Next up we explore Azure's managed services for open-source database engines.
Frequently asked questions
Is the “Azure Cosmos DB: Global NoSQL” lesson free?
Yes — the full text of “Azure Cosmos DB: Global NoSQL” is free to read here on the web, and the Cloud & IT Cert Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cloud & IT Cert Prep course, upgrade to CoddyKit PRO.
What will I learn in “Azure Cosmos DB: Global NoSQL”?
Explore Cosmos DB's multi-model, multi-region architecture, understand partition keys, and choose the right consistency level for your application's needs. You practise Cloud & IT Cert Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cloud & IT Cert Prep?
No prior experience is required. Cloud & IT Cert Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Azure Cosmos DB: Global NoSQL” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cloud & IT Cert Prep lesson?
Yes. Every Cloud & IT Cert Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Azure SQL Database
- Azure Cosmos DB: Global NoSQL
- Azure Database for Open-Source Engines
- Database Migration to Azure