Skip to content

Commit 2520b8a

Browse files
authored
Update performance-tips-java-sdk-v4.md
Enhance threshold based availability strategy to include proactive connection management.
1 parent f930e18 commit 2520b8a

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,28 @@ These techniques provide advanced mechanisms to address specific latency and ava
6363

6464
### Threshold-based availability strategy
6565

66-
The threshold-based availability strategy can improve tail latency and availability by sending parallel read requests to secondary regions and accepting the fastest response. This approach can drastically reduce the impact of regional outages or high-latency conditions on application performance.
66+
The threshold-based availability strategy can improve tail latency and availability by sending parallel read requests to secondary regions and accepting the fastest response. This approach can drastically reduce the impact of regional outages or high-latency conditions on application performance. Additionally, proactive connection management can be employed to further enhance performance by warming up connections and caches across both the current read region and preferred remote regions.
6767

6868
**Example configuration:**
6969
```java
70+
// Proactive Connection Management
71+
CosmosContainerIdentity containerIdentity = new CosmosContainerIdentity("sample_db_id", "sample_container_id");
72+
int proactiveConnectionRegionsCount = 1;
73+
Duration aggressiveWarmupDuration = Duration.ofSeconds(1);
74+
75+
CosmosAsyncClient clientWithOpenConnections = new CosmosClientBuilder()
76+
.endpoint("")
77+
.endpointDiscoveryEnabled(true)
78+
.preferredRegions(Arrays.asList("sample_region_1", "sample_region_2"))
79+
.openConnectionsAndInitCaches(new CosmosContainerProactiveInitConfigBuilder(Arrays.asList(containerIdentity))
80+
.setProactiveConnectionRegionsCount(proactiveConnectionRegionsCount)
81+
.setAggressiveWarmupDuration(aggressiveWarmupDuration)
82+
.build())
83+
.directMode()
84+
.buildAsyncClient();
85+
86+
CosmosAsyncContainer container = clientWithOpenConnections.getDatabase("sample_db_id").getContainer("sample_container_id");
87+
7088
int threshold = 500;
7189
int thresholdStep = 100;
7290

@@ -79,8 +97,8 @@ options.setCosmosEndToEndOperationLatencyPolicyConfig(config);
7997

8098
container.readItem("id", new PartitionKey("pk"), options, JsonNode.class).block();
8199

82-
//Write operations can benefit from threshold-based availability strategy if opted into non-idempotent write retry policy
83-
//and the account is configured for multi-region writes.
100+
// Write operations can benefit from threshold-based availability strategy if opted into non-idempotent write retry policy
101+
// and the account is configured for multi-region writes.
84102
options.setNonIdempotentWriteRetryPolicy(true, true);
85103
container.createItem("id", new PartitionKey("pk"), options, JsonNode.class).block();
86104
```
@@ -95,6 +113,8 @@ container.createItem("id", new PartitionKey("pk"), options, JsonNode.class).bloc
95113

96114
4. **Fastest Response Wins:** Whichever region responds first, that response is accepted, and the other parallel requests are ignored.
97115

116+
Proactive connection management helps by warming up connections and caches for containers across the preferred regions, reducing latency for failover scenarios or writes in multi-region setups.
117+
98118
This strategy can significantly improve latency in scenarios where a particular region is slow or temporarily unavailable, but it may incur more cost in terms of request units when parallel cross-region requests are required.
99119

100120
> [!NOTE]

0 commit comments

Comments
 (0)