Skip to content

Commit 719703d

Browse files
authored
Merge pull request #214793 from kushagraThapar/added_connection_config_file
Added tune connection configurations for java V4 SDK for direct and gateway mode
2 parents 40eded1 + eb77430 commit 719703d

File tree

4 files changed

+123
-67
lines changed

4 files changed

+123
-67
lines changed

articles/cosmos-db/nosql/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@
598598
href: best-practice-java.md
599599
- name: Performance tips for Java SDK v4
600600
href: performance-tips-java-sdk-v4.md
601+
- name: Connection Configurations for Java SDK v4
602+
href: tune-connection-configurations-java-sdk-v4.md
601603
- name: Async Java SDK v2
602604
href: performance-tips-async-java.md
603605
- name: Sync Java SDK v2

articles/cosmos-db/nosql/performance-tips-java-sdk-v4.md

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,6 @@ Azure Cosmos DB is a fast and flexible distributed database that scales seamless
3131
So if you're asking "How can I improve my database performance?" consider the following options:
3232

3333
## Networking
34-
35-
* **Connection mode: Use Direct mode**
36-
37-
Java SDK default connection mode is direct. You can configure the connection mode in the client builder using the *directMode()* or *gatewayMode()* methods, as shown below. To configure either mode with default settings, call either method without arguments. Otherwise, pass a configuration settings class instance as the argument (*DirectConnectionConfig* for *directMode()*, *GatewayConnectionConfig* for *gatewayMode()*.). To learn more about different connectivity options, see the [connectivity modes](sdk-connection-modes.md) article.
38-
39-
# [Async](#tab/api-async)
40-
41-
Java SDK V4 (Maven com.azure::azure-cosmos) Async API
42-
43-
[!code-java[](~/azure-cosmos-java-sql-api-samples/src/main/java/com/azure/cosmos/examples/documentationsnippets/async/SampleDocumentationSnippetsAsync.java?name=PerformanceClientConnectionModeAsync)]
44-
45-
# [Sync](#tab/api-sync)
46-
47-
Java SDK V4 (Maven com.azure::azure-cosmos) Sync API
48-
49-
[!code-java[](~/azure-cosmos-java-sql-api-samples/src/main/java/com/azure/cosmos/examples/documentationsnippets/sync/SampleDocumentationSnippets.java?name=PerformanceClientConnectionModeSync)]
50-
51-
---
52-
53-
The *directMode()* method has an additional override, for the following reason. Control plane operations such as database and container CRUD *always* utilize Gateway mode; when the user has configured Direct mode for data plane operations, control plane operations use default Gateway mode settings. This suits most users. However, users who want Direct mode for data plane operations as well as tunability of control plane Gateway mode parameters can use the following *directMode()* override:
54-
55-
# [Async](#tab/api-async)
56-
57-
Java SDK V4 (Maven com.azure::azure-cosmos) Async API
58-
59-
[!code-java[](~/azure-cosmos-java-sql-api-samples/src/main/java/com/azure/cosmos/examples/documentationsnippets/async/SampleDocumentationSnippetsAsync.java?name=PerformanceClientDirectOverrideAsync)]
60-
61-
# [Sync](#tab/api-sync)
62-
63-
Java SDK V4 (Maven com.azure::azure-cosmos) Sync API
64-
65-
[!code-java[](~/azure-cosmos-java-sql-api-samples/src/main/java/com/azure/cosmos/examples/documentationsnippets/sync/SampleDocumentationSnippets.java?name=PerformanceClientDirectOverrideSync)]
66-
67-
---
68-
6934
<a name="collocate-clients"></a>
7035
* **Collocate clients in same Azure region for performance**
7136
<a id="same-region"></a>
@@ -87,6 +52,10 @@ Limitations: accelerated networking must be supported on the VM OS, and can only
8752

8853
Please see the [Windows](../../virtual-network/create-vm-accelerated-networking-powershell.md) and [Linux](../../virtual-network/create-vm-accelerated-networking-cli.md) instructions for more details.
8954

55+
## Tuning direct and gateway connection configuration
56+
57+
For optimizing direct and gateway mode connection configurations, see how to [tune connection configurations for java sdk v4](tune-connection-configurations-java-sdk-v4.md).
58+
9059
## SDK usage
9160
* **Install the most recent SDK**
9261

@@ -132,35 +101,6 @@ Java SDK V4 (Maven com.azure::azure-cosmos) Sync API
132101

133102
---
134103

135-
* **Tuning ConnectionPolicy**
136-
137-
By default, Direct mode Azure Cosmos DB requests are made over TCP when using Azure Cosmos DB Java SDK v4. Internally Direct mode uses a special architecture to dynamically manage network resources and get the best performance.
138-
139-
In Azure Cosmos DB Java SDK v4, Direct mode is the best choice to improve database performance with most workloads.
140-
141-
* ***Overview of Direct mode***
142-
<a id="direct-connection"></a>
143-
144-
:::image type="content" source="./media/performance-tips-async-java/rntbdtransportclient.png" alt-text="Illustration of the Direct mode architecture" border="false":::
145-
146-
The client-side architecture employed in Direct mode enables predictable network utilization and multiplexed access to Azure Cosmos DB replicas. The diagram above shows how Direct mode routes client requests to replicas in the Azure Cosmos DB backend. The Direct mode architecture allocates up to 130 **Channels** on the client side per DB replica. 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**. 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.
147-
148-
* ***Configuration options for Direct mode***
149-
150-
If non-default Direct mode behavior is desired, create a *DirectConnectionConfig* instance and customize its properties, then pass the customized property instance to the *directMode()* method in the Azure Cosmos DB client builder.
151-
152-
These configuration settings control the behavior of the underlying Direct mode architecture discussed above.
153-
154-
As a first step, use the following recommended configuration settings below. These *DirectConnectionConfig* options are advanced configuration settings which can affect SDK performance in unexpected ways; we recommend users avoid modifying them unless they feel very comfortable in understanding the tradeoffs and it is absolutely necessary. Please contact the [Azure Cosmos DB team](mailto:[email protected]) if you run into issues on this particular topic.
155-
156-
| Configuration option | Default |
157-
| :------------------: | :-----: |
158-
| idleConnectionTimeout | "PT0" |
159-
| maxConnectionsPerEndpoint | "130" |
160-
| connectTimeout | "PT5S" |
161-
| idleEndpointTimeout | "PT1H" |
162-
| maxRequestsPerConnection | "30" |
163-
164104
* **Scale out your client-workload**
165105

166106
If you are testing at high throughput levels, the client application may become the bottleneck due to the machine capping out on CPU or network utilization. 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.
@@ -263,6 +203,7 @@ Java SDK V4 (Maven com.azure::azure-cosmos) Sync API
263203

264204
The latter is supported but will add latency to your application; the SDK must parse the item and extract the partition key.
265205

206+
266207
## Query operations
267208

268209
For query operations see the [performance tips for queries](performance-tips-query-sdk.md?pivots=programming-language-java).
@@ -319,7 +260,7 @@ x-ms-retry-after-ms :100
319260

320261
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.
321262

322-
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 *CosmosClientException* with status code 429 to the application. The default retry count can be changed by using setRetryOptions on the ConnectionPolicy instance. By default, the *CosmosClientException* with status code 429 is returned after a cumulative wait time of 30 seconds if the request continues to operate above the request rate. 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.
263+
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 *CosmosClientException* with status code 429 to the application. The default retry count can be changed by using `setMaxRetryAttemptsOnThrottledRequests()` on the `ThrottlingRetryOptions` instance. By default, the *CosmosClientException* with status code 429 is returned after a cumulative wait time of 30 seconds if the request continues to operate above the request rate. 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.
323264

324265
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. 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](../request-units.md).
325266

articles/cosmos-db/nosql/sdk-connection-modes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ There are two factors that dictate the number of TCP connections the SDK will op
7474

7575
Each established connection can serve a configurable number of concurrent operations. If the volume of concurrent operations exceeds this threshold, new connections will be open to serve them, and it's possible that for a physical partition, the number of open connections exceeds the steady state number. This behavior is expected for workloads that might have spikes in their operational volume. For the .NET SDK this configuration is set by [CosmosClientOptions.MaxRequestsPerTcpConnection](/dotnet/api/microsoft.azure.cosmos.cosmosclientoptions.maxrequestspertcpconnection), and for the Java SDK you can customize using [DirectConnectionConfig.setMaxRequestsPerConnection](/java/api/com.azure.cosmos.directconnectionconfig.setmaxrequestsperconnection).
7676

77-
By default, connections are permanently maintained to benefit the performance of future operations (opening a connection has computational overhead). There might be some scenarios where you might want to close connections that are unused for some time understanding that this might affect future operations slightly. For the .NET SDK this configuration is set by [CosmosClientOptions.IdleTcpConnectionTimeout](/dotnet/api/microsoft.azure.cosmos.cosmosclientoptions.idletcpconnectiontimeout), and for the Java SDK you can customize using [DirectConnectionConfig.setIdleConnectionTimeout](/java/api/com.azure.cosmos.directconnectionconfig.setidleconnectiontimeout). It isn't recommended to set these configurations to low values as it might cause connections to be frequently closed and affect overall performance.
77+
By default, connections are permanently maintained to benefit the performance of future operations (opening a connection has computational overhead). There might be some scenarios where you might want to close connections that are unused for some time understanding that this might affect future operations slightly. For the .NET SDK this configuration is set by [CosmosClientOptions.IdleTcpConnectionTimeout](/dotnet/api/microsoft.azure.cosmos.cosmosclientoptions.idletcpconnectiontimeout), and for the Java SDK you can customize using [DirectConnectionConfig.setIdleConnectionTimeout](/java/api/com.azure.cosmos.directconnectionconfig.setidleconnectiontimeout). It isn't recommended to set these configurations to low values as it might cause connections to be frequently closed and effect overall performance.
7878

7979
### Language specific implementation details
8080

8181
For further implementation details regarding a language see:
8282

8383
* [.NET SDK implementation information](https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/docs/SdkDesign.md)
84-
* [Java SDK direct mode information](performance-tips-java-sdk-v4.md#direct-connection)
84+
* [Java SDK direct mode information](tune-connection-configurations-java-sdk-v4.md#direct-connection-mode)
8585

8686
## Next steps
8787

0 commit comments

Comments
 (0)