5

I am working with some AWS Lambda functions running inside VPC that needs to connect to a backend Mongo Atlas database for data storage/retrieval.

I am coding with Javascript, so i naturally used the official MongoDB Node JS client to establish the connection. The issue i am facing is that whenever i made a few concurrent lambda invocations, a few of them are unable to successfully establish a connection and basically hangs until Lambda timeout. For example, if 5 different functions were to be invoked, 2 might hang at connection attempt until Lamdba timeout. The connection is saved outside of the handler function for reuse in subsequent function invocation and the code is based on best practices specified here: https://docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/

I have read through all the online resources and mongo documentations available as well but i am unable to find any solution. Has anyone ever encountered such issue with Lambda in VPC connection to Mongo and getting timeout? How did you resolve it? Thanks.

3
  • Have you considered setting poolSize on your connect() call, dependent on your typical Lambda concurrency? Commented Jun 19, 2019 at 14:47
  • I haven tried that yet but how will that param affect this issue? Commented Jun 20, 2019 at 4:55
  • You can read about connection pooling on the web, but it’s a standard technique for providing multiple concurrent clients with access to a DB. Commented Jun 20, 2019 at 11:31

1 Answer 1

2

I was having exactly the same issue.

Resolved it by explicitly NOT following the best-practices, and instead opening (and closing) a new connection for every lambda request that requires one.

In my setup, the connect() method takes between 20 and 70 milliseconds to complete, which I suppose I will more eagerly suffer than having intermittent and unreproducible hanging connections.

For good measure, I added { poolSize: 1 } to the MongoClient connection opts.

Sign up to request clarification or add additional context in comments.

1 Comment

The mongodb node.js client was also a partial culprit, as it sometimes left connections open even though close() was explicitly called on the client. Upgrading from 3.3.2 to 3.3.5 fixed this issue for us.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.