Domain 2 β€” Module 4 of 6 67%
11 of 30 overall
Domain 2: Design Data Storage Solutions Free ⏱ ~20 min read

Cosmos DB & Semi-Structured Data

Consistency models, partitioning strategies, and API choices β€” design a globally distributed NoSQL solution that balances performance, consistency, and cost.

Why Cosmos DB design matters

Simple explanation

Cosmos DB is like a global postal service that guarantees delivery speed β€” but you choose how β€œfresh” the letter needs to be.

It’s Azure’s globally distributed NoSQL database. The three big design decisions: Which API? (NoSQL, MongoDB, Cassandra, Gremlin, Table), Which consistency level? (strong to eventual β€” a tradeoff between correctness and speed), and How to partition? (the partition key determines performance and cost).

Cosmos DB consistency models

This is one of the most exam-tested topics in AZ-305. The five consistency levels form a spectrum:

Cosmos DB Consistency Levels
LevelGuaranteeLatencyThroughputBest For
StrongReads always return most recent committed writeHighest (cross-region round-trip)LowestFinancial transactions, inventory counts
Bounded StalenessReads lag behind writes by at most K versions or T timeHighMedium-lowGlobal apps needing near-strong consistency with better perf
Session (default)Reads are consistent within a session (your own writes)MediumMediumMost applications β€” user sees their own updates immediately
Consistent PrefixReads never see out-of-order writesLowMedium-highSocial feeds, activity logs (order matters, staleness is OK)
EventualReads may return older data, eventually convergesLowestHighestView counters, likes, non-critical telemetry

🏦 Elena’s consistency decision: FinSecure’s trading platform needs Strong consistency for account balance reads β€” a trade must see the latest balance. But their customer activity feed uses Session consistency β€” each user sees their own activity immediately, but seeing other users’ activity with a slight delay is acceptable.

Exam tip: Session consistency is the default and most common answer

If the exam scenario doesn’t mention a specific consistency requirement, Session is almost always correct. It provides the β€œread your own writes” guarantee that most applications need, with good performance. Choose Strong only when the scenario explicitly mentions β€œmust always read the latest data across all users/regions” β€” and be ready for the performance/cost tradeoff.

API selection

Cosmos DB API Options
APIData ModelQuery LanguageBest For
NoSQLJSON documentsSQL-like queriesNew cloud-native apps, most common choice
MongoDBBSON documentsMongoDB query languageMigrating existing MongoDB applications
CassandraWide-column (tables)CQL (Cassandra Query Language)Migrating Cassandra workloads, high-write IoT
GremlinGraph (vertices + edges)Gremlin traversal languageSocial networks, recommendation engines, fraud detection
TableKey-value pairsOData queriesMigrating Azure Table Storage (better perf + global distribution)

Design rule: Choose the API based on existing application investment, not personal preference. If the app is already built on MongoDB, use the MongoDB API β€” don’t rewrite for NoSQL API. For new apps, NoSQL API is the recommended default.

Partition key design

The partition key is the most important Cosmos DB design decision. A bad partition key causes hot partitions, poor query performance, and wasted RUs.

PrincipleGood ExampleBad Example
High cardinalityuserId (millions of unique values)country (only ~200 values β†’ hot partitions)
Even distributiontenantId (uniform data per tenant)createdDate (all today’s data in one partition)
Query alignmentKey used in most WHERE clausesKey rarely used in queries (forces cross-partition queries)

πŸš€ Marcus’s partition strategy: NovaSaaS uses tenantId as the partition key for most containers β€” queries are always scoped to a tenant, data is evenly distributed, and cross-partition queries are rare.

Design decision: Hierarchical partition keys

For very large datasets, Cosmos DB supports hierarchical partition keys (up to 3 levels). Example: /tenantId/userId/sessionId. This allows:

  • Queries filtered by tenantId β†’ scoped to that tenant’s partitions
  • Sub-filtering by userId β†’ further narrowed
  • Even data distribution across the hierarchy

Use when a single partition key would create partitions that exceed the 20 GB logical partition limit.

Throughput models

Cosmos DB Throughput Options
FactorProvisioned (Manual)Provisioned (Autoscale)Serverless
RU allocationFixed RU/s you setScales between 10% and max RU/sNo pre-allocation β€” pay per request
BillingPer-hour for allocated RU/sPer-hour for the highest RU/s the system scaled to within that hourPer-RU consumed
Minimum cost400 RU/s minimum10% of max (e.g., 400 if max is 4000)Zero when idle
Best forPredictable, steady workloadsVariable workloads with known peaksDev/test, infrequent access, spiky traffic
Max throughputUnlimited (manual scaling)Unlimited (set max)5,000 RU/s per container, 1 TB storage per container

Azure Table Storage vs Cosmos DB Table API

FactorAzure Table StorageCosmos DB Table API
PerformanceVariable latencySingle-digit ms guaranteed
Global distributionSingle region (GRS for DR only)Multi-region active-active
ThroughputPer-partition limitsProvisioned or serverless RU/s
Secondary indexesPrimary key onlyAutomatic indexing on all properties
CostVery lowHigher (premium performance)
Best forSimple key-value, cost-sensitive, low trafficHigh-performance global key-value

Knowledge check

Question

What are the five Cosmos DB consistency levels from strongest to weakest?

Click or press Enter to reveal answer

Question

What makes a good Cosmos DB partition key?

Click or press Enter to reveal answer

Question

When should you choose autoscale RU/s over manual provisioned throughput in Cosmos DB?

Click or press Enter to reveal answer

Knowledge Check

πŸš€ NovaSaaS is designing a Cosmos DB container for user session data. Each tenant has 100-10,000 users. Most queries filter by tenant first, then by user. Session data per user is typically 2-5 KB. Which partition key should Marcus recommend?

Knowledge Check

🏦 Elena needs Cosmos DB for a global trading platform. Account balance reads must always return the latest committed write β€” even across regions. Which consistency level should she recommend?


Next up: Semi-structured data is designed β€” now let’s handle unstructured data β€” Blob, Data Lake & Azure Files.