I’ve recently noticed some odd behavior while testing the performance of writing or reading to a serveless cluster.
I don’t think is related to the serverless option, but as a context I’m using a new Serverless cluster in AWS (Ireland region) which I interact with from my local machine.
@lewislbr thanks for the question! I think the difference in duration between the two scenarios comes from the extra time it takes to establish a new connection to the Serverless cluster for the first operation.
After creating a new mongo.Client, the first operation has to establish a new connection to a server. When you run a Ping before running InsertOne, a new connection is established to run the Ping and reused to run the InsertOne. When you run an InsertOne without running a Ping, a new connection has to be established to run the InsertOne.
If you run your InsertOne operation in a loop, you should see the first operation take longer because it has to establish a new connection and the subsequent operations don’t take as long because they can reuse that connection.
E.g. loop code:
for i := 0; i < 10; i++ {
start := time.Now()
_, err = collection.InsertOne(ctx, item)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Item added in", time.Since(start))
}