Codementor Events

How to solve database pooling issue in AWS Lambda function

Published May 04, 2019

People alway rave about how good serverless is but avoid a key problem which is how to handle many instances of lambda.

Usually handling many request at once is not an issue but your lambda usually integrate with many services(ie. database, SNS, Email service, etc.).

SNS, Email service can handle many request without an issue.

On the other hand, your database can't.

Usually in a normal application, we have database connection pooling. This allows your application to receives 100+ request and process queries with less than 100 connections to the database.

While in lambda, when you receive 100+ request, the request are so fast they spin up many lambda to handle the requests.

Worse case, it sends 100 database connection request and it denies all request pass the maximum.

I've spend countless hours going through stackoverflow and no solution existed.

What I ended up doing is the following:

  1. before the handler code, I created an IOC container with a connection to the pgbounce server.

2)I setup pgbounce to connect to my database.

3)I increased the maximum connections on both pgbounce and the database.

Now when 100+ lambda connects to pgbounce, it handles the database pooling for me and all requests are handled.

Now you have a bottleneck in pgbounce but this is easier to solve than forcing lambda to use the same instance.

If you want to talk more about this, you can book a time with me at http://manaknightdigital.com/codementor.html

Discover and read more posts from Ryan Wong
get started
post commentsBe the first to share your opinion
Jonathan Eunice
5 years ago

“You don’t eliminate bottlenecks, you just move them somewhere else” is a bedrock truism of optimizing scalability and performance. In this case, Lambda moved your bottleneck to the DBMS. You fixed that with your connection pooling. Now, on to the next!

Show more replies