AWS Lambda - Handler Lifetime
While working at Dosh, I had pretty heavy exposure to managing NodeJS services running in AWS Lambda. During that time, I had learned a few things about the platform that can be leveraged when writing Lambda services. Some of these lessons may influence how you write services but can also give you some performance boosts. It’s important to note that some of the behaviors that I observed about AWS Lambda may not apply to other serverless technologies.
When I first began working on AWS Lambda, I was skeptical. I remember a conversation with my manager where I made the comment about how Lambda feels like we are writing PHP transaction scripts all over again. As time went on, I learned a few things about the way that AWS Lambda manages it’s containers under the hood. By far, the biggest thing I took away was how long the lifespan of a Lambda function actually is. Consider the following Typescript handler:
From an engineering standpoint, this means that we can write lambda functions a lot like how we write your typical HTTP server. Services that load some data at start up can continue to benefit by doing that once and then re-using the loaded data for subsequent requests. Many of our reliability patterns can continue to be applied, such as circuit breaking, persistent connections, and distributed tracing. All in all, working in AWS Lambda isn’t all that different from your typical application development.
I went and took a look at some larger windows of time. Over a 24 hour window, I found some lambda’s running all day and even all night. The image below shows a provisioned lambda running from 9pm to 2am US Central Time.