DEV Community

Discussion on: A crash course on Serverless with AWS - Building APIs with Lambda and Aurora Serverless

Collapse
 
hzburki profile image
Haseeb Burki • Edited

Hey Adnan. Can you please explain a little how database connections in serverless framework work? More specifically how many database connections would your code create if you call the same endpoint multiple time and if you call the same endpoint from two different source simultaneously.

I have a serverless project integrated with express and sequelize, such that a single lambda has multiple routes. The F.E calls 6 api end points for different graphs simultaneously. My code create 6 connections instead of one, and the CPU utilization on my RDS instance goes up to 60-80% :| ...

Collapse
 
adnanrahic profile image
Adnan Rahić

This is a cool question. The keyword here is simultaneously. This has nothing to do with the Serverless Framework, and everything to do with the way AWS Lambda works under the hood.

When your front end sends 6 concurrent requests to a Lambda function, it will spawn 6 instances and every instance will handle one request. If you were to send 6 requests sequentially, one instance would be enough.

Because, in this case, you have 6 Lambda function instances, every one of those will create a database connection. Makes sense?

The way to handle this is to have one Lambda function dedicated to only database interaction, while the other act as proxies. This is very hard to configure. I'd suggest you use a serverless database that can scale as easily as Lambda. :)

Very cool question, feel free to hit me up through the chat here in dev.to if you have any more topics like these. I'd love to nerd out.