适用于 Azure Cosmos DB Sync Java SDK v2 的性能提示Performance tips for Azure Cosmos DB Sync 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 Sync Java SDK v2。These performance tips are for Azure Cosmos DB Sync Java SDK v2 only. 有关详细信息,请查看 Azure Cosmos DB Sync Java SDK v2 的发行说明Maven 存储库Please view the Azure Cosmos DB Sync Java SDK v2 Release notes and Maven repository 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 调用一样简单。Scaling up and down is as easy as making a single API call. 若要了解详细信息,请参阅如何预配容器吞吐量如何预配数据库吞吐量To learn more, see how to provision container throughput or how to provision database throughput. 但是,由于 Azure Cosmos DB 通过网络调用进行访问,因此,使用 Azure Cosmos DB Sync 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 Azure Cosmos DB Sync Java SDK v2.

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

网络Networking

  1. 连接模式:使用 DirectHttpsConnection mode: Use DirectHttps

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

    1. 网关(默认值)Gateway (default)

    2. DirectHttpsDirectHttps

      网关模式受所有 SDK 平台的支持并已配置为默认设置。Gateway mode is supported on all SDK platforms and is the configured default. 如果应用程序在有严格防火墙限制的企业网络中运行,则网关是最佳选择,因为它使用标准 HTTPS 端口与单个终结点。If your application runs within a corporate network with strict firewall restrictions, Gateway 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. 因此,DirectHttps 模式因为网络跃点较少,可以提供更好的性能。Because of this, DirectHttps mode offers better performance due to fewer network hops.

      Azure Cosmos DB Sync Java SDK v2 使用 HTTPS 作为传输协议。The Azure Cosmos DB Sync Java SDK v2 uses HTTPS as a transport protocol. HTTPS 使用 TLS 进行初始身份验证和加密通信。HTTPS uses TLS for initial authentication and encrypting traffic. 使用 zure Cosmos DB Sync Java SDK v2 时,只需打开 HTTPS 端口 443。When using the Azure Cosmos DB Sync Java SDK v2, only HTTPS port 443 needs to be open.

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

      Sync Java SDK V2 (Maven com.microsoft.azure::azure-documentdb)Sync Java SDK V2 (Maven com.microsoft.azure::azure-documentdb)

      public ConnectionPolicy getConnectionPolicy() {
          ConnectionPolicy policy = new ConnectionPolicy();
          policy.setConnectionMode(ConnectionMode.DirectHttps);
          policy.setMaxPoolSize(1000);
          return policy;
      }
      
      ConnectionPolicy connectionPolicy = new ConnectionPolicy();
      DocumentClient client = new DocumentClient(HOST, MASTER_KEY, connectionPolicy, null);
      

      图中显示了 Azure Cosmos DB 连接策略。

  2. 将客户端并置在同一 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.

    图中显示了两个区域中的请求和响应,其中计算机通过中间层服务连接到 Cosmos DB 帐户。

SDK 用法SDK Usage

  1. 安装最新的 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 SDK 页以了解最新的 SDK 并查看改进内容。See the Azure Cosmos DB SDK pages to determine the most recent SDK and review improvements.

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

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

  3. 使用网关模式时,增加每个主机的 MaxPoolSizeIncrease MaxPoolSize per host when using Gateway mode

    使用“网关”模式时,Azure Cosmos DB 请求是通过 HTTPS/REST 发出的,并且受制于每个主机名或 IP 地址的默认连接限制。Azure Cosmos DB requests are made over HTTPS/REST when using Gateway mode, and are subjected to the default connection limit per hostname or IP address. 可能需要将 MaxPoolSize 设置为较大的值 (200-1000),以便客户端库能够同时利用多个连接来访问 Azure Cosmos DB。You may need to set the MaxPoolSize to a higher value (200-1000) so that the client library can utilize multiple simultaneous connections to Azure Cosmos DB. 在 Azure Cosmos DB Sync Java SDK v2 中,ConnectionPolicy.getMaxPoolSize 的默认值为 100。In the Azure Cosmos DB Sync Java SDK v2, the default value for ConnectionPolicy.getMaxPoolSize is 100. 使用 setMaxPoolSize 可更改该值。Use setMaxPoolSize to change the value.

  4. 优化分区集合的并行查询。Tuning parallel queries for partitioned collections

    Azure Cosmos DB Sync Java SDK 版本 1.9.0 和更高版本支持并行查询,使你能够并行查询分区集合。Azure Cosmos DB Sync Java SDK version 1.9.0 and above support 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.

    (a) *优化 setMaxDegreeOfParallelism: _ 并行查询的工作方式是并行查询多个分区。(a) *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.

    (b) *优化 setMaxBufferedItemCount:* 并行查询专用于在客户端处理当前一批结果时预提取结果。(b) 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 会限制预提取结果的数目。setMaxBufferedItemCount limits the number of pre-fetched results. 通过将 setMaxBufferedItemCount 设置为预期返回的结果数(或较高的数值),可使查询从预提取获得最大的好处。By setting setMaxBufferedItemCount to the expected number of results returned (or a higher number), this enables the query to receive maximum benefit from pre-fetching.

    预提取的工作方式不因 MaxDegreeOfParallelism 而异,并且有一个单独的缓冲区用来存储所有分区的数据。Pre-fetching works the same way irrespective of the MaxDegreeOfParallelism, and there is a single buffer for the data from all partitions.

  5. _ 按 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 on throttle for the server-specified retry interval. 遵循退让可确保最大程度地减少等待重试的时间。Respecting the backoff ensures that you spend minimal amount of time waiting between retries. 重试策略支持包含在 Azure Cosmos DB Sync Java SDK 版本 1.8.0 及更高版本中。Retry policy support is included in Version 1.8.0 and above of the Azure Cosmos DB Sync Java SDK. 有关详细信息,请参阅 getRetryAfterInMillisecondsFor more information, see getRetryAfterInMilliseconds.

  6. 增大客户端工作负荷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.

  7. 使用基于名称的寻址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 these may not help.

  8. 调整查询/读取源的页面大小以获得更好的性能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.

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

索引策略Indexing Policy

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

    Azure Cosmos DB 的索引策略允许使用索引路径(setIncludedPathssetExcludedPaths)指定要在索引中包括或排除的文档路径。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 (subtree) of the documents from indexing using the "*" wildcard.

    Sync Java SDK V2 (Maven com.microsoft.azure::azure-documentdb)Sync Java SDK V2 (Maven com.microsoft.azure::azure-documentdb)

    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

  1. 测量和优化较低的每秒请求单位使用量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 标头(或 ResourceResponse<T>FeedResponse<T> 中的等效 RequestCharge 属性)来测量这些操作占用的请求单位数。To measure the overhead of any operation (create, update, or delete), inspect the x-ms-request-charge header (or the equivalent RequestCharge property in ResourceResponse<T> or FeedResponse<T> to measure the number of request units consumed by these operations.

    Sync Java SDK V2 (Maven com.microsoft.azure::azure-documentdb)Sync Java SDK V2 (Maven com.microsoft.azure::azure-documentdb)

    ResourceResponse<Document> response = client.createDocument(collectionLink, documentDefinition, null, false);
    
    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.

  2. 处理速率限制/请求速率太大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.

  3. 针对小型文档进行设计以提高吞吐量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.