cosmosdb-best-practices
from azurecosmosdb/cosmosdb-agent-kit
A collection of skills for AI coding agents working with Azure Cosmos DB. Skills are packaged instructions and scripts that extend agent capabilities.
6 stars2 forksUpdated Jan 25, 2026
npx skills add https://github.com/azurecosmosdb/cosmosdb-agent-kit --skill cosmosdb-best-practicesSKILL.md
Azure Cosmos DB Best Practices
Comprehensive performance optimization guide for Azure Cosmos DB applications, containing 45+ rules across 8 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
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Data Modeling | CRITICAL | model- |
| 2 | Partition Key Design | CRITICAL | partition- |
| 3 | Query Optimization | HIGH | query- |
| 4 | SDK Best Practices | HIGH | sdk- |
| 5 | Indexing Strategies | MEDIUM-HIGH | index- |
| 6 | Throughput & Scaling | MEDIUM | throughput- |
| 7 | Global Distribution | MEDIUM | global- |
| 8 | Monitoring & Diagnostics | LOW-MEDIUM | monitoring- |
Quick Reference
1. Data Modeling (CRITICAL)
model-embed-related- Embed related data retrieved togethermodel-reference-large- Reference data when items get too largemodel-avoid-2mb-limit- Keep items well under 2MB limitmodel-denormalize-reads- Denormalize for read-heavy workloadsmodel-schema-versioning- Version your document schemasmodel-type-discriminator- Use type discriminators for polymorphic data
2. Partition Key Design (CRITICAL)
partition-high-cardinality- Choose high-cardinality partition keyspartition-avoid-hotspots- Distribute writes evenlypartition-hierarchical- Use hierarchical partition keys for flexibilitypartition-query-patterns- Align partition key with query patternspartition-synthetic-keys- Create synthetic keys when neededpartition-20gb-limit- Plan for 20GB logical partition limit
3. Query Optimization (HIGH)
query-avoid-cross-partition- Minimize cross-partition queriesquery-use-projections- Project only needed fieldsquery-pagination- Use continuation tokens for paginationquery-avoid-scans- Avoid full container scansquery-parameterize- Use parameterized queriesquery-order-filters- Order filters by selectivity
4. SDK Best Practices (HIGH)
sdk-singleton-client- Reuse CosmosClient as singletonsdk-async-api- Use async APIs for throughputsdk-retry-429- Handle 429s with retry-aftersdk-connection-mode- Use Direct mode for productionsdk-preferred-regions- Configure preferred regionssdk-diagnostics- Log diagnostics for troubleshooting
5. Indexing Strategies (MEDIUM-HIGH)
index-exclude-unused- Exclude paths never queriedindex-composite- Use composite indexes for ORDER BYindex-spatial- Add spatial indexes for geo queriesindex-range-vs-hash- Choose appropriate index typesindex-lazy-consistent- Understand indexing modes
6. Throughput & Scaling (MEDIUM)
throughput-autoscale- Use autoscale for variable workloadsthroughput-right-size- Right-size provisioned throughputthroughput-serverless- Consider serverless for dev/testthroughput-burst- Understand burst capacitythroughput-container-vs-database- Choose allocation level wisely
7. Global Distribution (MEDIUM)
global-multi-region- Configure multi-region writesglobal-consistency- Choose appropriate consistency levelglobal-conflict-resolution- Implement conflict resolutionglobal-failover- Configure automatic failoverglobal-read-regions- Add read regions near users
8. Monitoring & Diagnostics (LOW-MEDIUM)
monitoring-ru-consumption- Track RU consumptionmonitoring-latency- Monitor P99 latencymonitoring-throttling- Alert on throttlingmonitoring-azure-monitor- Integrate Azure Monitormonitoring-diagnostic-logs- Enable diagnostic logging
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
Repository Stats
Stars6
Forks2
LicenseMIT License