【问题标题】:MongoDB C# Driver Is Slow On First Connection InitializeMongoDB C# 驱动程序在第一次连接初始化时很慢
【发布时间】:2021-03-24 19:17:46
【问题描述】:

我们有一个在 IIS 上运行的应用程序,使用 mongodb c# 驱动程序,(2.11.5.0 版本),当它打开与副本集的新连接时,大约需要 500 毫秒(包括数据库上市操作),

在相同的环境和相同的机器上,我用 NodeJS 应用程序尝试了相同的情况,它建立了连接并在 62 毫秒 内列出了数据库。我也尝试过使用 python 代码,它需要 12 毫秒,包括从集合中获取记录。 (每次我控制这些持续时间时,我都会关闭所有与 mongodb 的连接并首先停止这些应用程序)

在我们的环境中,第一个连接初始化缓慢是个问题,因为负载均衡器后面有服务器,如果没有足够的请求到达所有服务器并保持连接打开,即使我将空闲超时设置为更长的时间(如 20 分钟) ,请求可以被转发到空闲的服务器,500 毫秒的额外持续时间是事务的成本。在我们的例子中,事务响应时间应该小于 250 毫秒。

NodeJS 代码:

const {MongoClient} = require('mongodb');
async function listDatabases(client){
    databasesList = await client.db().admin().listDatabases();
};
async function main(){
 
    const uri = "mongodb://dbUserName:dbPassword@SERVER/DBName?retryWrites=true&w=majority&ssl=false&replicaSet=replicaName&authSource=admin";
    
    console.time("listDatabaseNamesDuration");

    const client = new MongoClient(uri,{ useNewUrlParser: true, useUnifiedTopology: true});
 
    try {
    
        await client.connect();
 
        await  listDatabases(client);

        console.timeEnd("listDatabaseNamesDuration");

    } catch (e) {
        console.error(e);
    } finally {
        await client.close();
    }
}
main();

C#代码:

       var credential = MongoCredential.CreateCredential(databaseName: "admin",
       username: "dbUserName",
       password: "dbPassword");
            IList<MongoServerAddress> serverList = new List<MongoServerAddress>();
            serverList.Add(new MongoServerAddress("serverName", portNumber));
            MongoClientSettings settings = new MongoClientSettings
            {
                Servers = serverList,
                ConnectionMode = ConnectionMode.ReplicaSet,
                ReplicaSetName = "replicasetName",
                Credential = credential,
                UseTls = false,
            };

            Stopwatch sw = Stopwatch.StartNew();
            MongoClient _client = new MongoClient(settings);
            _client.ListDatabaseNames();
            sw.Stop();
            output.ListDatabaseNamesDuration = sw.ElapsedMilliseconds;

【问题讨论】:

    标签: c# mongodb performance connection


    【解决方案1】:

    首先尝试缓存或将MongoClient 设为单例。如果这没有帮助,请尝试将 tcp 连接配置到KeepAlive,如下所示:

    var settings = new MongoClientSettings
    {
        Servers = serverList,
        ReplicaSetName = "replicasetName",
        Credential = credential,
        UseTls = false,
    };
    
    settings.ClusterConfigurator =
        b => b.ConfigureTcp(
            tcp => tcp.With(
                socketConfigurator:
                    (Action<Socket>)(
                        s => s.SetSocketOption(
                            SocketOptionLevel.Socket,
                            SocketOptionName.KeepAlive,
                            true))));
    
    var client = new MongoClient(settings);
    

    最好将 MongoClient 设为单例,因为这是 mongo 文档中的建议。

    【讨论】:

      猜你喜欢
      • 2013-11-29
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 2014-08-04
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多