3

I am using AWS-lambda handler to write a dynamoDB stream to mongoDB. I want to define the db connection outside of the handler so that the requests reuse the same connections, based on our expected throughput volume. The problem with the lambda containers will expire without disposing of the connections properly.

Does anybody know of any good solutions for this problem? Essentially boils down to "I want to use connection pools without maxing out the connection limit"

yo conway
  • 207
  • 1
  • 5
  • Have you measured how expensive connection creation is? Are you positive that the connections are not closed down when the container expires? There isn't a "destructor" in Lambda but I would expect the connections to be shut down when the container goes away. I also question how expensive the connection really is. – stdunbar Jun 04 '19 at 00:57

1 Answers1

0

When I faced with this problem, I've found out two solutions:

  1. "Best practice" - create/close MongoDB connection for each Lambda invocation. Good idea if you know that Lambdas will be invoked not so often => your Lambdas containers would shout down.
  2. Reuse connection pull - for me, it's normal when you know that you Lambda will invoked enough usually to keep (potentially) container warm. In this case you should set socketTimeoutMS option (mongoose) enough to keep it between Lambdas invocation. For me it's :
 { 
   reconnectTries: 30, 
   reconnectInterval: 500, 
   poolSize: 1, 
   socketTimeoutMS: 2000000, 
   keepAlive: true, 
 }

(connection will be closed automatically after timeout)

** FYI: Any warm-up may be not suitable for you, if you use DynamoDB streams as trigger for lambda.

** I've already asked similar question here: AWS Lambda (Node.js, v. 8.10) & Mongoose: MongoNetworkError connection to DB timed out

Max Vinogradov
  • 1,343
  • 1
  • 13
  • 31