Skip to content

Commit b1eb32c

Browse files
committed
addressing comments
1 parent e9e4ddf commit b1eb32c

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

articles/cosmos-db/performance-tips.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ So if you're asking "How can I improve my database performance?" consider the fo
3030

3131
* Gateway Mode
3232

33-
Gateway Mode is supported on all SDK platforms and is the configured default for [SDK V2](sql-api-sdk-dotnet.md). If your application runs 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. 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. Gateway connection mode is also recommended when you run applications in environments with limited number of socket connections, for example when using Azure Functions or if you are on a consumption plan.
33+
Gateway Mode is supported on all SDK platforms and is the configured default for [SDK V2](sql-api-sdk-dotnet.md). If your application runs 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. 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. Gateway connection mode is also recommended when you run applications in environments with limited number of socket connections.
34+
35+
When using the SDK in Azure Functions, particularly in Consumption Plan, be mindful of the current [limits in connections](../azure-functions/manage-connections.md). In that case, Gateway Mode might be recommended if you are also working with other HTTP based clients within your Azure Functions application.
3436

3537
* Direct Mode
3638

@@ -46,26 +48,26 @@ So if you're asking "How can I improve my database performance?" consider the fo
4648

4749
Azure Cosmos DB offers a simple and open RESTful programming model over HTTPS. Additionally, it offers an efficient TCP protocol, which is also RESTful in its communication model and is available through the .NET client SDK. Both Direct TCP and HTTPS use SSL for initial authentication and encrypting traffic. For best performance, use the TCP protocol when possible.
4850

49-
For the SDK V2, the Connectivity Mode is configured during the construction of the DocumentClient instance with the ConnectionPolicy parameter. If Direct Mode is used, the Protocol can also be set within the ConnectionPolicy parameter.
51+
For SDK V3, the Connectivity Mode is configured during the construction of the CosmosClient instance, as part of the CosmosClientOptions.
5052

5153
```csharp
5254
var serviceEndpoint = new Uri("https://contoso.documents.net");
5355
var authKey = "your authKey from the Azure portal";
54-
DocumentClient client = new DocumentClient(serviceEndpoint, authKey,
55-
new ConnectionPolicy
56+
CosmosClient client = new CosmosClient(serviceEndpoint, authKey,
57+
new CosmosClientOptions
5658
{
5759
ConnectionMode = ConnectionMode.Direct,
5860
ConnectionProtocol = Protocol.Tcp
5961
});
6062
```
6163

62-
For SDK V3, the Connectivity Mode is configured during the construction of the CosmosClient instance, as part of the CosmosClientOptions.
64+
For the SDK V2, the Connectivity Mode is configured during the construction of the DocumentClient instance with the ConnectionPolicy parameter. If Direct Mode is used, the Protocol can also be set within the ConnectionPolicy parameter.
6365

6466
```csharp
6567
var serviceEndpoint = new Uri("https://contoso.documents.net");
6668
var authKey = "your authKey from the Azure portal";
67-
CosmosClient client = new CosmosClient(serviceEndpoint, authKey,
68-
new CosmosClientOptions
69+
DocumentClient client = new DocumentClient(serviceEndpoint, authKey,
70+
new ConnectionPolicy
6971
{
7072
ConnectionMode = ConnectionMode.Direct,
7173
ConnectionProtocol = Protocol.Tcp
@@ -105,15 +107,20 @@ So if you're asking "How can I improve my database performance?" consider the fo
105107
1. **Install the most recent SDK**
106108

107109
The Azure Cosmos DB SDKs are constantly being improved to provide the best performance. See the [Azure Cosmos DB SDK](sql-api-sdk-dotnet-standard.md) pages to determine the most recent SDK and review improvements.
108-
2. **Use a singleton Azure Cosmos DB client for the lifetime of your application**
110+
111+
2. **Use Stream APIs**
112+
113+
The [SDK V3](sql-api-sdk-dotnet-standard.md) contains Stream based APIs that can receive and return data without serializing. It is beneficial for middle tier applications that do not consume the responses from the SDK directly but relay them to other application tiers. Stream handling is included in our [samples](https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement).
114+
115+
3. **Use a singleton Azure Cosmos DB client for the lifetime of your application**
109116

110117
Each DocumentClient instance is thread-safe and performs efficient connection management and address caching when operating in Direct Mode. To allow efficient connection management and better performance by the SDK client, it is recommended to use a single instance per AppDomain for the lifetime of the application.
111118

112119
<a id="max-connection"></a>
113-
3. **Increase System.Net MaxConnections per host when using Gateway mode**
120+
4. **Increase System.Net MaxConnections per host when using Gateway mode**
114121

115122
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. You may need to set the MaxConnections to a higher value (100-1000) so that the client library can utilize multiple simultaneous connections to Azure Cosmos DB. In the .NET SDK 1.8.0 and above, the default value for [ServicePointManager.DefaultConnectionLimit](https://msdn.microsoft.com/library/system.net.servicepointmanager.defaultconnectionlimit.aspx) is 50 and to change the value, you can set the [Documents.Client.ConnectionPolicy.MaxConnectionLimit](https://msdn.microsoft.com/library/azure/microsoft.azure.documents.client.connectionpolicy.maxconnectionlimit.aspx) to a higher value.
116-
4. **Tuning parallel queries for partitioned collections**
123+
5. **Tuning parallel queries for partitioned collections**
117124

118125
SQL .NET SDK version 1.9.0 and above support parallel queries, which enable you to query a partitioned collection in parallel. For more information, see [code samples](https://github.com/Azure/azure-documentdb-dotnet/blob/master/samples/code-samples/Queries/Program.cs) related to working with the SDKs. Parallel queries are designed to improve query latency and throughput over their serial counterpart. Parallel queries provide two parameters that users can tune to custom-fit their requirements, (a) MaxDegreeOfParallelism: to control the maximum number of partitions then can be queried in parallel, and (b) MaxBufferedItemCount: to control the number of pre-fetched results.
119126
@@ -126,10 +133,10 @@ So if you're asking "How can I improve my database performance?" consider the fo
126133
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. MaxBufferedItemCount is the parameter to limit the number of pre-fetched results. Setting MaxBufferedItemCount to the expected number of results returned (or a higher number) allows the query to receive maximum benefit from pre-fetching.
127134

128135
Pre-fetching works the same way irrespective of the MaxDegreeOfParallelism, and there is a single buffer for the data from all partitions.
129-
5. **Turn on server-side GC**
136+
6. **Turn on server-side GC**
130137

131138
Reducing the frequency of garbage collection may help in some cases. In .NET, set [gcServer](https://msdn.microsoft.com/library/ms229357.aspx) to true.
132-
6. **Implement backoff at RetryAfter intervals**
139+
7. **Implement backoff at RetryAfter intervals**
133140

134141
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. Retry policy support is included in Version 1.8.0 and above of the SQL [.NET](sql-api-sdk-dotnet.md) and [Java](sql-api-sdk-java.md), version 1.9.0 and above of the [Node.js](sql-api-sdk-node.md) and [Python](sql-api-sdk-python.md), and all supported versions of the [.NET Core](sql-api-sdk-dotnet-core.md) SDKs. For more information, [RetryAfter](https://msdn.microsoft.com/library/microsoft.azure.documents.documentclientexception.retryafter.aspx).
135142
@@ -139,15 +146,15 @@ So if you're asking "How can I improve my database performance?" consider the fo
139146
readDocument.RequestDiagnosticsString
140147
```
141148

142-
7. **Scale out your client-workload**
149+
8. **Scale out your client-workload**
143150

144151
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. 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.
145-
8. **Cache document URIs for lower read latency**
152+
9. **Cache document URIs for lower read latency**
146153

147154
Cache document URIs whenever possible for the best read performance. You have to define logic to cache the resourceid when you create the resource. Resourceid based lookups are faster than name based lookups, so caching these values improves the performance.
148155

149156
<a id="tune-page-size"></a>
150-
1. **Tune the page size for queries/read feeds for better performance**
157+
10. **Tune the page size for queries/read feeds for better performance**
151158

152159
When performing a bulk read of documents using read feed functionality (for example, ReadDocumentFeedAsync) or when issuing a SQL query, the results are returned in a segmented fashion if the result set is too large. By default, results are returned in chunks of 100 items or 1 MB, whichever limit is hit first.
153160

@@ -164,11 +171,11 @@ So if you're asking "How can I improve my database performance?" consider the fo
164171

165172
When a query is executed, the resulting data is sent within a TCP packet. If you specify too low value for `maxItemCount`, the number of trips required to send the data within the TCP packet are high, which impacts the performance. So if you are not sure what value to set for `maxItemCount` property, it's best to set it to -1 and let the SDK choose the default value.
166173

167-
10. **Increase number of threads/tasks**
174+
11. **Increase number of threads/tasks**
168175

169176
See [Increase number of threads/tasks](#increase-threads) in the Networking section.
170177

171-
11. **Use 64-bit host processing**
178+
12. **Use 64-bit host processing**
172179

173180
The SQL SDK works in a 32-bit host process when you are using SQL .NET SDK version 1.11.4 and above. However, if you are using cross partition queries, 64-bit host processing is recommended for improved performance. The following types of applications have 32-bit host process as the default, so in order to change that to 64-bit, follow these steps based on the type of your application:
174181

0 commit comments

Comments
 (0)