1 · When Cosmos DB Fits
- Global distribution — replicate data to 60+ Azure regions with a single click
- Multi-model — document, key-value, graph, column-family, table
- <10 ms latency — SLA-backed P99 reads <10 ms, writes <15 ms
- Elastic scale — millions of RU/s, petabytes of storage
- Schema-agnostic — automatic indexing of all properties by default
Choose Cosmos DB when your workload is globally distributed, high-throughput, and latency-sensitive with flexible schemas.
2 · Available APIs
3 · Partition Keys & Data Modeling
Partition Key Best Practices
- High cardinality — many distinct values (e.g.,
/userId,/tenantId) - Even distribution — avoid keys that funnel traffic to one partition (hot partition)
- Query affinity — most queries include the partition key → single-partition reads
Anti-Patterns
- ❌ Low-cardinality keys (
/statuswith 3 values) → hot partitions - ❌ Timestamp as key → write-heavy on latest partition
- ❌ Frequent cross-partition queries → high RU cost, fan-out
Hierarchical partition keys (preview): combine up to 3 levels (e.g., /tenantId + /userId) for finer distribution.
4 · Consistency Levels
| Level | Guarantee | RU Cost | Use Case |
|---|---|---|---|
| Strong | Linearizable reads | 2× writes | Financial ledgers |
| Bounded Staleness | Reads lag by ≤K versions or T time | 2× writes | Leaderboards, inventory counts |
| Session | Read-your-own-writes within session | 1× | User profiles, carts (most apps) |
| Consistent Prefix | No out-of-order reads | 1× | Social feeds, status updates |
| Eventual | No ordering guarantee | 1× | Likes, view counts, telemetry |
5 · RU/s & Throughput Models
A Request Unit (RU) = cost of reading a 1-KB item by ID. Writes ≈ 5× reads; queries vary by complexity.
RU Planning Tips
- Use the Capacity Calculator or SDK's
x-ms-request-chargeheader - Index fewer paths → lower write RU cost
- Point reads (ID + partition key) = cheapest operation (1 RU for 1 KB)
- Cross-partition queries cost proportionally to partitions touched
6 · Multi-Region Writes & Conflict Resolution
- Single-region write — one write region, multiple read replicas; automatic failover
- Multi-region write — all regions accept writes; lower write latency globally
Conflict Resolution Policies
- Last Writer Wins (LWW) — default; uses
_tsor custom path - Custom (stored procedure) — app-defined merge logic
- Async — conflicts queued in a feed for manual resolution
Multi-region writes add ~25% cost (additional RU charge per replica write).
7 · Cost Optimization
- TTL (Time to Live) — auto-expire items; no RU cost for deletion
- Analytical Store — column-oriented store for OLAP; no impact on transactional RUs
- Reserved Capacity — 1-yr or 3-yr reservations; up to 65% savings
- Shared throughput — database-level RU/s shared across containers (400 RU minimum)
- Composite indexes — reduce ORDER BY / multi-filter RU cost
- Exclude unused paths from indexing policy
8 · When NOT Cosmos DB
- ❌ Complex relational joins & multi-table transactions → Azure SQL
- ❌ Full-text search as primary need → Azure AI Search
- ❌ Budget-constrained, single-region, simple key-value → Table Storage / Redis
- ❌ Large analytical queries over TBs → Synapse Analytics
- ❌ Need stored procedures in T-SQL → SQL Database
9 · Real-World: Global E-Commerce Product Catalog
Scenario
An online retailer serves customers in North America, Europe, and Asia-Pacific. The product catalog has 5M items; reads hit 50K RPS at peak; writes are batch-imported nightly.
Design
- API: NoSQL (Core) — flexible product schemas, change feed for search sync
- Partition key:
/categoryId(high cardinality, query-aligned) - Consistency: Session — shoppers see their own recently-viewed items
- Throughput: Autoscale 10K–100K RU/s; spikes during flash sales
- Regions: East US, West Europe, Southeast Asia (single-write, multi-read)
- Cost levers: TTL on session/cart data (24 h); reserved capacity for baseline; analytical store feeds BI dashboards
Result
P99 read latency <8 ms globally; 55% cost reduction vs. provisioned-fixed after reserved capacity + autoscale.
10 · Knowledge Check
Q1: Which consistency level is the default for new Cosmos DB accounts?
Q2: A partition key with only 3 possible values causes which problem?
Q3: Which throughput model is best for a dev/test workload with sporadic usage?
Q4: With multi-region writes enabled, what is the strongest consistency level available?
Q5: What is the RU cost of a point read (1 KB item by ID + partition key)?