Azure Cosmos DB: Partition Keys and Data Modeling

Choosing the right partition key is the most important Cosmos DB decision you’ll make. Get it wrong, and you’ll hit performance issues. Here’s how to think about it.

What is a Partition Key?

Cosmos DB distributes data across physical partitions based on your partition key. All items with the same partition key value live together.

Good Partition Key Characteristics

  • High cardinality: Many distinct values
  • Even distribution: No hot spots
  • Query alignment: Common queries filter on partition key

Examples

Good: /userId for user-centric data
Good: /tenantId for multi-tenant apps
Bad: /country for US-heavy traffic
Bad: /status (only 3-4 values = hot partitions)

Cross-Partition Queries

Queries that don’t filter on partition key cost more (fan-out to all partitions). Design your model to minimize these.

References


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT p.ID FROM cmxg4_posts AS p WHERE (p.post_date < '2019-04-05 10:00:00' OR (p.post_date = '2019-04-05 10:00:00' AND p.ID < 15379)) AND p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_date DESC, p.ID DESC LIMIT 1

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT p.ID FROM cmxg4_posts AS p WHERE (p.post_date > '2019-04-05 10:00:00' OR (p.post_date = '2019-04-05 10:00:00' AND p.ID > 15379)) AND p.post_type = 'post' AND p.post_status = 'publish' ORDER BY p.post_date ASC, p.ID ASC LIMIT 1

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT SQL_CALC_FOUND_ROWS cmxg4_comments.comment_ID FROM cmxg4_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 15379 AND comment_type NOT IN ('note') ORDER BY cmxg4_comments.comment_date_gmt ASC, cmxg4_comments.comment_ID ASC

Leave a comment

Your email address will not be published. Required fields are marked *

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'akismet_comment_nonce' LIMIT 1

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'cookie_consent_template' LIMIT 1

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'jpsq_sync_checkout'

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT COUNT(*) FROM cmxg4_jetpack_sync_queue WHERE queue_id = 'sync'