Best practices for connection pooling for azure cosmosdb Mongodb API

KS 1 Reputation point
2020-11-05T04:41:00.167+00:00

What are standard industry practices to implement connection pooling for azure cosmosdb Mongodb API?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,902 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anurag Sharma 17,631 Reputation points
    2020-11-05T10:03:31.22+00:00

    Hi @KS , welcome to Microsoft Q&A forum.

    Choosing best practices for Connection Pooling in general involve multiple parameters. We already know creating new connection every time is an expensive operation as it involves opening the connection, doing some work, closing it, that would definitely result in connection latency which would again impact the throughput i.e. RUs for Azure Cosmos Mongo DB.

    When it comes to Azure Cosmos DB, I would recommend you to read through the excerpts from the article. (For mongodb we need to replace the DocumentClient with MongoClient but underlying concept would be almost similar)

    Use a singleton Azure Cosmos DB client for the lifetime of your application:
    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 SDK client performance, we recommend that you use a single instance per AppDomain for the lifetime of the application.

    Increase System.Net MaxConnections:
    Azure Cosmos DB requests are made over HTTPS/REST when you use gateway mode. They're subjected to the default connection limit per hostname or IP address. You might need to set MaxConnections to a higher value (100 to 1,000) so the client library can use multiple simultaneous connections to Azure Cosmos DB.

    Also we would always need to be careful about number of max connection as we don't want them to be there idle for long time as they eat up server resources as well. We need to set the timeout property on them accordingly.

    Below is an example of .NET SDK for Azure Cosmos DB MongoDB API and MongoClientSettings class details that offers all the properties that we need to set up connection pool:
    https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/cosmos-db/create-mongodb-dotnet.md

    https://mongodb.github.io/mongo-csharp-driver/2.7/apidocs/html/T_MongoDB_Driver_MongoClientSettings.htm

    Please let me know if that helps.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.