cosmosdb-best-practices
Azure Cosmos DB performance optimization and best practices guidelines for NoSQL, partitioning, queries, SDK usage, and vector search. 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
- 最近更新
- 2026-11-14
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- cosmosdb-best-practices
- license
- MIT
- metadata
- { "author": "cosmosdb-agent-kit", "version": "1.0.0" }
- description
- Azure Cosmos DB performance optimization and best practices guidelines for NoSQL, partitioning, queries, SDK usage, and vector search. Use when writing, reviewing, or refactoring code that interacts with Azure Cosmos DB, designing data models, optimizing queries, or implementing high-performance database operations.
02 资源文件 75
- 参考文档rules/_sections.md2.1 KB
- 参考文档rules/_template.md802 B
- 参考文档rules/global-conflict-resolution.md4.0 KB
- 参考文档rules/global-consistency.md3.7 KB
- 参考文档rules/global-failover.md3.7 KB
- 参考文档rules/global-multi-region.md3.2 KB
- 参考文档rules/global-read-regions.md3.9 KB
- 参考文档rules/global-zone-redundancy.md3.5 KB
- 参考文档rules/index-composite.md6.0 KB
- 参考文档rules/index-exclude-unused.md3.1 KB
- 参考文档rules/index-lazy-consistent.md3.1 KB
- 参考文档rules/index-range-vs-hash.md3.4 KB
- 参考文档rules/index-spatial.md3.1 KB
- 参考文档rules/model-avoid-2mb-limit.md2.0 KB
- 参考文档rules/model-denormalize-reads.md3.4 KB
- 参考文档rules/model-embed-related.md2.5 KB
- 参考文档rules/model-id-constraints.md2.9 KB
- 参考文档rules/model-json-serialization.md4.0 KB
- 参考文档rules/model-nesting-depth.md3.3 KB
- 参考文档rules/model-numeric-precision.md3.2 KB
- 参考文档rules/model-reference-large.md2.3 KB
- 参考文档rules/model-relationship-references.md3.6 KB
- 参考文档rules/model-schema-versioning.md2.4 KB
- 参考文档rules/model-type-discriminator.md3.5 KB
- 参考文档rules/monitoring-azure-monitor.md4.4 KB
- 参考文档rules/monitoring-diagnostic-logs.md5.1 KB
- 参考文档rules/monitoring-latency.md4.2 KB
- 参考文档rules/monitoring-ru-consumption.md4.2 KB
- 参考文档rules/monitoring-throttling.md4.4 KB
- 参考文档rules/partition-20gb-limit.md2.8 KB
- 参考文档rules/partition-avoid-hotspots.md3.4 KB
- 参考文档rules/partition-hierarchical.md2.7 KB
- 参考文档rules/partition-high-cardinality.md2.2 KB
- 参考文档rules/partition-key-length.md2.8 KB
- 参考文档rules/partition-query-patterns.md2.7 KB
- 参考文档rules/partition-synthetic-keys.md3.8 KB
- 参考文档rules/pattern-change-feed-materialized-views.md7.2 KB
- 参考文档rules/pattern-efficient-ranking.md5.8 KB
- 参考文档rules/pattern-service-layer-relationships.md4.8 KB
- 参考文档rules/query-avoid-cross-partition.md2.5 KB
- 参考文档rules/query-avoid-scans.md2.5 KB
- 参考文档rules/query-order-filters.md2.6 KB
- 参考文档rules/query-pagination.md6.2 KB
- 参考文档rules/query-parameterize.md2.9 KB
- 参考文档rules/query-use-projections.md2.7 KB
- 参考文档rules/sdk-async-api.md3.2 KB
- 参考文档rules/sdk-availability-strategy.md5.3 KB
- 参考文档rules/sdk-circuit-breaker.md8.4 KB
- 参考文档rules/sdk-connection-mode.md2.9 KB
- 参考文档rules/sdk-diagnostics.md3.6 KB
- 参考文档rules/sdk-emulator-ssl.md7.8 KB
- 参考文档rules/sdk-etag-concurrency.md8.1 KB
- 参考文档rules/sdk-excluded-regions.md4.9 KB
- 参考文档rules/sdk-java-content-response.md3.9 KB
- 参考文档rules/sdk-java-cosmos-config.md5.9 KB
- 参考文档rules/sdk-java-spring-boot-versions.md4.8 KB
- 参考文档rules/sdk-local-dev-config.md5.4 KB
- 参考文档rules/sdk-newtonsoft-dependency.md3.9 KB
- 参考文档rules/sdk-preferred-regions.md3.2 KB
- 参考文档rules/sdk-retry-429.md3.7 KB
- 参考文档rules/sdk-serialization-enums.md2.8 KB
- 参考文档rules/sdk-singleton-client.md3.7 KB
- 参考文档rules/sdk-spring-data-annotations.md3.4 KB
- 参考文档rules/sdk-spring-data-repository.md3.4 KB
- 参考文档rules/throughput-autoscale.md2.6 KB
- 参考文档rules/throughput-burst.md2.7 KB
- 参考文档rules/throughput-container-vs-database.md3.2 KB
- 参考文档rules/throughput-right-size.md3.1 KB
- 参考文档rules/throughput-serverless.md2.7 KB
- 参考文档rules/vector-distance-query.md4.8 KB
- 参考文档rules/vector-embedding-policy.md3.8 KB
- 参考文档rules/vector-enable-feature.md3.0 KB
- 参考文档rules/vector-index-type.md4.7 KB
- 参考文档rules/vector-normalize-embeddings.md8.6 KB
- 参考文档rules/vector-repository-pattern.md12.9 KB
03 SKILL.md 正文
Azure Cosmos DB Best Practices
Comprehensive performance optimization guide for Azure Cosmos DB applications, containing 73+ rules across 10 categories, prioritized by impact to guide automated refactoring and code generation.
73 individual rule files are in the rules/ directory, one file per rule, synced from the AzureCosmosDB/cosmosdb-agent-kit. Load only the relevant rule file(s) when answering a question — do NOT load all files at once.
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
- Implementing vector search and RAG patterns
Rule Index
Rules are grouped by category prefix. For a given question, load files matching
the relevant prefix (e.g., model-*.md for data modeling, sdk-*.md for SDK usage).
See rules/_sections.md for category descriptions.
1. Data Modeling — CRITICAL (prefix: model-)
- model-embed-related.md — Embed related data retrieved together
- model-reference-large.md — Reference data when items grow large
- model-avoid-2mb-limit.md — Keep items well under 2MB limit
- model-id-constraints.md — Follow ID value length and character constraints
- model-nesting-depth.md — Stay within 128-level nesting depth limit
- model-numeric-precision.md — Understand IEEE 754 numeric precision limits
- model-denormalize-reads.md — Denormalize for read-heavy workloads
- model-schema-versioning.md — Version your document schemas
- model-type-discriminator.md — Use type discriminators for polymorphic data
- model-json-serialization.md — Handle JSON serialization correctly
- model-relationship-references.md — Use ID references with transient hydration
2. Partition Key Design — CRITICAL (prefix: partition-)
- partition-high-cardinality.md — Choose high-cardinality partition keys
- partition-avoid-hotspots.md — Distribute writes evenly
- partition-hierarchical.md — Use hierarchical partition keys for flexibility
- partition-query-patterns.md — Align partition key with query patterns
- partition-synthetic-keys.md — Create synthetic keys when needed
- partition-key-length.md — Respect partition key value length limits
- partition-20gb-limit.md — Plan for 20GB logical partition limit
3. Query Optimization — HIGH (prefix: query-)
- query-avoid-cross-partition.md — Minimize cross-partition queries
- query-use-projections.md — Project only needed fields
- query-pagination.md — Use continuation tokens for pagination
- query-avoid-scans.md — Avoid full container scans
- query-parameterize.md — Use parameterized queries
- query-order-filters.md — Order filters by selectivity
4. SDK Best Practices — HIGH (prefix: sdk-)
- sdk-singleton-client.md — Reuse CosmosClient as singleton
- sdk-async-api.md — Use async APIs for throughput
- sdk-retry-429.md — Handle 429s with retry-after
- sdk-connection-mode.md — Use Direct mode for production
- sdk-preferred-regions.md — Configure preferred regions
- sdk-excluded-regions.md — Exclude regions experiencing issues
- sdk-availability-strategy.md — Configure availability strategy for resilience
- sdk-circuit-breaker.md — Use circuit breaker for fault tolerance
- sdk-diagnostics.md — Log diagnostics for troubleshooting
- sdk-serialization-enums.md — Serialize enums as strings not integers
- sdk-emulator-ssl.md — Configure SSL and connection mode for Cosmos DB Emulator
- sdk-etag-concurrency.md — Use ETags for optimistic concurrency
- sdk-java-content-response.md — Enable content response on write operations (Java)
- sdk-java-cosmos-config.md — Configure Cosmos DB in Spring Boot with dependent beans
- sdk-java-spring-boot-versions.md — Match Java version to Spring Boot requirements
- sdk-local-dev-config.md — Configure local development to avoid cloud conflicts
- sdk-newtonsoft-dependency.md — Explicitly reference Newtonsoft.Json package (.NET)
- sdk-spring-data-annotations.md — Annotate entities for Spring Data Cosmos
- sdk-spring-data-repository.md — Use CosmosRepository correctly
5. Indexing Strategies — MEDIUM-HIGH (prefix: index-)
- index-exclude-unused.md — Exclude paths never queried
- index-composite.md — Use composite indexes for ORDER BY
- index-spatial.md — Add spatial indexes for geo queries
- index-range-vs-hash.md — Choose appropriate index types
- index-lazy-consistent.md — Understand indexing modes
6. Throughput & Scaling — MEDIUM (prefix: throughput-)
- throughput-autoscale.md — Use autoscale for variable workloads
- throughput-right-size.md — Right-size provisioned throughput
- throughput-serverless.md — Consider serverless for dev/test
- throughput-burst.md — Understand burst capacity
- throughput-container-vs-database.md — Choose allocation level wisely
7. Global Distribution — MEDIUM (prefix: global-)
- global-multi-region.md — Configure multi-region writes
- global-consistency.md — Choose appropriate consistency level
- global-conflict-resolution.md — Implement conflict resolution
- global-failover.md — Configure automatic failover
- global-read-regions.md — Add read regions near users
- global-zone-redundancy.md — Enable zone redundancy for HA
8. Monitoring & Diagnostics — LOW-MEDIUM (prefix: monitoring-)
- monitoring-ru-consumption.md — Track RU consumption
- monitoring-latency.md — Monitor P99 latency
- monitoring-throttling.md — Alert on throttling
- monitoring-azure-monitor.md — Integrate Azure Monitor
- monitoring-diagnostic-logs.md — Enable diagnostic logging
9. Design Patterns — HIGH (prefix: pattern-)
- pattern-change-feed-materialized-views.md — Use Change Feed for materialized views
- pattern-efficient-ranking.md — Use count-based or cached approaches for ranking
- pattern-service-layer-relationships.md — Use a service layer to hydrate document references
10. Vector Search — HIGH (prefix: vector-)
- vector-enable-feature.md — Enable vector search feature on account
- vector-embedding-policy.md — Define vector embedding policy on container
- vector-index-type.md — Configure vector indexes (QuantizedFlat or DiskANN)
- vector-distance-query.md — Use VectorDistance() for similarity search
- vector-normalize-embeddings.md — Normalize embeddings for cosine similarity
- vector-repository-pattern.md — Implement repository pattern for vector search
How to Use
Each rule is a separate file under rules/. When answering a Cosmos DB question, read only the relevant rule file(s) based on the prefix matching the topic:
- Data modeling question → read
rules/model-*.mdfiles - Partition key question → read
rules/partition-*.mdfiles - SDK/client question → read
rules/sdk-*.mdfiles - etc.
Each rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
Source: AzureCosmosDB/cosmosdb-agent-kit
04 同仓库更多 Skills
web-search
Implement web search capabilities using the z-ai-web-dev-sdk. Use this skill when the user needs to search for real-time information from the web, retrieve up-to-date content beyond the knowledge cutoff, or find the latest news and data. Returns structured search results with URLs, snippets, and metadata.
web-reader
Implement web page content extraction capabilities using the z-ai-web-dev-sdk. Use this skill when the user needs to scrape web pages, extract article content, retrieve page metadata, or build applications that process web content. Supports automatic content extraction with title, HTML, and publication time retrieval.
video-understand
Implement specialized video understanding capabilities using the z-ai-web-dev-sdk. Use this skill when the user needs to analyze video content, understand motion and temporal sequences, extract information from video frames, describe video scenes, or perform video-based AI analysis. Optimized for MP4, AVI, MOV, and other common video formats.
Video Generation
Implement AI-powered video generation capabilities using the z-ai-web-dev-sdk. Use this skill when the user needs to generate videos from text prompts or images, create video content programmatically, or build applications that produce video outputs. Supports asynchronous task management with status polling and result retrieval.
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.
seo-content-writer
Use when the user asks to "write SEO content", "create a blog post", "write an article", "content writing", "draft optimized content", "write me an article", "create a blog post about", "help me write SEO content", or "draft content for". Creates high-quality, SEO-optimized content that ranks in search engines. Applies on-page SEO best practices, keyword optimization, and content structure for maximum visibility and engagement. For AI citation optimization, see geo-content-optimizer. For updating existing content, see content-refresher.