0
0
Azurecloud~5 mins

Cosmos DB overview and use cases in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes apps need to store data that can be accessed quickly from anywhere in the world. Cosmos DB is a cloud database that helps apps do this by keeping data close to users and making it easy to find and update.
When you want your app to work fast for users in different countries by storing data near them.
When your app needs to handle lots of users reading and writing data at the same time without slowing down.
When you want to store different types of data like documents, key-value pairs, or graphs in one place.
When you want your app to keep working even if one part of the cloud has a problem.
When you want to easily scale your database up or down as your app grows or shrinks.
Commands
This command creates a new Cosmos DB account named 'example-cosmosdb' in the resource group 'example-group' with the East US region. It sets the consistency level to Session for balanced speed and accuracy.
Terminal
az cosmosdb create --name example-cosmosdb --resource-group example-group --locations regionName=EastUS failoverPriority=0 isZoneRedundant=false --default-consistency-level Session --kind GlobalDocumentDB
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-group/providers/Microsoft.DocumentDB/databaseAccounts/example-cosmosdb", "name": "example-cosmosdb", "type": "Microsoft.DocumentDB/databaseAccounts", "location": "eastus", "properties": { "databaseAccountOfferType": "Standard", "consistencyPolicy": { "defaultConsistencyLevel": "Session" }, "locations": [ { "locationName": "East US", "failoverPriority": 0, "isZoneRedundant": false } ] } }
--name - Sets the name of the Cosmos DB account
--resource-group - Specifies the Azure resource group to use
--default-consistency-level - Defines how consistent the data reads are
This command creates a new SQL API database called 'example-database' inside the Cosmos DB account.
Terminal
az cosmosdb sql database create --account-name example-cosmosdb --resource-group example-group --name example-database
Expected OutputExpected
{ "id": "/dbs/example-database", "resource": { "id": "example-database" }, "_rid": "someRid", "_self": "dbs/someRid/", "_etag": "\"0000\"", "_colls": "colls/", "_users": "users/" }
--account-name - Specifies which Cosmos DB account to use
--name - Sets the name of the new database
This command creates a container named 'example-container' inside the 'example-database'. The container uses '/category' as the partition key to organize data efficiently.
Terminal
az cosmosdb sql container create --account-name example-cosmosdb --resource-group example-group --database-name example-database --name example-container --partition-key-path /category
Expected OutputExpected
{ "id": "/dbs/example-database/colls/example-container", "resource": { "id": "example-container", "partitionKey": { "paths": [ "/category" ], "kind": "Hash" } }, "_rid": "someRid", "_self": "dbs/someRid/colls/someRid/", "_etag": "\"0000\"" }
--database-name - Specifies the database to add the container to
--partition-key-path - Defines the key used to split data for performance
This command shows details about the 'example-container' to confirm it was created correctly.
Terminal
az cosmosdb sql container show --account-name example-cosmosdb --resource-group example-group --database-name example-database --name example-container
Expected OutputExpected
{ "id": "/dbs/example-database/colls/example-container", "resource": { "id": "example-container", "partitionKey": { "paths": [ "/category" ], "kind": "Hash" } }, "_rid": "someRid", "_self": "dbs/someRid/colls/someRid/", "_etag": "\"0000\"" }
Key Concept

If you remember nothing else from this pattern, remember: Cosmos DB stores data close to users worldwide and organizes it for fast, reliable access.

Common Mistakes
Not specifying a partition key when creating a container
Without a partition key, Cosmos DB cannot distribute data efficiently, causing slow performance and limits on data size.
Always set a meaningful partition key path that matches how your app queries data.
Using strong consistency in global apps without considering latency
Strong consistency can slow down data access for users far from the main region.
Use session or eventual consistency for better speed in global apps.
Summary
Create a Cosmos DB account with az cosmosdb create to set up your global database.
Add a SQL API database inside the account to organize your data.
Create containers with partition keys to store and access data efficiently.
Use show commands to verify your resources are created correctly.