适用于 Azure Cosmos DB 异步 Java SDK v2 的性能提示Performance tips for Azure Cosmos DB Async Java SDK v2

适用于: SQL API

重要

这不是最新的 Azure Cosmos DB Java SDK!This is not the latest Java SDK for Azure Cosmos DB! 应将项目升级到 Azure Cosmos DB Java SDK v4,然后阅读 Azure Cosmos DB Java SDK v4 性能提示指南You should upgrade your project to Azure Cosmos DB Java SDK v4 and then read the Azure Cosmos DB Java SDK v4 performance tips guide. 请按照迁移到 Azure Cosmos DB Java SDK v4 指南和 Reactor 与 RxJava 指南中的说明进行升级。Follow the instructions in the Migrate to Azure Cosmos DB Java SDK v4 guide and Reactor vs RxJava guide to upgrade.

本文中的性能提示仅适用于 Azure Cosmos DB Async Java SDK v2。The performance tips in this article are for Azure Cosmos DB Async Java SDK v2 only. 请查看 Azure Cosmos DB Async Java SDK v2 发行说明Maven 存储库、Azure Cosmos DB Async Java SDK v2 故障排除指南了解详细信息。See the Azure Cosmos DB Async Java SDK v2 Release notes, Maven repository, and Azure Cosmos DB Async Java SDK v2 troubleshooting guide for more information.

Azure Cosmos DB 是一个快速、弹性的分布式数据库,可以在提供延迟与吞吐量保证的情况下无缝缩放。Azure Cosmos DB is a fast and flexible distributed database that scales seamlessly with guaranteed latency and throughput. 凭借 Azure Cosmos DB,无需对体系结构进行重大更改或编写复杂的代码即可缩放数据库。You do not have to make major architecture changes or write complex code to scale your database with Azure Cosmos DB. 扩展和缩减操作就像执行单个 API 调用或 SDK 方法调用一样简单。Scaling up and down is as easy as making a single API call or SDK method call. 但是,由于 Azure Cosmos DB 是通过网络调用访问的,因此,使用 Azure Cosmos DB Async Java SDK v2 时,可以通过客户端优化来获得最高性能。However, because Azure Cosmos DB is accessed via network calls there are client-side optimizations you can make to achieve peak performance when using the Azure Cosmos DB Async Java SDK v2.

如果有“如何改善数据库性能?”的疑问,So if you're asking "How can I improve my database performance?" 请考虑以下选项:consider the following options:

网络Networking

  • 连接模式:使用直接模式 Connection mode: Use Direct mode

    客户端连接到 Azure Cosmos DB 的方式对性能有重大影响(尤其在客户端延迟方面)。How a client connects to Azure Cosmos DB has important implications on performance, especially in terms of client-side latency. ConnectionMode 是可用于配置客户端 ConnectionPolicy 的关键配置设置 。The ConnectionMode is a key configuration setting available for configuring the client ConnectionPolicy. 对于 Azure Cosmos DB Async Java SDK v2,有两种可用的 ConnectionMode:For Azure Cosmos DB Async Java SDK v2, the two available ConnectionModes are:

    网关模式受所有 SDK 平台支持,默认情况下,它是已配置的选项。Gateway mode is supported on all SDK platforms and it is the configured option by default. 如果应用程序在有严格防火墙限制的企业网络中运行,则网关模式是最佳选择,因为它使用标准 HTTPS 端口与单个终结点。If your applications run within a corporate network with strict firewall restrictions, Gateway mode is the best choice since it uses the standard HTTPS port and a single endpoint. 但是,对于性能的影响是每次在 Azure Cosmos DB 中读取或写入数据时,网关模式都涉及到额外的网络跃点。The performance tradeoff, however, is that Gateway mode involves an additional network hop every time data is read or written to Azure Cosmos DB. 因此,由于涉及的网络跃点较少,直接模式会提供更好的性能。Because of this, Direct mode offers better performance due to fewer network hops.

    ConnectionMode 是在构造 DocumentClient 实例期间使用 ConnectionPolicy 参数配置的 。The ConnectionMode is configured during the construction of the DocumentClient instance with the ConnectionPolicy parameter.

Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)

    public ConnectionPolicy getConnectionPolicy() {
        ConnectionPolicy policy = new ConnectionPolicy();
        policy.setConnectionMode(ConnectionMode.Direct);
        policy.setMaxPoolSize(1000);
        return policy;
    }

    ConnectionPolicy connectionPolicy = new ConnectionPolicy();
    DocumentClient client = new DocumentClient(HOST, MASTER_KEY, connectionPolicy, null);
  • 将客户端并置在同一 Azure 区域内以提高性能 Collocate clients in same Azure region for performance

    如果可能,请将任何调用 Azure Cosmos DB 的应用程序放在与 Azure Cosmos 数据库所在的相同区域中。When possible, place any applications calling Azure Cosmos DB in the same region as the Azure Cosmos database. 根据请求采用的路由,各项请求从客户端传递到 Azure 数据中心边界时的此类延迟可能有所不同。This latency can likely vary from request to request depending on the route taken by the request as it passes from the client to the Azure datacenter boundary. 通过确保在与预配 Azure Cosmos DB 终结点所在的同一 Azure 区域中调用应用程序,可能会实现最低的延迟。The lowest possible latency is achieved by ensuring the calling application is located within the same Azure region as the provisioned Azure Cosmos DB endpoint. 有关可用区域的列表,请参阅 Azure Regions(Azure 区域)。For a list of available regions, see Azure Regions.

    Azure Cosmos DB 连接策略演示

SDK 用法SDK Usage

  • 安装最新的 SDKInstall the most recent SDK

    Azure Cosmos DB SDK 正在不断改进以提供最佳性能。The Azure Cosmos DB SDKs are constantly being improved to provide the best performance. 请参阅 Azure Cosmos DB Async Java SDK v2 发行说明页以了解最新的 SDK 并查看改进内容。See the Azure Cosmos DB Async Java SDK v2 Release Notes pages to determine the most recent SDK and review improvements.

  • 在应用程序生存期内使用单一实例 Azure Cosmos DB 客户端Use a singleton Azure Cosmos DB client for the lifetime of your application

    每个 AsyncDocumentClient 实例都是线程安全的,可执行高效的连接管理和地址缓存。Each AsyncDocumentClient instance is thread-safe and performs efficient connection management and address caching. 若要通过 AsyncDocumentClient 获得高效的连接管理和更好的性能,建议在应用程序生存期内对每个 AppDomain 使用单个 AsyncDocumentClient 实例。To allow efficient connection management and better performance by AsyncDocumentClient, it is recommended to use a single instance of AsyncDocumentClient per AppDomain for the lifetime of the application.

  • 优化 ConnectionPolicyTuning ConnectionPolicy

    默认情况下,使用 Azure Cosmos DB Async Java SDK v2 时,直接模式 Cosmos DB 请求通过 TCP 发出。By default, Direct mode Cosmos DB requests are made over TCP when using the Azure Cosmos DB Async Java SDK v2. 在内部,SDK 使用特殊的直接模式体系结构来动态管理网络资源并获得最佳性能。Internally the SDK uses a special Direct mode architecture to dynamically manage network resources and get the best performance.

    在 Azure Cosmos DB Async Java SDK v2 中,直接模式是在大多数工作负载下提高数据库性能的最佳选择。In the Azure Cosmos DB Async Java SDK v2, Direct mode is the best choice to improve database performance with most workloads.

    • *直接模式概述 _*Overview of Direct mode _

      直接模式体系结构插图

      在直接模式下采用的客户端体系结构使得网络利用率可预测,并实现对 Azure Cosmos DB 副本的多路访问。The client-side architecture employed in Direct mode enables predictable network utilization and multiplexed access to Azure Cosmos DB replicas. 上图显示了直接模式如何将客户端请求路由到 Cosmos DB 后端中的副本。The diagram above shows how Direct mode routes client requests to replicas in the Cosmos DB backend. 直接模式体系结构在客户端为每个数据库副本最多分配 10 个通道。The Direct mode architecture allocates up to 10 _ Channels* on the client side per DB replica. 一个通道是前面带有请求缓冲区(深度为 30 个请求)的 TCP 连接。A Channel is a TCP connection preceded by a request buffer, which is 30 requests deep. 属于某个副本的通道由该副本的服务终结点按需动态分配。The Channels belonging to a replica are dynamically allocated as needed by the replica's Service Endpoint. 当用户在直接模式下发出请求时,TransportClient 会根据分区键将请求路由到适当的服务终结点。When the user issues a request in Direct mode, the TransportClient routes the request to the proper service endpoint based on the partition key. 请求队列在服务终结点之前缓冲请求。The Request Queue buffers requests before the Service Endpoint.

    • *直接模式的 ConnectionPolicy 配置选项 _*ConnectionPolicy Configuration options for Direct mode _

      第一步是使用下面推荐的配置设置。As a first step, use the following recommended configuration settings below. 如果遇到有关此特定主题方面的问题,请与 Azure Cosmos DB 团队联系。Please contact the Azure Cosmos DB team if you run into issues on this particular topic.

      如果使用 Azure Cosmos DB 作为参考数据库(即,该数据库用于多个点读取操作和少量的写入操作),可以接受将 idleEndpointTimeout 设置为 0(即无超时)。If you are using Azure Cosmos DB as a reference database (that is, the database is used for many point read operations and few write operations), it may be acceptable to set _idleEndpointTimeout* to 0 (that is, no timeout).

      配置选项Configuration option 默认Default
      bufferPageSizebufferPageSize 81928192
      connectionTimeoutconnectionTimeout “PT1M”"PT1M"
      idleChannelTimeoutidleChannelTimeout "PT0S""PT0S"
      idleEndpointTimeoutidleEndpointTimeout "PT1M10S""PT1M10S"
      maxBufferCapacitymaxBufferCapacity 83886088388608
      maxChannelsPerEndpointmaxChannelsPerEndpoint 10 个10
      maxRequestsPerChannelmaxRequestsPerChannel 3030
      receiveHangDetectionTimereceiveHangDetectionTime "PT1M5S""PT1M5S"
      requestExpiryIntervalrequestExpiryInterval "PT5S""PT5S"
      requestTimeoutrequestTimeout “PT1M”"PT1M"
      requestTimerResolutionrequestTimerResolution "PT0.5S""PT0.5S"
      sendHangDetectionTimesendHangDetectionTime "PT10S""PT10S"
      shutdownTimeoutshutdownTimeout "PT15S""PT15S"
    • *直接模式的编程提示 _*Programming tips for Direct mode _

      查看 Azure Cosmos DB Async Java SDK v2 故障排除一文,将其作为解决任何 SDK 问题的基线。Review the Azure Cosmos DB Async Java SDK v2 Troubleshooting article as a baseline for resolving any SDK issues.

      使用直接模式时的一些重要编程技巧:Some important programming tips when using Direct mode:

      在应用程序中使用多线程处理进行高效的 TCP 数据传输 - 发出请求后,应用程序应通过订阅来接收另一线程上的数据。_ Use multithreading in your application for efficient TCP data transfer - After making a request, your application should subscribe to receive data on another thread. 否则,将强制执行非预期的“半双工”操作,并且将阻止后续请求,以等待上一个请求的回复。Not doing so forces unintended "half-duplex" operation and the subsequent requests are blocked waiting for the previous request's reply.

        * **Carry out compute-intensive workloads on a dedicated thread** - For similar reasons to the previous tip, operations such as complex   data processing are best placed in a separate thread. A request that pulls in data from another data store (for example if the thread   utilizes Azure Cosmos DB and Spark data stores simultaneously) may experience increased latency and it is recommended to spawn an   additional thread that awaits a response from the other data store.
      
        * The underlying network IO in the Azure Cosmos DB Async Java SDK v2 is managed by Netty, see these [tips for avoiding coding   patterns that block Netty IO threads](troubleshoot-java-async-sdk.md#invalid-coding-pattern-blocking-netty-io-thread).
      
      • 数据建模 - Azure Cosmos DB SLA 假定文档大小小于 1KB。Data modeling - The Azure Cosmos DB SLA assumes document size to be less than 1KB. 优化数据模型和编程以优先使用较小的文档大小通常可以降低延迟。Optimizing your data model and programming to favor smaller document size will generally lead to decreased latency. 如果需要存储和检索大于 1 KB 的文档,建议的方法是将文档链接到 Azure Blob 存储中的数据。If you are going to need storage and retrieval of docs larger than 1KB, the recommended approach is for documents to link to data in Azure Blob Storage.
  • 优化分区集合的并行查询。Tuning parallel queries for partitioned collections

    Azure Cosmos DB Async Java SDK v2 支持并行查询,使你能够并行查询分区集合。Azure Cosmos DB Async Java SDK v2 supports parallel queries, which enable you to query a partitioned collection in parallel. 有关详细信息,请参阅与使用这些 SDK 相关的代码示例For more information, see code samples related to working with the SDKs. 并行查询旨改善查询延迟和串行配对物上的吞吐量。Parallel queries are designed to improve query latency and throughput over their serial counterpart.

    • *优化 setMaxDegreeOfParallelism: _*Tuning setMaxDegreeOfParallelism: _

      并行查询的方式是并行查询多个分区。Parallel queries work by querying multiple partitions in parallel. 但就查询本身而言,会按顺序提取单个已分区集合中的数据。However, data from an individual partitioned collection is fetched serially with respect to the query. 因此,通过使用 setMaxDegreeOfParallelism 设置分区数,最有可能实现查询的最高性能,但前提是所有其他系统条件仍保持不变。So, use setMaxDegreeOfParallelism to set the number of partitions that has the maximum chance of achieving the most performant query, provided all other system conditions remain the same. 如果不知道分区数,可使用 setMaxDegreeOfParallelism 设置一个较高的数值,系统会选择最小值(分区数、用户输入)作为最大并行度。If you don't know the number of partitions, you can use setMaxDegreeOfParallelism to set a high number, and the system chooses the minimum (number of partitions, user provided input) as the maximum degree of parallelism.

      必须注意,如果查询时数据均衡分布在所有分区之间,则并行查询可提供最大的优势。It is important to note that parallel queries produce the best benefits if the data is evenly distributed across all partitions with respect to the query. 如果对分区集合进行分区,其中全部或大部分查询所返回的数据集中于几个分区(最坏的情况下为一个分区),则这些分区会遇到查询的性能瓶颈。If the partitioned collection is partitioned such a way that all or a majority of the data returned by a query is concentrated in a few partitions (one partition in worst case), then the performance of the query would be bottlenecked by those partitions.

    _ *优化 setMaxBufferedItemCount: *Tuning setMaxBufferedItemCount: _

      Parallel query is designed to pre-fetch results while the current batch of results is being processed by the client. The pre-fetching helps in overall latency improvement of a query. setMaxBufferedItemCount limits the number of pre-fetched results. Setting setMaxBufferedItemCount to the expected number of results returned (or a higher number) enables the query to receive maximum benefit from pre-fetching.
    
      Pre-fetching works the same way irrespective of the MaxDegreeOfParallelism, and there is a single buffer for the data from all partitions.
    

_ 按 getRetryAfterInMilliseconds 间隔实现退避_ Implement backoff at getRetryAfterInMilliseconds intervals

During performance testing, you should increase load until a small rate of requests get throttled. If throttled, the client application should backoff for the server-specified retry interval. Respecting the backoff ensures that you spend minimal amount of time waiting between retries.
  • 增大客户端工作负荷Scale out your client-workload

    如果以高吞吐量级别(> 50,000 RU/s)进行测试,客户端应用程序可能成为瓶颈,因为计算机的 CPU 或网络利用率将达到上限。If you are testing at high throughput levels (>50,000 RU/s), the client application may become the bottleneck due to the machine capping out on CPU or network utilization. 如果达到此上限,可以跨多个服务器横向扩展客户端应用程序以继续进一步推送 Azure Cosmos DB 帐户。If you reach this point, you can continue to push the Azure Cosmos DB account further by scaling out your client applications across multiple servers.

  • 使用基于名称的寻址Use name based addressing

    使用基于名称的寻址,其中的链接格式为 dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId,而不是使用格式为 dbs/<database_rid>/colls/<collection_rid>/docs/<document_rid> 的 SelfLinks (_self)(旨在避免检索用于构造链接的所有资源的 ResourceId)。Use name-based addressing, where links have the format dbs/MyDatabaseId/colls/MyCollectionId/docs/MyDocumentId, instead of SelfLinks (_self), which have the format dbs/<database_rid>/colls/<collection_rid>/docs/<document_rid> to avoid retrieving ResourceIds of all the resources used to construct the link. 此外,由于会重新创建这些资源(名称可能相同),因此,缓存这些资源的用处不大。Also, as these resources get recreated (possibly with same name), caching them may not help.

  • 调整查询/读取源的页面大小以获得更好的性能Tune the page size for queries/read feeds for better performance

    使用读取源功能(例如 readDocuments)执行批量文档读取时,或发出 SQL 查询时,如果结果集太大,则以分段方式返回结果。When performing a bulk read of documents by using read feed functionality (for example, readDocuments) or when issuing a SQL query, the results are returned in a segmented fashion if the result set is too large. 默认情况下,以包括 100 个项的块或 1 MB 大小的块返回结果(以先达到的限制为准)。By default, results are returned in chunks of 100 items or 1 MB, whichever limit is hit first.

    若要减少检索所有适用结果所需的网络往返次数,可以使用 x-ms-max-item-count 请求标头将页面大小最大增加到 1000。To reduce the number of network round trips required to retrieve all applicable results, you can increase the page size using the x-ms-max-item-count request header to up to 1000. 在只需要显示几个结果的情况下(例如,用户界面或应用程序 API 一次只返回 10 个结果),也可以将页面大小缩小为 10,以降低读取和查询所耗用的吞吐量。In cases where you need to display only a few results, for example, if your user interface or application API returns only 10 results a time, you can also decrease the page size to 10 to reduce the throughput consumed for reads and queries.

    也可以使用 setMaxItemCount 方法设置页面大小。You may also set the page size using the setMaxItemCount method.

  • 使用相应的计划程序(避免窃取事件循环 IO Netty 线程)Use Appropriate Scheduler (Avoid stealing Event loop IO Netty threads)

    Azure Cosmos DB Async Java SDK v2 对非阻塞 IO使用 nettyThe Azure Cosmos DB Async Java SDK v2 uses netty for non-blocking IO. SDK 使用固定数量的 IO netty 事件循环线程(数量与计算机提供的 CPU 核心数相同)来执行 IO 操作。The SDK uses a fixed number of IO netty event loop threads (as many CPU cores your machine has) for executing IO operations. API 返回的可观测对象针对某个共享的 IO 事件循环 netty 线程发出结果。The Observable returned by API emits the result on one of the shared IO event loop netty threads. 因此,切勿阻塞共享的 IO 事件循环 netty 线程。So it is important to not block the shared IO event loop netty threads. 针对 IO 事件循环 netty 线程执行 CPU 密集型工作或者阻塞操作可能导致死锁,或大大减少 SDK 吞吐量。Doing CPU intensive work or blocking operation on the IO event loop netty thread may cause deadlock or significantly reduce SDK throughput.

    例如,以下代码针对事件循环 IO netty 线程执行 CPU 密集型工作:For example the following code executes a cpu intensive work on the event loop IO netty thread:

    Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)

    Observable<ResourceResponse<Document>> createDocObs = asyncDocumentClient.createDocument(
      collectionLink, document, null, true);
    
    createDocObs.subscribe(
      resourceResponse -> {
        //this is executed on eventloop IO netty thread.
        //the eventloop thread is shared and is meant to return back quickly.
        //
        // DON'T do this on eventloop IO netty thread.
        veryCpuIntensiveWork();
      });
    

    收到结果后,如果想要针对结果执行 CPU 密集型工作,应避免针对事件循环 IO netty 线程执行。After result is received if you want to do CPU intensive work on the result you should avoid doing so on event loop IO netty thread. 可以提供自己的计划程序,以提供自己的线程来运行工作。You can instead provide your own Scheduler to provide your own thread for running your work.

    Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)

    import rx.schedulers;
    
    Observable<ResourceResponse<Document>> createDocObs = asyncDocumentClient.createDocument(
      collectionLink, document, null, true);
    
    createDocObs.subscribeOn(Schedulers.computation())
    subscribe(
      resourceResponse -> {
        // this is executed on threads provided by Scheduler.computation()
        // Schedulers.computation() should be used only when:
        //   1. The work is cpu intensive 
        //   2. You are not doing blocking IO, thread sleep, etc. in this thread against other resources.
        veryCpuIntensiveWork();
      });
    

    根据工作的类型,应该使用相应的现有 RxJava 计划程序来执行工作。Based on the type of your work you should use the appropriate existing RxJava Scheduler for your work. 请阅读 SchedulersRead here Schedulers.

    有关详细信息,请查看 Azure Cosmos DB Async Java SDK v2 的 GitHub 页For More Information, Please look at the GitHub page for Azure Cosmos DB Async Java SDK v2.

  • 禁用 netty 的日志记录Disable netty's logging

    Netty 库日志记录非常琐碎,因此需要将其关闭(在配置中禁止登录可能并不足够),以避免产生额外的 CPU 开销。Netty library logging is chatty and needs to be turned off (suppressing sign in the configuration may not be enough) to avoid additional CPU costs. 如果不处于调试模式,请一起禁用 netty 日志记录。If you are not in debugging mode, disable netty's logging altogether. 因此,如果要使用 log4j 来消除 netty 中 org.apache.log4j.Category.callAppenders() 产生的额外 CPU 开销,请将以下行添加到基代码:So if you are using log4j to remove the additional CPU costs incurred by org.apache.log4j.Category.callAppenders() from netty add the following line to your codebase:

    org.apache.log4j.Logger.getLogger("io.netty").setLevel(org.apache.log4j.Level.OFF);
    
  • OS 打开文件资源限制OS Open files Resource Limit

    某些 Linux 系统(例如 CentOS)对打开的文件数和连接总数施加了上限。Some Linux systems (like CentOS) have an upper limit on the number of open files and so the total number of connections. 运行以下命令以查看当前限制:Run the following to view the current limits:

    ulimit -a
    

    打开的文件数 (nofile) 需要足够大,以便为配置的连接池大小和 OS 打开的其他文件留出足够的空间。The number of open files (nofile) needs to be large enough to have enough room for your configured connection pool size and other open files by the OS. 可以修改此参数,以增大连接池大小。It can be modified to allow for a larger connection pool size.

    打开 limits.conf 文件:Open the limits.conf file:

    vim /etc/security/limits.conf
    

    添加/修改以下行:Add/modify the following lines:

    * - nofile 100000
    

索引策略Indexing Policy

  • 从索引中排除未使用的路径以加快写入速度Exclude unused paths from indexing for faster writes

    Azure Cosmos DB 的索引策略允许使用索引路径(setIncludedPaths 和 setExcludedPaths)指定要在索引中包括或排除的文档路径。Azure Cosmos DB's indexing policy allows you to specify which document paths to include or exclude from indexing by leveraging Indexing Paths (setIncludedPaths and setExcludedPaths). 在事先知道查询模式的方案中,使用索引路径可改善写入性能并降低索引存储空间,因为索引成本与索引的唯一路径数目直接相关。The use of indexing paths can offer improved write performance and lower index storage for scenarios in which the query patterns are known beforehand, as indexing costs are directly correlated to the number of unique paths indexed. 例如,以下代码演示如何使用“*”通配符从索引编制中排除文档的整个部分(也称为子树)。For example, the following code shows how to exclude an entire section of the documents (also known as a subtree) from indexing using the "*" wildcard.

    Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)

    Index numberIndex = Index.Range(DataType.Number);
    numberIndex.set("precision", -1);
    indexes.add(numberIndex);
    includedPath.setIndexes(indexes);
    includedPaths.add(includedPath);
    indexingPolicy.setIncludedPaths(includedPaths);
    collectionDefinition.setIndexingPolicy(indexingPolicy);
    

    有关索引的详细信息,请参阅 Azure Cosmos DB 索引策略For more information, see Azure Cosmos DB indexing policies.

吞吐量Throughput

  • 测量和优化较低的每秒请求单位使用量Measure and tune for lower request units/second usage

    Azure Cosmos DB 提供一组丰富的数据库操作,包括 UDF 的关系和层次查询,存储过程和触发器 - 所有这些都是对数据库集合内的文档进行的操作。Azure Cosmos DB offers a rich set of database operations including relational and hierarchical queries with UDFs, stored procedures, and triggers - all operating on the documents within a database collection. 与这些操作关联的成本取决于完成操作所需的 CPU、IO 和内存。The cost associated with each of these operations varies based on the CPU, IO, and memory required to complete the operation. 与考虑和管理硬件资源不同的是,可以考虑将请求单位 (RU) 作为所需资源的单个措施,以执行各种数据库操作和服务应用程序请求。Instead of thinking about and managing hardware resources, you can think of a request unit (RU) as a single measure for the resources required to perform various database operations and service an application request.

    吞吐量是基于为每个容器设置的请求单位数量预配的。Throughput is provisioned based on the number of request units set for each container. 请求单位消耗以每秒速率评估。Request unit consumption is evaluated as a rate per second. 如果应用程序的速率超过了为其容器预配的请求单位速率,则会受到限制,直到该速率降到容器的预配级别以下。Applications that exceed the provisioned request unit rate for their container are limited until the rate drops below the provisioned level for the container. 如果应用程序需要较高级别的吞吐量,可以通过预配更多请求单位来增加吞吐量。If your application requires a higher level of throughput, you can increase your throughput by provisioning additional request units.

    查询的复杂性会影响操作使用的请求单位数量。The complexity of a query impacts how many request units are consumed for an operation. 谓词数、谓词性质、UDF 数目和源数据集的大小都会影响查询操作的成本。The number of predicates, nature of the predicates, number of UDFs, and the size of the source data set all influence the cost of query operations.

    若要测量任何操作(创建、更新或删除)的开销,请检查 x-ms-request-charge 标头来测量这些操作占用的请求单位数。To measure the overhead of any operation (create, update, or delete), inspect the x-ms-request-charge header to measure the number of request units consumed by these operations. 也可以在 ResourceResponse<T> 或 FeedResponse<T> 中找到等效的 RequestCharge 属性。You can also look at the equivalent RequestCharge property in ResourceResponse<T> or FeedResponse<T>.

    Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)Async Java SDK V2 (Maven com.microsoft.azure::azure-cosmosdb)

    ResourceResponse<Document> response = asyncClient.createDocument(collectionLink, documentDefinition, null,
                                                 false).toBlocking.single();
    response.getRequestCharge();
    

在此标头中返回的请求费用是预配吞吐量的一小部分。The request charge returned in this header is a fraction of your provisioned throughput. 例如,如果预配了 2000 RU/s,上述查询返回 1000 个 1KB 文档,则操作成本为 1000。For example, if you have 2000 RU/s provisioned, and if the preceding query returns 1000 1KB-documents, the cost of the operation is 1000. 因此在一秒内,服务器在对后续请求进行速率限制之前,只接受两个此类请求。As such, within one second, the server honors only two such requests before rate limiting subsequent requests. 有关详细信息,请参阅请求单位请求单位计算器For more information, see Request units and the request unit calculator.

  • 处理速率限制/请求速率太大Handle rate limiting/request rate too large

    客户端尝试超过帐户保留的吞吐量时,服务器的性能不会降低,并且不会使用超过保留级别的吞吐量容量。When a client attempts to exceed the reserved throughput for an account, there is no performance degradation at the server and no use of throughput capacity beyond the reserved level. 服务器将抢先结束 RequestRateTooLarge(HTTP 状态代码 429)的请求并返回 x-ms-retry-after-ms 标头,该标头指示重新尝试请求前用户必须等待的时间量(以毫秒为单位)。The server will preemptively end the request with RequestRateTooLarge (HTTP status code 429) and return the x-ms-retry-after-ms header indicating the amount of time, in milliseconds, that the user must wait before reattempting the request.

    HTTP Status 429,
    Status Line: RequestRateTooLarge
    x-ms-retry-after-ms :100
    

    SDK 全部都会隐式捕获此响应,并遵循服务器指定的 retry-after 标头,并重试请求。The SDKs all implicitly catch this response, respect the server-specified retry-after header, and retry the request. 除非多个客户端同时访问帐户,否则下次重试就会成功。Unless your account is being accessed concurrently by multiple clients, the next retry will succeed.

    如果存在多个高于请求速率的请求操作,则客户端当前在内部设置为 9 的默认重试计数可能无法满足需要;在此情况下,客户端就会向应用程序引发 DocumentClientException,其状态代码为 429。If you have more than one client cumulatively operating consistently above the request rate, the default retry count currently set to 9 internally by the client may not suffice; in this case, the client throws a DocumentClientException with status code 429 to the application. 可以通过在 ConnectionPolicy 实例上使用 setRetryOptions 来更改默认重试计数。The default retry count can be changed by using setRetryOptions on the ConnectionPolicy instance. 默认情况下,如果请求继续以高于请求速率的方式运行,则在 30 秒的累积等待时间后返回 DocumentClientException 和状态代码 429。By default, the DocumentClientException with status code 429 is returned after a cumulative wait time of 30 seconds if the request continues to operate above the request rate. 即使当前的重试计数小于最大重试计数(默认值 9 或用户定义的值),也会发生这种情况。This occurs even when the current retry count is less than the max retry count, be it the default of 9 or a user-defined value.

    尽管自动重试行为有助于改善大多数应用程序的复原能力和可用性,但是在执行性能基准测试时可能会造成冲突(尤其是在测量延迟时)。While the automated retry behavior helps to improve resiliency and usability for the most applications, it might come at odds when doing performance benchmarks, especially when measuring latency. 如果实验达到服务器限制并导致客户端 SDK 静默重试,则客户端观测到的延迟会剧增。The client-observed latency will spike if the experiment hits the server throttle and causes the client SDK to silently retry. 若要避免性能实验期间出现延迟高峰,可以测量每个操作返回的费用,并确保请求以低于保留请求速率的方式运行。To avoid latency spikes during performance experiments, measure the charge returned by each operation and ensure that requests are operating below the reserved request rate. 有关详细信息,请参阅请求单位For more information, see Request units.

  • 针对小型文档进行设计以提高吞吐量Design for smaller documents for higher throughput

    给定操作的请求费用(请求处理成本)与文档大小直接相关。The request charge (the request processing cost) of a given operation is directly correlated to the size of the document. 大型文档的操作成本高于小型文档的操作成本。Operations on large documents cost more than operations for small documents.

后续步骤Next steps

若要深入了解如何设计应用程序以实现缩放和高性能,请参阅 Azure Cosmos DB 中的分区和缩放To learn more about designing your application for scale and high performance, see Partitioning and scaling in Azure Cosmos DB.