Serverless Lambdas are not Zombies

Hunting good/best practices in serverless applications with nodejs
Hunting good/best practices in serverless 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.

Common lambda steps
Common lambda steps

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.

The worst practice on lambdas for relatively heavy processes.
The worst practice on lambdas for relatively heavy processes.

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.

Best good practices with lambdas
Best good practices with lambdas

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.

--

--

--

Software engineer frontend lover and backend enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

HMS Account Kit and Features

Basics Of Docker

Your dream doesn’t really matter to anyone else Some people might take interest. S

A Day in the Life of a Software Engineer at Alasco

Animating Sprites in Unity

Animated Login Form using HTML CSS

Animated Login Form using HTML CSS

DeepAR Studio 4.0

10 essential things that should be consider before hiring an iOS developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Carlos Guadir

Carlos Guadir

Software engineer frontend lover and backend enthusiast

More from Medium

Intro to GraphQL

Get to know JSON

How to be an impactful developer

Javasript => Closures…