cosmosdb-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAzure Cosmos DB Best Practices
Azure Cosmos DB 最佳实践
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.
这是一份针对Azure Cosmos DB应用的全面性能优化指南,包含8个类别下的45+条规则,按影响优先级排序,可指导自动化重构和代码生成。
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
在以下场景中可参考本指南:
- 为Cosmos DB设计数据模型时
- 选择分区键时
- 编写或优化查询时
- 实现SDK模式时
- 审查代码以排查性能问题时
- 配置吞吐量和扩缩容时
- 构建全球分布式应用时
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Data Modeling | CRITICAL | |
| 2 | Partition Key Design | CRITICAL | |
| 3 | Query Optimization | HIGH | |
| 4 | SDK Best Practices | HIGH | |
| 5 | Indexing Strategies | MEDIUM-HIGH | |
| 6 | Throughput & Scaling | MEDIUM | |
| 7 | Global Distribution | MEDIUM | |
| 8 | Monitoring & Diagnostics | LOW-MEDIUM | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 数据建模 | 关键 | |
| 2 | 分区键设计 | 关键 | |
| 3 | 查询优化 | 高 | |
| 4 | SDK最佳实践 | 高 | |
| 5 | 索引策略 | 中高 | |
| 6 | 吞吐量与扩缩容 | 中 | |
| 7 | 全球分布式部署 | 中 | |
| 8 | 监控与诊断 | 中低 | |
Quick Reference
快速参考
1. Data Modeling (CRITICAL)
1. 数据建模(关键)
- - Embed related data retrieved together
model-embed-related - - Reference data when items get too large
model-reference-large - - Keep items well under 2MB limit
model-avoid-2mb-limit - - Denormalize for read-heavy workloads
model-denormalize-reads - - Version your document schemas
model-schema-versioning - - Use type discriminators for polymorphic data
model-type-discriminator
- - 嵌入需一起检索的关联数据
model-embed-related - - 当数据项过大时使用引用方式
model-reference-large - - 确保数据项远小于2MB限制
model-avoid-2mb-limit - - 针对读密集型工作负载采用反范式设计
model-denormalize-reads - - 为文档模式添加版本控制
model-schema-versioning - - 对多态数据使用类型鉴别器
model-type-discriminator
2. Partition Key Design (CRITICAL)
2. 分区键设计(关键)
- - Choose high-cardinality partition keys
partition-high-cardinality - - Distribute writes evenly
partition-avoid-hotspots - - Use hierarchical partition keys for flexibility
partition-hierarchical - - Align partition key with query patterns
partition-query-patterns - - Create synthetic keys when needed
partition-synthetic-keys - - Plan for 20GB logical partition limit
partition-20gb-limit
- - 选择高基数分区键
partition-high-cardinality - - 均匀分配写入操作
partition-avoid-hotspots - - 使用分层分区键提升灵活性
partition-hierarchical - - 使分区键与查询模式对齐
partition-query-patterns - - 必要时创建合成键
partition-synthetic-keys - - 为20GB逻辑分区限制提前规划
partition-20gb-limit
3. Query Optimization (HIGH)
3. 查询优化(高)
- - Minimize cross-partition queries
query-avoid-cross-partition - - Project only needed fields
query-use-projections - - Use continuation tokens for pagination
query-pagination - - Avoid full container scans
query-avoid-scans - - Use parameterized queries
query-parameterize - - Order filters by selectivity
query-order-filters
- - 尽量减少跨分区查询
query-avoid-cross-partition - - 仅投影所需字段
query-use-projections - - 使用延续令牌实现分页
query-pagination - - 避免全容器扫描
query-avoid-scans - - 使用参数化查询
query-parameterize - - 按选择性对筛选器排序
query-order-filters
4. SDK Best Practices (HIGH)
4. SDK最佳实践(高)
- - Reuse CosmosClient as singleton
sdk-singleton-client - - Use async APIs for throughput
sdk-async-api - - Handle 429s with retry-after
sdk-retry-429 - - Use Direct mode for production
sdk-connection-mode - - Configure preferred regions
sdk-preferred-regions - - Exclude regions experiencing issues
sdk-excluded-regions - - Configure availability strategy for resilience
sdk-availability-strategy - - Use circuit breaker for fault tolerance
sdk-circuit-breaker - - Log diagnostics for troubleshooting
sdk-diagnostics - - Serialize enums as strings not integers
sdk-serialization-enums - - Configure SSL and connection mode for Cosmos DB Emulator
sdk-emulator-ssl - - Enable content response on write operations (Java)
sdk-java-content-response - - Match Java version to Spring Boot requirements
sdk-java-spring-boot-versions - - Configure local development to avoid cloud conflicts
sdk-local-dev-config
- - 将CosmosClient作为单例复用
sdk-singleton-client - - 使用异步API提升吞吐量
sdk-async-api - - 结合retry-after处理429错误
sdk-retry-429 - - 生产环境使用Direct模式
sdk-connection-mode - - 配置首选区域
sdk-preferred-regions - - 排除出现问题的区域
sdk-excluded-regions - - 配置可用性策略以提升韧性
sdk-availability-strategy - - 使用断路器实现容错
sdk-circuit-breaker - - 记录诊断信息用于故障排查
sdk-diagnostics - - 将枚举序列化为字符串而非整数
sdk-serialization-enums - - 为Cosmos DB模拟器配置SSL和连接模式
sdk-emulator-ssl - - 在写入操作上启用内容响应(Java)
sdk-java-content-response - - 使Java版本与Spring Boot要求匹配
sdk-java-spring-boot-versions - - 配置本地开发环境以避免云服务冲突
sdk-local-dev-config
5. Indexing Strategies (MEDIUM-HIGH)
5. 索引策略(中高)
- - Exclude paths never queried
index-exclude-unused - - Use composite indexes for ORDER BY
index-composite - - Add spatial indexes for geo queries
index-spatial - - Choose appropriate index types
index-range-vs-hash - - Understand indexing modes
index-lazy-consistent
- - 排除从未被查询的路径
index-exclude-unused - - 为ORDER BY操作使用复合索引
index-composite - - 为地理查询添加空间索引
index-spatial - - 选择合适的索引类型
index-range-vs-hash - - 了解索引模式
index-lazy-consistent
6. Throughput & Scaling (MEDIUM)
6. 吞吐量与扩缩容(中)
- - Use autoscale for variable workloads
throughput-autoscale - - Right-size provisioned throughput
throughput-right-size - - Consider serverless for dev/test
throughput-serverless - - Understand burst capacity
throughput-burst - - Choose allocation level wisely
throughput-container-vs-database
- - 针对可变工作负载使用自动扩缩容
throughput-autoscale - - 合理配置预配吞吐量
throughput-right-size - - 开发/测试环境可考虑无服务器模式
throughput-serverless - - 了解突发容量机制
throughput-burst - - 谨慎选择吞吐量分配级别
throughput-container-vs-database
7. Global Distribution (MEDIUM)
7. 全球分布式部署(中)
- - Configure multi-region writes
global-multi-region - - Choose appropriate consistency level
global-consistency - - Implement conflict resolution
global-conflict-resolution - - Configure automatic failover
global-failover - - Add read regions near users
global-read-regions - - Enable zone redundancy for HA
global-zone-redundancy
- - 配置多区域写入
global-multi-region - - 选择合适的一致性级别
global-consistency - - 实现冲突解决机制
global-conflict-resolution - - 配置自动故障转移
global-failover - - 在用户附近添加读取区域
global-read-regions - - 启用区域冗余以实现高可用
global-zone-redundancy
8. Monitoring & Diagnostics (LOW-MEDIUM)
8. 监控与诊断(中低)
- - Track RU consumption
monitoring-ru-consumption - - Monitor P99 latency
monitoring-latency - - Alert on throttling
monitoring-throttling - - Integrate Azure Monitor
monitoring-azure-monitor - - Enable diagnostic logging
monitoring-diagnostic-logs
- - 跟踪RU消耗
monitoring-ru-consumption - - 监控P99延迟
monitoring-latency - - 针对限流设置告警
monitoring-throttling - - 集成Azure Monitor
monitoring-azure-monitor - - 启用诊断日志
monitoring-diagnostic-logs
9. Design Patterns (HIGH)
9. 设计模式(高)
- - Use Change Feed for cross-partition query optimization
pattern-change-feed-materialized-views
- - 使用Change Feed优化跨分区查询
pattern-change-feed-materialized-views
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.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
阅读单个规则文件获取详细说明和代码示例:
rules/model-embed-related.md
rules/partition-high-cardinality.md
rules/_sections.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 额外背景信息和参考资料
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有展开规则的完整指南:
AGENTS.md