Create a Cosmos DB account (free tier) and add data
In this task you'll create a Cosmos DB account and add data.
Azure Cosmos DB is a NoSQL database for data that doesn't fit neatly into tables: items are stored as JSON documents, and each one can have different fields. It's fast at any size and can copy data to regions around the world.
Every container has a partition key, such as /category, which decides how data is spread out. Speed is measured in request units (RU/s). The free tier gives one account per subscription 1,000 RU/s and 25 GB at no charge.
Example. Larkfield Group stores its product catalogue in Cosmos DB, with the partition key /category. A teapot item has a capacity field and a mug item has a colour field, and both sit happily in the same container.
Before you start
You need: Contributor on the resource group. Time: 15 minutes.
Where to look
- Home
- Azure Cosmos DB
- + Create
- Azure Cosmos DB for NoSQL
Steps
- Open Azure Cosmos DB, select + Create, then Create under Azure Cosmos DB for NoSQL.
- Choose rg-portal-practice, a unique account name such as cosmos-portal plus your initials, and your region. If it asks for a Workload Type, choose Learning or Development/Testing.
- Set Capacity mode to Provisioned throughput, set Apply Free Tier Discount to Apply, and keep Limit total account throughput ticked.Free tier works only with provisioned throughput, and only one account per subscription can have it. The limit stops you going over the free 1,000 RU/s.
- Leave geo-redundancy and multi-region writes off, then Review + create and Create. It takes a few minutes.
- Open the account and select Data Explorer, then + New Container (it may sit under a + New... menu). Create a database shop and a container products with the partition key /category, and keep the throughput at 400 RU/s or less.
- A Welcome! What is Cosmos DB? window may open the first time: close it. Open products, select Items, then New Item (in some versions it's in the ... menu next to the container). Paste
{ "id": "1", "name": "Teapot", "category": "kitchen", "price": 18.5 }and select Save. - Select New SQL Query, run
SELECT * FROM c WHERE c.category = 'kitchen'and look at the result and its request charge.A query that includes the partition key reads one partition, so as the data grows it costs fewer RUs than one that has to search them all. With one or two items, both cost about 3 RUs. - Open Default consistency under Settings. Read the five levels from Strong (every read sees the latest write, slowest) to Eventual (fastest, but reads may briefly be out of date). Leave it on Session.Session means each user always sees their own changes, which suits most apps.
- Open Replicate data globally and look at the map, but don't add a region: each extra region is billed.
Commands
# A free-tier Cosmos DB account for NoSQL
az cosmosdb create --resource-group rg-portal-practice --name cosmos-portal-<initials> \
--enable-free-tier true --default-consistency-level Session
# A database and a container with a partition key
az cosmosdb sql database create --resource-group rg-portal-practice \
--account-name cosmos-portal-<initials> --name shop
az cosmosdb sql container create --resource-group rg-portal-practice \
--account-name cosmos-portal-<initials> --database-name shop \
--name products --partition-key-path /category --throughput 400
Watch out for
- Cost
- Free tier covers 1,000 RU/s and 25 GB for the account's whole life. Anything above that, and serverless or extra regions, is billed. Keep the throughput limit ticked.
- Security
- The account keys give full access. Prefer Entra ID role-based access for apps, and turn off key-based access where you can. Limit network access with firewall rules or private endpoints.
- Common mistakes
- Forgetting to apply the free tier at creation: it can't be added later. A failed create can still hold the free tier, so delete the failed account before trying again. "High demand in this region" means pick another region. Choosing a partition key with few values, such as true or false, which squeezes all the data into a few partitions.
Clean up
- Resource groups
- rg-portal-practice
- Delete resource group
You've finished the databases section: delete the resource group. It removes the SQL server and databases and the Cosmos DB account. The free tier becomes available again for a new account.
Words to know
- Item
- One JSON document in Cosmos DB, such as one product.
- Container
- A collection of items with the same partition key, like a table without fixed columns.
- Partition key
- A field used to spread items across storage. Pick one with many different values.
- RU/s
- Request units per second: the throughput you reserve. Reading one small item costs about one RU.
- Consistency level
- How up to date a read is guaranteed to be when data is copied to several places: Strong, Bounded staleness, Session (the default), Consistent prefix or Eventual.
Tick off each step and track your progress in the guide.
Open this task in Azure Portal, Hands On