cosmosdb-best-practices

Azure Cosmos DB performance optimization and best practices guidelines for NoSQL, partitioning, queries, and SDK usage. Use when writing, reviewing, or refactoring code that interacts with Azure Cosmos DB, designing data models, optimizing queries, or implementing high-performance database operations.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "cosmosdb-best-practices" with this command: npx skills add azurecosmosdb/cosmosdb-agent-kit/azurecosmosdb-cosmosdb-agent-kit-cosmosdb-best-practices

Azure Cosmos DB Best Practices

Comprehensive performance optimization guide for Azure Cosmos DB applications, containing 60+ rules across 9 categories, prioritized by impact to guide automated refactoring and code generation.

When to Apply

Reference these guidelines when:

  • Designing data models for Cosmos DB
  • Choosing partition keys
  • Writing or optimizing queries
  • Implementing SDK patterns
  • Reviewing code for performance issues
  • Configuring throughput and scaling
  • Building globally distributed applications

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Data ModelingCRITICALmodel-
2Partition Key DesignCRITICALpartition-
3Query OptimizationHIGHquery-
4SDK Best PracticesHIGHsdk-
5Indexing StrategiesMEDIUM-HIGHindex-
6Throughput & ScalingMEDIUMthroughput-
7Global DistributionMEDIUMglobal-
8Monitoring & DiagnosticsLOW-MEDIUMmonitoring-

Quick Reference

1. Data Modeling (CRITICAL)

  • model-embed-related - Embed related data retrieved together
  • model-reference-large - Reference data when items get too large
  • model-avoid-2mb-limit - Keep items well under 2MB limit
  • model-id-constraints - Follow ID value length and character constraints
  • model-nesting-depth - Stay within 128-level nesting depth limit
  • model-numeric-precision - Understand IEEE 754 numeric precision limits
  • model-denormalize-reads - Denormalize for read-heavy workloads
  • model-schema-versioning - Version your document schemas
  • model-type-discriminator - Use type discriminators for polymorphic data
  • model-json-serialization - Handle JSON serialization correctly for Cosmos DB documents
  • model-relationship-references - Use ID references with transient hydration for document relationships

2. Partition Key Design (CRITICAL)

  • partition-high-cardinality - Choose high-cardinality partition keys
  • partition-avoid-hotspots - Distribute writes evenly
  • partition-hierarchical - Use hierarchical partition keys for flexibility
  • partition-query-patterns - Align partition key with query patterns
  • partition-synthetic-keys - Create synthetic keys when needed
  • partition-key-length - Respect partition key value length limits
  • partition-20gb-limit - Plan for 20GB logical partition limit

3. Query Optimization (HIGH)

  • query-avoid-cross-partition - Minimize cross-partition queries
  • query-use-projections - Project only needed fields
  • query-pagination - Use continuation tokens for pagination
  • query-avoid-scans - Avoid full container scans
  • query-parameterize - Use parameterized queries
  • query-order-filters - Order filters by selectivity

4. SDK Best Practices (HIGH)

  • sdk-singleton-client - Reuse CosmosClient as singleton
  • sdk-async-api - Use async APIs for throughput
  • sdk-retry-429 - Handle 429s with retry-after
  • sdk-connection-mode - Use Direct mode for production
  • sdk-preferred-regions - Configure preferred regions
  • sdk-excluded-regions - Exclude regions experiencing issues
  • sdk-availability-strategy - Configure availability strategy for resilience
  • sdk-circuit-breaker - Use circuit breaker for fault tolerance
  • sdk-diagnostics - Log diagnostics for troubleshooting
  • sdk-serialization-enums - Serialize enums as strings not integers
  • sdk-emulator-ssl - Configure SSL and connection mode for Cosmos DB Emulator
  • sdk-java-content-response - Enable content response on write operations (Java)
  • sdk-java-spring-boot-versions - Match Java version to Spring Boot requirements
  • sdk-local-dev-config - Configure local development to avoid cloud conflicts
  • sdk-spring-data-annotations - Annotate entities for Spring Data Cosmos
  • sdk-spring-data-repository - Use CosmosRepository correctly and handle Iterable return types

5. Indexing Strategies (MEDIUM-HIGH)

  • index-exclude-unused - Exclude paths never queried
  • index-composite - Use composite indexes for ORDER BY
  • index-spatial - Add spatial indexes for geo queries
  • index-range-vs-hash - Choose appropriate index types
  • index-lazy-consistent - Understand indexing modes

6. Throughput & Scaling (MEDIUM)

  • throughput-autoscale - Use autoscale for variable workloads
  • throughput-right-size - Right-size provisioned throughput
  • throughput-serverless - Consider serverless for dev/test
  • throughput-burst - Understand burst capacity
  • throughput-container-vs-database - Choose allocation level wisely

7. Global Distribution (MEDIUM)

  • global-multi-region - Configure multi-region writes
  • global-consistency - Choose appropriate consistency level
  • global-conflict-resolution - Implement conflict resolution
  • global-failover - Configure automatic failover
  • global-read-regions - Add read regions near users
  • global-zone-redundancy - Enable zone redundancy for HA

8. Monitoring & Diagnostics (LOW-MEDIUM)

  • monitoring-ru-consumption - Track RU consumption
  • monitoring-latency - Monitor P99 latency
  • monitoring-throttling - Alert on throttling
  • monitoring-azure-monitor - Integrate Azure Monitor
  • monitoring-diagnostic-logs - Enable diagnostic logging

9. Design Patterns (HIGH)

  • pattern-change-feed-materialized-views - Use Change Feed for cross-partition query optimization
  • pattern-efficient-ranking - Use count-based or cached approaches for efficient ranking
  • pattern-service-layer-relationships - Use a service layer to hydrate document references

How to Use

Read individual rule files for detailed explanations and code examples:

rules/model-embed-related.md
rules/partition-high-cardinality.md
rules/_sections.md

Each rule file contains:

  • Brief explanation of why it matters
  • Incorrect code example with explanation
  • Correct code example with explanation
  • Additional context and references

Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

openclaw-version-monitor

监控 OpenClaw GitHub 版本更新,获取最新版本发布说明,翻译成中文, 并推送到 Telegram 和 Feishu。用于:(1) 定时检查版本更新 (2) 推送版本更新通知 (3) 生成中文版发布说明

Archived SourceRecently Updated
Coding

ask-claude

Delegate a task to Claude Code CLI and immediately report the result back in chat. Supports persistent sessions with full context memory. Safe execution: no data exfiltration, no external calls, file operations confined to workspace. Use when the user asks to run Claude, delegate a coding task, continue a previous Claude session, or any task benefiting from Claude Code's tools (file editing, code analysis, bash, etc.).

Archived SourceRecently Updated
Coding

ai-dating

This skill enables dating and matchmaking workflows. Use it when a user asks to make friends, find a partner, run matchmaking, or provide dating preferences/profile updates. The skill should execute `dating-cli` commands to complete profile setup, task creation/update, match checking, contact reveal, and review.

Archived SourceRecently Updated
Coding

clawhub-rate-limited-publisher

Queue and publish local skills to ClawHub with a strict 5-per-hour cap using the local clawhub CLI and host scheduler.

Archived SourceRecently Updated