I’m developing some functions with serverless with the nodejs template. I have a service that connects to a mysql database and retrieves some data. Everything is fine when I make the first call, but when I repeat it I receive an error because the connection was closed.
I managed to get it working creating the connection in each of the handlers, but I don’t like this solution for several reasons:
- It will not give a good performance (creating the connection on each call)
- It not scales well. It’s restricted to the maximum number of simultaneous connections.
I read about the lib folder, creating the connection on a module placed there and then requiring that connection, but it doesn’t work either.
Am I right with this thougts? Or we must create a db connection on each call?
Thanks in advance
Your Lambda functions should be completely stateless, and relying on a previously opened connection is stateful. This is part of the Lambda/FaaS (Function-as-a-Service) model, rather than a Serverless thing. Basically, you cannot guarantee that your function will run in the same context/container, but it might.
Are you sure that it is really a performance issue for your application? This is definitely a trade-off that using Lambda forces you to make; While it’s obviously less efficient to create connections every time, the up-side is that you don’t need to manage the underlying application container (i.e. OS, etc) yourself.
Thanks @rowanu. Thinking about it this past days, I realized that’s the way it should be, to preserve the stateless character of the service.
The performance problem shouldn’t be that if you manage your db to be clustered, and balancing the connections over that cluster. But that problem would be with a lot of simultaneous connections.
Thank you very much for your clarification.
@dfernper I think this should be working by default: https://forums.aws.amazon.com/thread.jspa?threadID=216000
Basically if the Mysql Node driver does proper connection pooling, stores the pool in the same process it should be fine. Have you tried accessing the connection pool and logging it on every request to make sure if it already has been running?
One issue of course is that if you burst functions like crazy you might run out of connections, but other than that it should be fine.
@dfernper how did you end up handling your connection pooling?