{"id":8444,"date":"2024-08-12T06:34:06","date_gmt":"2024-08-12T13:34:06","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=8444"},"modified":"2024-08-12T06:34:06","modified_gmt":"2024-08-12T13:34:06","slug":"new-sdk-options-for-fine-grained-request-routing-to-azure-cosmos-db","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/new-sdk-options-for-fine-grained-request-routing-to-azure-cosmos-db\/","title":{"rendered":"New SDK Options for Fine-Grained Request Routing to Azure Cosmos DB"},"content":{"rendered":"<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/new_fine_grain.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-8456\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/new_fine_grain.png\" alt=\"New SDK Options for Fine-Grained Request Routing to Azure Cosmos DB\" width=\"960\" height=\"540\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/new_fine_grain.png 960w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/new_fine_grain-300x169.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/new_fine_grain-768x432.png 768w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p>The latest updates to the Azure Cosmos DB .NET SDK bring a powerful new feature with the release of version 3.37.0: the <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> request option. This enhancement allows developers to exclude specific regions from their preferred locations when making a request, enabling more precise and efficient request routing. Similarly, the Azure Cosmos DB Java SDK has also incorporated this feature in its version 4.47.0. With these updates, users can now exercise finer control over their data access patterns, ensuring optimal performance and reliability for their applications.<\/p>\n<h2>What is ExcludeRegions?<\/h2>\n<p><span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> allows you to have more control over which region your requests are routed to when using Azure Cosmos DB in your applications. By specifying a list of the regions you would like to exclude in the <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> property of the\u00a0<span style=\"font-family: terminal, monaco, monospace;\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/microsoft.azure.cosmos.requestoptions?view=azure-dotnet\" target=\"_blank\" rel=\"noopener\">RequestOptions\u00a0<\/a><\/span>objects, the SDK will ensure that the request, including any cross regional retries, are not routed to any regions in that list. If all regions are included in the list, then the request will be routed to the primary\/hub region.<\/p>\n<p>Currently, the SDK has the <span style=\"font-family: terminal, monaco, monospace;\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/nosql\/tutorial-global-distribution?tabs=dotnetv3%2Capi-async#net-sdk\" target=\"_blank\" rel=\"noopener\">ApplicationPreferredRegions<\/a> <\/span>list in the <span style=\"font-family: terminal, monaco, monospace;\">CosmosClientOptions<\/span>. This allows you to specify an ordered regions list where SDK will only attempt to perform operations from the regions specified in preferred locations in order, failing over to the next valid region in the list if the request fails. The <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> option allows you to bypass the order of this list on a per-request level and should be used in conjunction to <span style=\"font-family: terminal, monaco, monospace;\">PreferredLocations<\/span> to help route your requests. In the example below, we show how to create a <span style=\"font-family: terminal, monaco, monospace;\">CosmosClient<\/span> instance that uses <span style=\"font-family: terminal, monaco, monospace;\">ApplicationPreferredRegions<\/span> and how to bypass that list using the <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> option:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">CosmosClientOptions clientOptions = new CosmosClientOPtions()\r\n{\r\n  ApplicationPreferredRegions = new List&lt;string&gt; {\"West US\", \"Central US\", \"East US\"};\r\n}\r\n\r\nCosmosClient client = new CosmosClient(connectionString, clientOptions);\r\n\r\nDatabase db = client.GetDatabase(\"myDb\");\r\nContainer container = db.GetContainer(\"myContainer\");\r\n\r\n\/\/Request will be served out of the West US region\r\nawait container.ReadItemAsync&lt;dynamic&gt;(\"item\", new PartitionKey(\"pk\"))\r\n\r\n\/\/By using ExcludeRegions, we are able to bypass the ApplicationPreferredRegions list\r\n\/\/ and route a request directly to the East US region\r\nawait container.ReadItemAsync&lt;dynamic&gt;(\r\n  \"item\", \r\n  new PartitionKey(\"pk\"),\r\n  new ItemRequestOptions()\r\n  {\r\n    ExcludeRegions = new List&lt;string&gt;() { \"West US\", \"Central US\" }\r\n  });<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>Use Cases<\/h2>\n<p>In the examples below assume we have an Azure Cosmos DB account with three regions set up: East US, Central US, and West US.<\/p>\n<p><img decoding=\"async\" width=\"915\" height=\"707\" class=\"wp-image-8448\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/a-screenshot-of-a-computer-description-automatica.png\" alt=\"A screenshot of a computer Description automatically generated\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/a-screenshot-of-a-computer-description-automatica.png 915w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/a-screenshot-of-a-computer-description-automatica-300x232.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2024\/07\/a-screenshot-of-a-computer-description-automatica-768x593.png 768w\" sizes=\"(max-width: 915px) 100vw, 915px\" \/><\/p>\n<p>First, <a id=\"post-8444-_Int_SQKgbwPx\"><\/a>let\u2019s say our application is receiving Status Code 429 responses from the backend, indicating we have exhausted all our <a id=\"post-8444-_Int_yt9RjQEN\"><\/a>RUs. When running into <a id=\"post-8444-_Int_o3bVwYSc\"><\/a>429s, if we still want the request to succeed, we can use <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> to ensure that the request is served out of a secondary region where there are still RUs provisioned. Today, to do this, you will need a secondary client with separate preferred regions configured. With ExcludeRegions, the process is quite simple:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">ItemResponse&lt;CosmosItem&gt; item;\r\n\r\nitem = await container.ReadItemAsync&lt;CosmosItem&gt;(\"id\", partitionKey);\r\n\r\nif (item.StatusCode == StatusCodes.TooManyRequests)\r\n{\r\n    ItemRequestOptions requestOptions = new RequestOptions()\r\n    {\r\n        ExcludeRegions = new List&lt;string&gt;() { \"East US\" }\r\n    };\r\n\r\n    item = await container.ReadItemAsync&lt;CosmosItem&gt;(\"id\", partitionKey, requestOptions);\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>This eliminates the need for a using secondary client and enables us to adhere <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/cosmos-db\/nosql\/best-practice-dotnet\" target=\"_blank\" rel=\"noopener\">to Azure Cosmos DB .NET SDK best practices<\/a> by maintaining a singleton client instance. It additionally reduces the number of <a id=\"post-8444-_Int_GdnLi3qL\"><\/a>HTTP connections now that there is a single client. And because this works with all variations of request options the <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> can be used for queries like in the example below:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">QueryRequestOptions queryRequestOptions = new RequestOptions()\r\n{\r\n    ExcludeRegions = new List&lt;string&gt;() { \"East US\" }\r\n};\r\n\r\nusing (FeedIterator&lt;CosmosItem&gt; queryFeedIterator = await container.GetItemQueryIterator&lt;CosmosItem&gt;(\r\n    queryDefinition, \r\n    queryRequestOptions))\r\n{\r\n    while(queryFeedIterator.HasMoreResults)\r\n    {\r\n        var item = await feedIterator.ReadNextAsync();\r\n    }\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>Another use case for <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> is to ensure a request gets routed to a specific region. Suppose you need to guarantee that your requests are processed from a specific region, such as the West US. By including all your account&#8217;s regions except West US in the <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> list, the SDK guarantees that requests are solely served there. To route a request to a specific region using the <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> list, you can make a request like this:<\/p>\n<pre class=\"prettyprint language-cs language-csharp\"><code class=\"language-cs language-csharp\">ItemRequestOptions requestOptions = new RequestOptions()\r\n{\r\n    ExcludeRegions = new List&lt;string&gt;() { \"East US\", \"Central US\" }\r\n};\r\n\r\nItemResponse&lt;CosmosItem&gt; item = await container.ReadItemAsync&lt;CosmosItem&gt;(\"id\", partitionKey, requestOptions);<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>Next Steps<\/h2>\n<p>In all, the new <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> request option is a powerful tool that allows you to have fine grain control over how your requests are routed. You can try out <span style=\"font-family: terminal, monaco, monospace;\">ExcludeRegions<\/span> by updating your SDK to version 3.37.0 or above. Please share any feedback on <a id=\"post-8444-_Int_VaR564fq\"><\/a>our <a href=\"https:\/\/github.com\/Azure\/azure-cosmos-dotnet-v3\/issues\" target=\"_blank\" rel=\"noopener\">official GitHub repository.<\/a><\/p>\n<h2>About Azure Cosmos DB<\/h2>\n<p>Azure Cosmos DB is a fully managed and serverless distributed database for modern app development, with SLA-backed speed and availability, automatic and instant scalability, and support for open-source PostgreSQL, MongoDB, and Apache Cassandra. <a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\">Try Azure Cosmos DB for free here.<\/a> To stay in the loop on Azure Cosmos DB updates, follow us on <a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\">X<\/a>, <a href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>, and <a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\">LinkedIn<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The latest updates to the Azure Cosmos DB .NET SDK bring a powerful new feature with the release of version 3.37.0: the ExcludeRegions request option. This enhancement allows developers to exclude specific regions from their preferred locations when making a request, enabling more precise and efficient request routing. Similarly, the Azure Cosmos DB Java SDK [&hellip;]<\/p>\n","protected":false},"author":164043,"featured_media":8456,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-8444","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-sql-api"],"acf":[],"blog_post_summary":"<p>The latest updates to the Azure Cosmos DB .NET SDK bring a powerful new feature with the release of version 3.37.0: the ExcludeRegions request option. This enhancement allows developers to exclude specific regions from their preferred locations when making a request, enabling more precise and efficient request routing. Similarly, the Azure Cosmos DB Java SDK [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/8444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/164043"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=8444"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/8444\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/8456"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=8444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=8444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=8444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}