You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/performance-tips.md
+45-22Lines changed: 45 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,18 +28,17 @@ So if you're asking "How can I improve my database performance?" consider the fo
28
28
29
29
How a client connects to Azure Cosmos DB has important implications on performance, especially in terms of observed client-side latency. There are two key configuration settings available for configuring client Connection Policy – the connection *mode* and the connection *protocol*. The two available modes are:
30
30
31
-
* Gateway Mode (default)
31
+
* Gateway mode
32
32
33
-
Gateway Mode is supported on all SDK platforms and is the configured default. 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 [Microsoft.Azure.DocumentDB SDK](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
34
35
-
* Direct Mode
35
+
When using the SDK in Azure Functions, particularly in [consumption plan](../azure-functions/functions-scale.md#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.
36
36
37
-
Direct mode supports connectivity through TCP and HTTPS protocols. If you are using the latest version of .NET SDK, direct connectivity mode is supported in .NET Standard 2.0 and .NET framework. When using Direct Mode, there are two protocol options available:
37
+
*Direct mode
38
38
39
-
* TCP
40
-
* HTTPS
39
+
Direct mode supports connectivity through TCP and HTTPS protocols and is the default connectivity mode if you are using [Microsoft.Azure.Cosmos/.Net V3 SDK](sql-api-sdk-dotnet-standard.md).
41
40
42
-
When using Gateway mode, Cosmos DB uses port 443 and ports 10250, 10255 and 10256 when using Azure Cosmos DB's API for MongoDB. The 10250 port maps to a default MongoDB instance without geo-replication and 10255/10256 ports map to the MongoDB instance with geo-replication functionality. When using TCP in Direct Mode, in addition to the Gateway ports, you need to ensure the port range between 10000 and 20000 is open because Azure Cosmos DB uses dynamic TCP ports. If these ports are not open and you attempt to use TCP, you receive a 503 Service Unavailable error. The following table shows connectivity modes available for different APIs and the service ports user for each API:
41
+
When using gateway mode, Cosmos DB uses port 443 and ports 10250, 10255 and 10256 when using Azure Cosmos DB's API for MongoDB. The 10250 port maps to a default MongoDB instance without geo-replication and 10255/10256 ports map to the MongoDB instance with geo-replication functionality. When using TCP in Direct Mode, in addition to the Gateway ports, you need to ensure the port range between 10000 and 20000 is open because Azure Cosmos DB uses dynamic TCP ports. If these ports are not open and you attempt to use TCP, you receive a 503 Service Unavailable error. The following table shows connectivity modes available for different APIs and the service ports user for each API:
43
42
44
43
|Connection mode |Supported protocol |Supported SDKs |API/Service port |
45
44
|---------|---------|---------|---------|
@@ -49,7 +48,20 @@ So if you're asking "How can I improve my database performance?" consider the fo
49
48
50
49
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.
51
50
52
-
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 while creating the CosmosClient instance, as part of the CosmosClientOptions.
>OpenAsyncmethodwillgeneraterequeststoobtaintheaddressroutingtablefor all the containers in the account. For accounts that have many containers but their application accesses a subset of them, it would generate an unnecessary amount of traffic that makes the initialization slow. So using OpenAsync method might not be useful in this scenario as it slows down application startup.
89
+
74
90
<a id="same-region"></a>
75
91
3. **Collocate clients in same Azure region for performance**
76
92
@@ -91,15 +107,22 @@ So if you're asking "How can I improve my database performance?" consider the fo
EachDocumentClientinstanceisthread-safeandperformsefficientconnectionmanagementandaddresscaching when operating in Direct Mode. 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.
111
+
2. **UseStreamAPIs**
112
+
113
+
The [.NetSDKV3](sql-api-sdk-dotnet-standard.md) containsstreamAPIsthatcanreceiveandreturndatawithoutserializing.
114
+
115
+
Themiddle-tierapplicationsthatdon't consume the responses from the SDK directly but relay them to other application tiers can benefit from the stream APIs. See the [Item management](https://github.com/Azure/azure-cosmos-dotnet-v3/blob/master/Microsoft.Azure.Cosmos.Samples/Usage/ItemManagement) samples for examples on stream handling.
EachDocumentClientandCosmosClientinstanceisthread-safeandperformsefficientconnectionmanagementandaddresscaching 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.
AzureCosmosDBrequestsaremadeoverHTTPS/RESTwhenusingGatewaymode, andaresubjectedtothedefaultconnectionlimitperhostnameorIPaddress. YoumayneedtosettheMaxConnectionstoahighervalue (100-1000) sothattheclientlibrarycanutilizemultiplesimultaneousconnectionstoAzureCosmosDB. Inthe .NETSDK1.8.0andabove, thedefaultvaluefor [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.
SQL .NETSDKversion1.9.0andabovesupportparallelqueries, whichenableyoutoqueryapartitionedcollectioninparallel. Formoreinformation, see [codesamples](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.
105
128
@@ -112,10 +135,10 @@ So if you're asking "How can I improve my database performance?" consider the fo
@@ -142,19 +165,19 @@ So if you're asking "How can I improve my database performance?" consider the fo
142
165
> [!NOTE]
143
166
>ThemaxItemCountpropertyshouldn't be used just for pagination purpose. It'smainusageittoimprovetheperformanceofqueriesbyreducingthemaximumnumberofitemsreturnedinasinglepage.
144
167
145
-
YoucanalsosetthepagesizeusingtheavailableAzureCosmosDBSDKs. The [MaxItemCount](/dotnet/api/microsoft.azure.documents.client.feedoptions.maxitemcount?view=azure-dotnet) propertyinFeedOptionsallowsyoutosetthemaximumnumberofitemstobereturnedintheenmurationoperation. When `maxItemCount` issetto -1, theSDKautomaticallyfindsthemostoptimalvaluedependingonthedocumentsize. Forexample:
168
+
YoucanalsosetthepagesizeusingtheavailableAzureCosmosDBSDKs. The [MaxItemCount](/dotnet/api/microsoft.azure.documents.client.feedoptions.maxitemcount?view=azure-dotnet) propertyinFeedOptionsallowsyoutosetthemaximumnumberofitemstobereturnedintheenumerationoperation. When `maxItemCount` issetto -1, theSDKautomaticallyfindsthemostoptimalvaluedependingonthedocumentsize. Forexample:
146
169
147
170
```csharp
148
171
IQueryable<dynamic>authorResults=client.CreateDocumentQuery(documentCollection.SelfLink, "SELECT p.Author FROM Pages p WHERE p.Title = 'About Seattle'", newFeedOptions { MaxItemCount=1000 });
149
172
```
150
173
151
174
Whenaqueryisexecuted, theresultingdataissentwithinaTCPpacket. Ifyouspecifytoolowvaluefor `maxItemCount`, thenumberoftripsrequiredtosendthedatawithintheTCPpacketarehigh, whichimpactstheperformance. Soifyouarenotsurewhatvaluetosetfor `maxItemCount` property, it's best to set it to -1 and let the SDK choose the default value.
152
175
153
-
10. **Increasenumberofthreads/tasks**
176
+
11. **Increasenumberofthreads/tasks**
154
177
155
178
See [Increasenumberofthreads/tasks](#increase-threads) intheNetworkingsection.
156
179
157
-
11. **Use64-bithostprocessing**
180
+
12. **Use64-bithostprocessing**
158
181
159
182
TheSQLSDKworksina32-bithostprocesswhenyouareusingSQL .NETSDKversion1.11.4andabove. However, ifyouareusingcrosspartitionqueries, 64-bithostprocessingisrecommendedforimprovedperformance. Thefollowingtypesofapplicationshave 32-bithostprocessasthedefault, soinordertochangethatto64-bit, followthesestepsbasedonthetypeofyourapplication:
0 commit comments