Search code examples
mongodbperformancegomongodb-querymongo-go

Why is a simple query taking more than 2 seconds with Golang Mongo driver?


I am coding a golang web service that has a mongo database, I am using the go.mongodb.org/mongo-driver v1.11.6 and a simple query is taking more than 2 seconds to complete. The database has only a few records, is it just for testing, no more than 10 records.

I've been looking for the code part where the time is being wasted and I found that the problem is with the mongo package. The methods Find, FindOne and even Insert and Update are taking more than 1 or 2 seconds to complete.

This is the mongo client instantiation

func NewMongoDB() *MongoDB {
    uri := config.GetEnvConfig().MongoURI
    database := config.GetEnvConfig().MongoDatabase

    client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))

    if err != nil {
    panic(err)
    }

    return &MongoDB{
    client:   client,
    database: database,
    }
}

This is the function code:

func getMessageByIdFromDB(id string) (*Message, error) {
    conn := database.NewMongoDB()
    defer conn.Disconnect()

    filter := map[string]string{
    "message_id": id,
    }

    var message Message

    start := time.Now()

    err := conn.GetCollection(collectionName).FindOne(context.TODO(), filter).Decode(&message)

    elapsed := time.Since(start)
    log.Printf("Querying messages took %s", elapsed)

    if err != nil {
    return nil, err
    }

    return &message, nil
}

This is the result of the time tracking of the function:

Querying messages took 2.320409472s

It is not about internet connection because I made the query test with python and the request took only 0.003 seconds

I tried to change the version of the package without achieving it. Also I tried to reinstall all the packages of the project, with the same result.

I also tried to create search indexes in database with no different results. The query also take more than 2 seconds to complete.

I think the query shouldn't take more than a few milliseconds to complete.


Solution

  • mongo.Connect() "only" initializes the Client by starting background monitoring goroutines. It may not necessary connect to the (remote) database.

    When you then perform queries, they certainly require to build up connections, and that may take seconds.

    You may use the Client.Ping() method prior to force connecting to the database and verify that the connection was created successfully, so when you perform queries after that, a connection will be ready.

    You may also try to repeat the same query, as after the first query the connection will not be closed but put into a connection pool and reused when needed again (for the second query).