Availability and best practices for the Azure Cosmos DB .NET SDK - Episode 13

Azure Cosmos DB · 4K views · 2021 · 1:39:43

Open on YouTube

Cloud, DevOps & reliabilityDatabases & storageDistributed systemsinterviewdated

Why Stack Overflow recommends it

Stack Overflow users link it when explaining why the first Cosmos DB request is slow, how Direct mode uses Gateway for metadata and TCP for data, and why to keep a singleton client.

Recommended by 2 people across 2 different months, 2021 to 2022.

What it is

A 1h39m episode of the Azure Cosmos DB show where host Mark talks with Matias Quaranta, an SDK engineer, about the .NET SDK. It covers Gateway versus Direct connectivity modes, why the first request is slow, best practices for high availability, and how the SDK handles failover and regional outages.

What people said

Recommends Gaurav Mantri · 2021-06 · Stack Overflow · on “Asp.net core 3 application slow to load cosmos db query”

First, I would highly recommend that you (or anyone using Cosmos DB .Net SDK) to watch this video on Cosmos DB Youtube Channel: [this video] . This provides really useful information about the best practices to follow when working with this SDK. This video will explain why the first request takes so much time and how you can speed that up. To summarize for the purpose of this answer, creating an instance of Cosmos Client (with "Direct" connection mode) does not do much. When you make the 1st request with that client, the initialization happens and at that time SDK makes a few network requests to get necessary information about establishing "Direct" (TCP) connection. That's why it takes a great de

Recommends Matias Quaranta · 2021-12 · Stack Overflow · on “What data goes through Gateway connection to CosmosDB in Direct mode?”

This is explained in detail on: [this video] The SDK in Direct mode requires doing HTTP calls to Gateway to obtain: Account information (which regions are available, what is the consistency configuration, etc) Container information (what's the partition key definition?) Routing information (What are the TCP addresses to connect to for data plane operations) Any metadata resource operations (CRUD over Databases/Containers) These are all considered metadata/management plane operations, and go through Gateway. Data plane operations (CRUD/Query over Items/documents) are done through TCP.

Cites Matias Quaranta · 2021-09 · Stack Overflow · on “Azure Functions - Java CosmosClientBuilder slow on initial connection”

This is a expected behavior, see [this video] . The first request a client does needs to go through a warm-up step. This warm-up consists of fetching the account information, container information, routing and partitioning information in order to know where to route the requests (as you experienced, further requests do not get this extra latency). Hence the importance of maintaining a singleton instance. In some Functions plan (Consumption) instances get de-provisioned if there is no activity, in which case, any existing instance of the client is destroyed, so when a new instance is provisioned, your first request will pay this warm-up cost. There are currently no workaround I'm aware of in the J

Cites Madhuraj Vadde · 2022-03 · Stack Overflow · on “Azure Cosmos DB Connection Slow”

Thank you Gaurav Mantri [LINK] , David Makogon [LINK] and Mark Brown [LINK] . Posting your suggestions as answer to help other community members. Practices that you can adopt to make the connection faster Initialize the cosmos connection at startup. you would need to use CreateAndInitializeAsync [LINK] method of the CosmosClient. Make sure to run the code in the same region as that of cosmos DB. Always have the reference to cosmos client and container alive. This will ensure the subsequent calls (after 1st call) will be faster. Reference: Why the first request takes so much time and how you can speed that up. [this video] [LINK] )

Cites David Klempfner · 2022-11 · Stack Overflow · on “How to use CosmosClient.CreateAndInitializeAsync() with CosmosClientBuilder.Build()”

This YouTube video [this video] @27:20 talks about populating the cache with routing info to avoid latency during a cold start. You can either try to get a document you know doesn't exist, or you can use CosmosClient.CreateAndInitializeAsync() . I already have this code set up: private async Task<Container> CreateContainerAsync(string endpoint, string authKey) { var cosmosClientBuilder = new CosmosClientBuilder( accountEndpoint: endpoint, authKeyOrResourceToken: authKey) .WithConnectionModeDirect(portReuseMode: PortReuseMode.PrivatePortPool, idleTcpConnectionTimeout: TimeSpan.FromHours(1)) .WithApplicationName(UserAgentSuffix) .WithConsistencyLevel(ConsistencyLevel.Session) .WithApplicationRegion