100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Azure

Cosmos DB Basics

Azure's globally distributed, multi-model NoSQL database service, covering APIs, partitioning, consistency levels, and request units.

Databases & IdentityBeginner10 min readJul 10, 2026
Analogies

What Is Azure Cosmos DB?

Azure Cosmos DB is a globally distributed, multi-model NoSQL database service that offers single-digit-millisecond latency, automatic horizontal scaling, and support for multiple APIs — including the native Core (SQL) API, MongoDB, Cassandra, Gremlin (graph), and Table — so applications can use familiar drivers and query languages while Cosmos DB manages replication and scaling underneath.

🏏

Cricket analogy: It's like a franchise that can field a team under IPL rules, T20I rules, or ODI rules depending on the match without rebuilding the squad — Cosmos DB lets the same underlying data be accessed via whichever API (SQL, Mongo, Cassandra) the application already speaks.

Partitioning and Throughput

Cosmos DB scales horizontally by distributing data across physical partitions based on a partition key you choose, and throughput is measured in Request Units per second (RU/s) — provisioned at the database or container level, or consumed on-demand with autoscale or serverless modes — so choosing a partition key with high cardinality and even access distribution is critical to avoid hot partitions.

🏏

Cricket analogy: Choosing a good partition key is like a tournament organizer scheduling matches across many grounds by team name rather than funneling every match through one stadium — an even spread (like alphabetically distributing 20 IPL teams) avoids one venue becoming a bottleneck.

Consistency Levels

Cosmos DB offers five tunable consistency levels — Strong, Bounded Staleness, Session, Consistent Prefix, and Eventual — trading off latency, availability, and throughput against read freshness, with Session consistency (the default) guaranteeing that a single client always reads its own writes even as data replicates globally.

🏏

Cricket analogy: Session consistency is like a player checking the live scoreboard on their own phone app, which always reflects their own team's latest runs instantly, even if a fan across the stadium sees a slightly delayed update on the big screen.

javascript
// Node.js SDK: create a container with a partition key and query it
const { CosmosClient } = require("@azure/cosmos");
const client = new CosmosClient({ endpoint, key });
const database = client.database("ecommerce");

const { container } = await database.containers.createIfNotExists({
  id: "orders",
  partitionKey: { paths: ["/customerId"] },
});

await container.items.create({
  id: "order-1042",
  customerId: "cust-9981",
  amount: 249.99,
  status: "shipped",
});

const { resources } = await container.items
  .query("SELECT * FROM c WHERE c.customerId = @cid", {
    parameters: [{ name: "@cid", value: "cust-9981" }],
  })
  .fetchAll();

Cosmos DB's serverless mode charges purely per Request Unit consumed with no minimum provisioned throughput, making it well-suited for spiky or low-traffic workloads, while autoscale provisioned throughput scales RU/s between 10% and 100% of a configured maximum automatically.

Global Distribution

Cosmos DB lets you add or remove Azure regions to a database account with a single configuration change, transparently replicating data worldwide and enabling multi-region writes so applications can read and write to the nearest region, with automatic conflict resolution policies handling concurrent updates to the same item.

🏏

Cricket analogy: Multi-region writes are like the IPL and BBL both running franchise auctions independently and later reconciling player contracts through a governing body's conflict rules, rather than forcing every transaction through one central office in Mumbai.

Choosing a low-cardinality partition key (such as a fixed 'status' field with only three possible values) creates hot partitions that throttle throughput with 429 errors under load — always pick a key with many distinct values and even access patterns, ideally aligned with your most common query filter.

  • Cosmos DB is a globally distributed, multi-model NoSQL database with multiple API options.
  • Throughput is measured in Request Units per second (RU/s), provisioned, autoscale, or serverless.
  • Partition key choice determines horizontal scaling and must avoid hot partitions.
  • Five tunable consistency levels trade off latency and freshness; Session is the default.
  • Regions can be added or removed from an account with a single configuration change.
  • Multi-region writes enable low-latency local writes with automatic conflict resolution.
  • Serverless mode suits spiky workloads; autoscale suits variable but steady traffic.

Practice what you learned

Was this page helpful?

Topics covered

#Azure#AzureFundamentalsStudyNotes#CloudComputing#CosmosDBBasics#Cosmos#Partitioning#Throughput#Consistency#StudyNotes#SkillVeris