Cosmos DB Partitioning and Throughput Design

Watch the video to deepen your understanding.
SubscribeComplete the full lesson to earn 25 points — 50 with Pro
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks, the wait, and see fewer ads — read each lesson on a single page with Pro
Lesson: Cosmos DB Partitioning and Throughput Design
Introduction: The Architecture of Scale
In the world of distributed databases, "scale" is not just about adding more hardware; it is about how data is organized to allow for horizontal growth. Azure Cosmos DB is a globally distributed, multi-model database service designed to provide single-digit millisecond latency at any scale.
The secret to this performance lies in Partitioning. Unlike relational databases where you might struggle with vertical scaling (buying a bigger server), Cosmos DB uses partitioning to distribute your data across multiple physical partitions. Understanding how to design your partition key is the single most important decision you will make when building on Cosmos DB, as it directly dictates both your performance and your cost (Throughput).
The Mechanics of Partitioning
What is a Partition Key?
A partition key is a property (or set of properties) within your document that Cosmos DB uses to determine which logical partition a document belongs to.
- Logical Partition: A grouping of items that share the same partition key value.
- Physical Partition: The internal infrastructure managed by Azure that stores your logical partitions.
When you perform a read or write operation, the database uses the partition key to route the request directly to the specific physical node holding that data. If your partition key is well-designed, your database can handle millions of requests per second by spreading the load evenly across many physical nodes.
Throughput (Request Units - RUs)
Cosmos DB measures the cost of operations in Request Units (RUs). RUs represent a currency that abstracts CPU, IOPS, and memory.
- Provisioned Throughput: You reserve a specific number of RUs per second for your container.
- The Distribution Problem: If you provision 1,000 RUs/s for a container, those 1,000 RUs are distributed equally across all physical partitions. If one partition holds 90% of your data (a "Hot Partition"), you will hit a 429 "Too Many Requests" error on that partition, even if the rest of your database is idle.
Practical Examples: Choosing a Partition Key
Scenario 1: The "High Cardinality" Approach (Recommended)
Imagine an E-commerce system storing Orders.
- Poor Choice:
State(e.g., "CA", "NY"). There are only 50 states. If 30% of your customers are in California, that partition will be significantly busier than others. - Great Choice:
OrderId. Every order is unique. This ensures that every order goes to a different logical partition, distributing the load perfectly across the entire cluster.
Scenario 2: The "Multi-Tenant" Approach
If you are building a SaaS application, you likely want to isolate data by TenantId.
- Best Practice: Use
TenantIdas your partition key. This allows you to perform queries scoped to a single tenant efficiently, which is the most common pattern in SaaS applications.
Code Snippet: Defining a Container with a Partition Key
When using the Azure Cosmos DB .NET SDK, you define the partition key during container creation:
// Define the partition key path
string partitionKeyPath = "/tenantId";
// Create the container
ContainerResponse response = await database.CreateContainerIfNotExistsAsync(
id: "OrdersContainer",
partitionKeyPath: partitionKeyPath,
throughput: 400 // Provisioned RUs
);
Best Practices
- High Cardinality is King: Choose a partition key with a wide range of values. The more unique values, the better the distribution.
- Avoid Hot Partitions: Monitor your metrics in the Azure Portal. If you see one partition consistently using more RUs than others, your partition key is skewed.
- Query Scoping: Always include the partition key in your query filters whenever possible. A "Cross-Partition Query" (where you don't provide the partition key) must be sent to every physical partition, which is significantly more expensive and slower.
- Synthetic Keys: If you don't have a natural high-cardinality property, create a synthetic one. For example, concatenate
UserId+Timestampto create a unique key ifUserIdalone results in partitions that are too large.
Common Pitfalls
- The "Big Data" Trap: Do not pick a partition key that results in a logical partition growing beyond 20 GB. If a single logical partition exceeds this, you will be unable to add more data to it.
- Over-provisioning: Don't just provision 100,000 RUs because "it's fast." Calculate your expected read/write patterns and use the Cosmos DB Capacity Planner to estimate costs.
- Updating the Partition Key: You cannot change the partition key once a container is created. Choosing the wrong key requires migrating all your data to a new container. Choose carefully at the start.
💡 Pro Tip: The "Right-Sized" Partition
A good rule of thumb is that your partition key should allow for a high volume of operations while keeping the logical partition size under 20GB. Aim for a distribution where your RU load is spread as evenly as possible across the container's physical partitions.
Key Takeaways
- Partitioning is foundational: It is the mechanism that allows Cosmos DB to scale horizontally.
- Choose Cardinality: A high-cardinality partition key (like
OrderIdorDeviceId) is almost always superior to low-cardinality keys (likeStatusorCountry). - Optimize for Queries: While distribution is vital, ensure your partition key aligns with your most frequent query patterns to avoid expensive cross-partition scans.
- Monitor for Skew: Use the Azure Portal to watch for "Hot Partitions" and adjust your strategy if one partition begins to dominate your RU consumption.
- Immutability: Since partition keys cannot be changed post-creation, spend extra time during the design phase modeling your data access patterns.
Reach the last section to complete this lesson and earn points — you're on section 1 of 3.
- Introduction to Azure Monitor
- Azure Monitor Architecture and Data Sources
- Configuring Log Analytics Workspaces
- Designing Log Routing Solutions
- Configuring Diagnostic Settings
- Application Insights for Solution Architects
- Network Watcher and Network Monitoring
- Azure Monitor Alerts and Action Groups
- Workbooks and Custom Dashboards
- Designing a Comprehensive Monitoring Strategy
- Logging and Monitoring Quiz5q
- Microsoft Entra ID for Solution Architects
- Designing Identity Solutions: B2B Collaboration
- Designing Identity Solutions: B2C Scenarios
- Conditional Access Policy Design
- Designing for Multi-Factor Authentication
- Managed Identities for Azure Resources
- Service Principals and App Registrations
- Role-Based Access Control Design
- Privileged Identity Management
- Microsoft Entra ID Protection
- Zero Trust Architecture with Microsoft Entra
- Authentication and Authorization Quiz5q
- Introduction to Azure Governance
- Designing Management Group Hierarchies
- Subscription Strategy Design
- Resource Group Organization Patterns
- Azure Policy Design and Assignment
- Custom Policy Definitions and Initiatives
- Resource Locks and Tagging Strategies
- Azure Blueprints and Landing Zones
- Cost Management and Budget Design
- Cloud Adoption Framework for Governance
- Governance Solutions Quiz5q
- Introduction to Azure Storage
- Storage Account Types and Replication
- Blob Storage Tiers and Lifecycle Management
- Azure Files and Azure NetApp Files
- Azure Managed Disks Design
- Azure Data Lake Storage Gen2
- Cosmos DB Consistency Models
- Cosmos DB Partitioning and Throughput Design
- Cosmos DB API Selection Guide
- Table Storage and Queue Storage Design
- Storage Security and Encryption
- Non-Relational Storage Quiz5q
- Azure SQL Database Service Tiers
- Azure SQL Managed Instance Design
- Azure Database for MySQL and PostgreSQL
- Database Scaling: Vertical and Horizontal
- Read Replicas and Geo-Replication
- Database Security and Auditing Design
- Transparent Data Encryption and Always Encrypted
- Caching with Azure Cache for Redis
- Azure SQL Elastic Pools Design
- Relational Storage Quiz5q
- Azure Data Factory Design Patterns
- Data Integration Pipeline Architecture
- Azure Synapse Analytics Design
- Azure Databricks Integration Patterns
- Azure Stream Analytics for Real-Time Data
- Azure Event Hubs for Data Ingestion
- Data Migration Strategies and Tools
- Azure Purview for Data Governance
- Data Integration Quiz5q
- Introduction to High Availability in Azure
- Availability Zones and Availability Sets
- Azure Load Balancer Design
- Application Gateway and WAF Design
- Azure Front Door and Global Load Balancing
- Azure Traffic Manager Routing Methods
- Multi-Region Architecture Design
- SLA Design and Composite SLAs
- Health Probes and Failover Configuration
- Azure Service Fabric for Stateful HA
- High Availability Quiz5q
- Azure Backup Architecture and Vaults
- Backup Policies for VMs and Databases
- Azure Site Recovery Design
- RTO and RPO Planning Strategies
- Geo-Redundant and Cross-Region Recovery
- Hybrid and On-Premises Backup Solutions
- Resiliency Patterns and Chaos Engineering
- Disaster Recovery Testing and Drills
- Azure Immutable Backup and Soft Delete
- Backup and Disaster Recovery Quiz5q
- Introduction to Azure Compute Options
- Virtual Machine Design and Sizing
- VM Scale Sets and Autoscaling Strategies
- Azure Batch for Large-Scale Workloads
- Azure App Service Plans and Design
- App Service Environments and Isolation
- Azure Container Instances
- Azure Kubernetes Service Architecture
- AKS Networking and Storage Design
- Azure Functions and Serverless Design
- Durable Functions and Orchestration
- Compute Decision Framework
- Azure Virtual Desktop Design
- Compute Solutions Quiz5q
- Microservices Architecture Patterns
- Azure API Management Design
- Azure Service Bus Messaging Design
- Azure Event Grid and Event-Driven Architecture
- Azure Event Hubs for Streaming
- Azure Logic Apps and Integration Workflows
- Azure SignalR and Web PubSub
- Caching Strategies and Azure CDN
- App Configuration and Feature Flags
- Designing for Scalability and Performance
- Azure Container Apps Design
- Application Architecture Quiz5q
- Virtual Network Design and Address Planning
- Subnet Design and Network Segmentation
- Hub-Spoke Network Topology
- Azure Virtual WAN Design
- VPN Gateway Design and Configuration
- ExpressRoute Circuit Design
- Network Security Groups Design
- Azure Firewall and Firewall Manager
- Azure DDoS Protection Design
- Private Endpoints and Private Link
- Azure DNS and DNS Architecture
- Network Performance and Traffic Routing
- Azure Bastion and Secure Access
- Network Solutions Quiz5q
- Azure Migrate Overview and Assessment
- Migration Assessment and Discovery
- Azure Cloud Adoption Framework for Migration
- VM Migration with Azure Migrate
- Database Migration with Azure DMS
- Application Migration to App Service
- Containerizing Applications for Migration
- Migration Cost Planning and Optimization
- Data Box and Offline Migration Methods
- Migrations Quiz5q
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles the points you earn on every lesson and quiz so you progress twice as fast, unlocks half of every practice exam — plus full case studies — with the Learn & Exam study modes, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× points per lesson & quiz
- ✓ 50% of every exam unlocked
- ✓ Learn & Exam modes
- ✓ Distraction-free lessons