Overview
Azure Cosmos DB is a fully managed NoSQL database service that offers turnkey global distribution, low latency, and multi‑model support. This tutorial walks you through provisioning a Cosmos DB account, creating a database and container, and performing CRUD operations using the Azure portal and SDK.
Getting Started
Prerequisites:
- Azure subscription
- Azure CLI or Azure Portal access
- Node.js (v14+) installed locally
1. Sign in to Azure Portal and search for Cosmos DB.
2. Click Create → Core (SQL) (NoSQL).
3. Fill out the form (resource group, account name, API = Core (SQL)), then click Review + create.
Create a Database
Using Azure Portal:
- Navigate to your Cosmos DB account.
- Select Data Explorer → New Database.
- Enter
TutorialDBas the name and set Throughput to400 RU/s. - Click OK.
Using Azure CLI:
az cosmosdb sql database create \
--account-name MyCosmosAccount \
--resource-group MyResourceGroup \
--name TutorialDB \
--throughput 400
Create a Container
Container stores your items. Define a partition key for efficient scaling.
az cosmosdb sql container create \
--account-name MyCosmosAccount \
--resource-group MyResourceGroup \
--database-name TutorialDB \
--name Items \
--partition-key-path "/category"
Resulting container:
| Name | Partition Key | Throughput |
|---|---|---|
| Items | /category | 400 RU/s |
Query Data with Node.js SDK
Install the SDK:
npm install @azure/cosmos
Sample script (save as app.js):
const { CosmosClient } = require("@azure/cosmos");
const endpoint = "https://<your-account>.documents.azure.com:443/";
const key = "<your-primary-key>";
const client = new CosmosClient({ endpoint, key });
async function run() {
const database = client.database("TutorialDB");
const container = database.container("Items");
// Create item
const { resource: createdItem } = await container.items.create({
id: "item1",
category: "books",
title: "Learn Azure Cosmos DB",
price: 29.99
});
console.log("Created item:", createdItem.id);
// Query items
const querySpec = {
query: "SELECT * FROM c WHERE c.category = @category",
parameters: [{ name: "@category", value: "books" }]
};
const { resources: results } = await container.items.query(querySpec).fetchAll();
console.log("Query results:", results);
}
run().catch(console.error);
Scaling & Throughput
Cosmos DB offers two throughput models:
- Provisioned RU/s: Fixed throughput; ideal for predictable workloads.
- Serverless: Pay-per-request; best for sporadic traffic.
Enable autoscale to automatically adjust RU/s up to a maximum you define.
az cosmosdb sql container update \
--account-name MyCosmosAccount \
--resource-group MyResourceGroup \
--database-name TutorialDB \
--name Items \
--throughput-autoscale-max 4000
Best Practices
- Choose a partition key with high cardinality and even data distribution.
- Leverage point reads (ReadItemAsync) for low latency.
- Use TTL to auto-delete stale data when appropriate.
- Monitor RU consumption with Azure Monitor and set alerts.
- Enable Multi‑Region Writes for active‑active solutions.