Skip to content

Commit 973688b

Browse files
committed
V3 policies
1 parent 4ceec66 commit 973688b

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

articles/cosmos-db/performance-tips.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@ So if you're asking "How can I improve my database performance?" consider the fo
2828

2929
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:
3030

31-
* Gateway Mode (default)
31+
* Gateway Mode
3232

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 [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.
3434

3535
* Direct Mode
3636

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:
38-
39-
* TCP
40-
* HTTPS
37+
Direct mode supports connectivity through TCP and HTTPS protocols and is the configured default for [SDK V3](sql-api-sdk-dotnet-standard.md). If you are using the latest version of .NET SDK, direct connectivity mode is supported in .NET Standard 2.0 and .NET framework.
4138

4239
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:
4340

@@ -49,7 +46,7 @@ So if you're asking "How can I improve my database performance?" consider the fo
4946

5047
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.
5148

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.
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.
5350

5451
```csharp
5552
var serviceEndpoint = new Uri("https://contoso.documents.net");
@@ -62,15 +59,32 @@ So if you're asking "How can I improve my database performance?" consider the fo
6259
});
6360
```
6461

62+
For SDK V3, the Connectivity Mode is configured during the construction of the CosmosClient instance, as part of the CosmosClientOptions.
63+
64+
```csharp
65+
var serviceEndpoint = new Uri("https://contoso.documents.net");
66+
var authKey = "your authKey from the Azure portal";
67+
CosmosClient client = new CosmosClient(serviceEndpoint, authKey,
68+
new CosmosClientOptions
69+
{
70+
ConnectionMode = ConnectionMode.Direct,
71+
ConnectionProtocol = Protocol.Tcp
72+
});
73+
```
74+
6575
Because TCP is only supported in Direct Mode, if Gateway Mode is used, then the HTTPS protocol is always used to communicate with the Gateway and the Protocol value in the ConnectionPolicy is ignored.
6676

6777
![Illustration of the Azure Cosmos DB connection policy](./media/performance-tips/connection-policy.png)
6878

6979
2. **Call OpenAsync to avoid startup latency on first request**
7080

71-
By default, the first request has a higher latency because it has to fetch the address routing table. To avoid this startup latency on the first request, you should call OpenAsync() once during initialization as follows.
81+
By default, the first request has a higher latency because it has to fetch the address routing table. When using the [SDK V2](sql-api-sdk-dotnet.md), to avoid this startup latency on the first request, you should call OpenAsync() once during initialization as follows.
7282

7383
await client.OpenAsync();
84+
85+
> [!NOTE]
86+
> OpenAsync will generate requests to obtain the address routing table for all the containers in the account. For accounts that have many containers but their application is accessing only a very small subset of them,
87+
7488
<a id="same-region"></a>
7589
3. **Collocate clients in same Azure region for performance**
7690

@@ -93,7 +107,7 @@ So if you're asking "How can I improve my database performance?" consider the fo
93107
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.
94108
2. **Use a singleton Azure Cosmos DB client for the lifetime of your application**
95109

96-
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 DocumentClient, it is recommended to use a single instance of DocumentClient per AppDomain for the lifetime of the application.
110+
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.
97111

98112
<a id="max-connection"></a>
99113
3. **Increase System.Net MaxConnections per host when using Gateway mode**

0 commit comments

Comments
 (0)