MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Incorrect user/pass rejection takes a long time from node/express/mongodb

I’m using an express API in node for mongodb access. My auth route works fine when the user/pass is correct. But when the user/pass is incorrect, the mongod instance issues this message 60 times:

2020-11-12T14:52:31.769-0500 I NETWORK  [conn181] received client metadata from 127.0.0.1:49487 conn181: { driver: { name: "nodejs", version: "3.5.7" }, os: { type: "Darwin", name: "darwin", architecture: "x64", version: "19.6.0" }, platform: "'Node.js v11.0.0, LE (unified)" }
2020-11-12T14:52:31.770-0500 I ACCESS   [conn181] SASL SCRAM-SHA-1 authentication failed for badUser on test_db from client 127.0.0.1:49487 ; UserNotFound: Could not find user badUser@test_db
2020-11-12T14:52:31.770-0500 I NETWORK  [conn181] end connection 127.0.0.1:49487 (0 connections now open)

…then my node/express API finally returns

Authentication failed MongoServerSelectionError: Authentication failed.

This is correct, but takes about 20 seconds to complete. Therefore the user’s time is wasted waiting for the ‘incorrect user/pass’ message.

Here is the connect code in node:

var auth = { user:encodeURIComponent(req.body.user), password:encodeURIComponent(req.body.pass) }
var clientOptions = { authSource:dbName, auth:auth, authMechanism:'DEFAULT', useUnifiedTopology:true}
client = new MongoClient(process.env.MONGO_SERVER_URI, clientOptions)

client.connect((err, client) => {
  if (err) {
    res.status(401).send(JSON.stringify({err:`Authentication failed`}))
    return
  } 
}

Why is mongodb (or MongoClient) taking so long to reject the login?

http://mongodb.github.io/node-mongodb-native/3.6/api/MongoClient.html#.connect

reconnectTries number 30 optional Server attempt to reconnect #times

reconnectTries not allowed when useUnifiedTopology:true

so I removed useUnifiedTopology:true and bad logins are rejected immediately. Any idea what is going on?

1 Like

Looks like Unified Topology is still a work in progress.

The only hopeful suggestion I’ve found in 1/2 hour of Googling is serverSelectionTimeoutMS (which is allowed with UT) which may improve your UX.