Azure Cosmos DB Fundamentals¶
Overview¶
Azure Cosmos DB is a globally distributed, multi-model NoSQL database service designed for high availability, low latency, and elastic scalability.
Data Models and APIs¶
NoSQL API (Core SQL API)¶
- Native API for Cosmos DB
- SQL-like queries on JSON documents
- Best performance and features
- Recommended for new applications
MongoDB API¶
- MongoDB compatibility (wire protocol compatible)
- Migrate MongoDB apps without code changes
- MongoDB query language support
Cassandra API¶
- Apache Cassandra compatible
- CQL (Cassandra Query Language)
- Wide-column store model
Gremlin API¶
- Graph database model
- Apache TinkerPop compatible
- Gremlin query language
- Best for: Social networks, recommendations
Table API¶
- Key-value store
- Upgrade from Azure Table Storage
- Better performance and global distribution
Partitioning Strategy¶
Partition Key Selection¶
Critical for performance and cost: - High cardinality: Many unique values - Even distribution: Avoid hot partitions - Query patterns: Include in queries when possible
Examples: - Good: UserId, TenantId, DeviceId - Bad: Country (low cardinality), Date (hot partitions)
Hierarchical Partition Keys¶
- Sub-partition data for better distribution
- Example:
/TenantId/UserId - Requires SDK support
Consistency Levels¶
Strong¶
- Linearizability guarantee
- Highest consistency, lowest availability
- Use: Financial transactions, inventory
Bounded Staleness¶
- Reads lag by K versions or T time
- Predictable staleness
- Use: Stock quotes, scoreboards
Session¶
- Read-your-own-writes
- Default consistency level
- Use: Most applications
Consistent Prefix¶
- Reads never see out-of-order writes
- Use: Social media updates
Eventual¶
- Lowest latency, highest availability
- Use: View counts, non-critical data
Request Units (RUs)¶
RU Consumption¶
- 1 RU = Read 1KB item by ID and partition key
- Writes cost more than reads
- Queries cost varies by complexity
Provisioning Models¶
Provisioned Throughput: - Fixed RU/s allocation - Standard or Autoscale - Best for: Predictable workloads
Serverless: - Pay-per-request - No capacity planning - Best for: Intermittent workloads
Global Distribution¶
Multi-Region Writes¶
- Write to any region
- Conflict resolution policies
- Last-Write-Wins (default)
- Custom conflict resolution
Automatic Failover¶
- Transparent to applications
- Configurable priority order
- No downtime
Performance Optimization¶
Indexing¶
- Automatic indexing of all properties
- Customize indexing policy
- Exclude large properties (reduce RU cost)
{
"indexingMode": "consistent",
"automatic": true,
"includedPaths": [{"path": "/*"}],
"excludedPaths": [{"path": "/largeProperty/?"}]
}
Change Feed¶
- Real-time notification of changes
- Enable event-driven architectures
- Process changes in order per partition
- Use cases: Materialized views, real-time analytics
Best Practices¶
Data Modeling¶
- Denormalize for read performance
- Embed related data when possible
- Reference for large or frequently updated data
- Model for queries not normalization
Partition Key¶
- Avoid hot partitions
- Include in queries for efficiency
- Plan for growth
- Use hierarchical keys when needed
Cost Optimization¶
- Right-size RU/s provisioning
- Use TTL for expiring data
- Optimize queries to reduce RU consumption
- Archive old data to cheaper storage
Study Tips¶
Key Concepts¶
- API selection (SQL, MongoDB, Cassandra, etc.)
- Partition key importance
- Consistency levels (5 levels)
- RU consumption and provisioning
- Global distribution capabilities
Common Scenarios¶
- NoSQL document DB β NoSQL API
- Migrate MongoDB β MongoDB API
- Graph relationships β Gremlin API
- Financial system β Strong consistency
- Social media β Eventual consistency
- E-commerce cart β Session consistency
Remember¶
- Partition key selection is critical
- Session consistency is default
- RU/s = throughput capacity
- Global distribution = multi-region writes
- Change Feed = event-driven architecture