cosmosdb-best-practices
提供Azure Cosmos DB的性能优化策略和最佳实践,确保数据库高效运行和数据管理。
npx skills add azurecosmosdb/cosmosdb-agent-kit --skill cosmosdb-best-practicesBefore / After 效果对比
1 组在使用Azure Cosmos DB时,若不遵循最佳实践,可能导致性能瓶颈、高延迟和不必要的成本开销,影响应用扩展性。
遵循Azure Cosmos DB性能优化和最佳实践指南,能够显著提升NoSQL数据库的性能、降低成本,并确保系统稳定可靠。
description SKILL.md
name: cosmosdb-best-practices description: | 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.
license: MIT metadata: author: cosmosdb-agent-kit version: "1.0.0"
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
| 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-id-constraints- Follow ID value length and character constraintsmodel-nesting-depth- Stay within 128-level nesting depth limitmodel-numeric-precision- Understand IEEE 754 numeric precision limitsmodel-denormalize-reads- Denormalize for read-heavy workloadsmodel-schema-versioning- Version your document schemasmodel-type-discriminator- Use type discriminators for polymorphic datamodel-json-serialization- Handle JSON serialization correctly for Cosmos DB documentsmodel-relationship-references- Use ID references with transient hydration for document relationships
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-key-length- Respect partition key value length limitspartition-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-excluded-regions- Exclude regions experiencing issuessdk-availability-strategy- Configure availability strategy for resiliencesdk-circuit-breaker- Use circuit breaker for fault tolerancesdk-diagnostics- Log diagnostics for troubleshootingsdk-serialization-enums- Serialize enums as strings not integerssdk-emulator-ssl- Configure SSL and connection mode for Cosmos DB Emulatorsdk-java-content-response- Enable content response on write operations (Java)sdk-java-spring-boot-versions- Match Java version to Spring Boot requirementssdk-local-dev-config- Configure local development to avoid cloud conflictssdk-spring-data-annotations- Annotate entities for Spring Data Cosmossdk-spring-data-repository- Use CosmosRepository correctly and handle Iterable return types
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 usersglobal-zone-redundancy- Enable zone redundancy for HA
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
9. Design Patterns (HIGH)
pattern-change-feed-materialized-views- Use Change Feed for cross-partition query optimizationpattern-efficient-ranking- Use count-based or cached approaches for efficient rankingpattern-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
forum用户评价 (0)
发表评价
暂无评价,来写第一条吧
统计数据
用户评分
为此 Skill 评分