Serverless Lambdas are not Zombies
Lambdas and the best practices when you are creating microservices or monorepo applications with nodejs

Well as you know at this time, lambda is a code portion executing by a runtime in a cloud provider like AWS Lambdas, Google Functions or Azure Functions to recall some of them. Commonly these lambda functions have been used to create API’s with the purpose to provide an HTTP endpoint. Also there are other use cases like websockets or cron jobs. But for this short article I’ll talk about HTTP endpoints.
At creating a lambda its a procedural code executing to 1) receive the request, 2) prepare lambda, 3) process request and finally 4) return a response.

But what happens if you have a database connection, or maybe you are preparing (post-building) a frameworks app instance. Let me tell you about my experience with it. My team had a database connection and also we created a graphql server with apollo server. So for each request we recreate the connection and also post-build an apollo server instance, we did that because we needed some flexibility but it is not enough to justify that we forgot an important feature about lambdas.

After realizing our mistake. We conclude that lambdas are not Zombies, they have a short life cycle before freezing the code execution, lambdas are alive for some minutes. So, you can re-use some of the instances or database connections. This speeds up the process for the request almost over 50% of the time for every request. Finally a good proposal for the last lambdas could be like this.

Conclusion
Applying this pattern, very close to singleton because this is not object oriented programming. With this, we handle the heavy task for the first time request cold-start in the lambda function, for the next requests the heavy process is cached in the runtime memory. So the response time is reduced at the maximum and you are ready for a production release. That’s all folks. Thank you for reading and have a nice day.