Warm Up Those Slow Startup Times

Keeping Your Lambda Functions Warm

Ivan Montiel
clarityhub
4 min readJan 26, 2018

--

When we began our journey of migrating to serverless, we encountered a big problem: some of our critical user flows felt sluggish. When we would deploy a new version of our application, we would go test it and the on-boarding process would take multiple seconds to get through. Not to mention, our dashboard made several requests when booted, causing a significant time delay before the first meaningful data could be displayed.

When we switched from servers through EC2 to AWS Lambda functions, we knew were were making a huge trade-off. We sacrificed the need to maintain servers, but now we had cold start times with our functions.

We needed to find a way to mitigate these cold startup times on our critical user flows.

Cold Startup Times

In AWS Lambda (and most other cloud function offerings), a cold start happens when an inactive function is executed for the first time. A function is considered cold when it is inactive and must have processor time and memory provisioned for it.

While you aren’t billed for the cold startup time in most cases, it will make your app feel sluggish to end users. From what we’ve seen, a cold startup time can take anywhere from 1 second to several seconds. And when you have multiple API calls to create a single view on your webapp, you may find that most functions take 1 second to startup, but one function will take 7 seconds to startup. Now your single view takes a full 7 seconds to display meaningful data.

One important thing to keep in mind with AWS Lambda is: “Containers are not reused after ~15 minutes of inactivity”.

If you don’t expect your function to get called once every 15 minutes, then you can expect slow downs when it eventually does get called.

The Solution: Keeping Functions Warm

We found an amazing serverless plugin for helping us keep our critical functions warm:

The plugin let’s you mitigate the cold AWS Lambda startup problem by having an AWS Lambda function call your other AWS Lambda functions once every ~5 minutes to keep them warm. By scheduling a CloudWatch event, serverless-plugin-warmup keeps your functions hot so that when they are invoked by end users, they won’t have to experience the slow cold startup times.

Installing the plugin is simple with serverless, but you can easily reproduce the functionality of the plugin outside of serverless:

  1. Create a new function get invoked by a CloudWatch event
  2. Have this function invoke a list of your other functions
  3. Make any function that will be called by your invoker have an early exit when warmed.

For serverless, you just install serverless-plugin-warmup:

Add the plugin to your serverless.yml:

Add warmup: true property to all functions you want to be warm:

And add an early exit to your warmed functions. We like to wrap functions with a higher order function:

You’ll need to add an IAM Role so that the plugin can invoke AWS Lambda functions for you:

Downsides

Now that our AWS Lambda functions are hot, we don’t have to worry about sluggish startup times for our critical functions. But why not just warm up every single one of our functions?

The warm up function gets called 8760 times a month (~43800 in a month / 5 minute interval). And each function that it needs to keep warm is also invoked.

So you are basically using (N + 1) * 8760 100 ms time slicing a month.

According to the Github page for the plugin, keeping 10 functions warm a month is about $14.58. Not a huge amount, but there is a cost associated with keeping your functions warm.

The team is really happy with our move to serverless — albeit with a few growing pains. I’d be interested in learning more about your experience with serverless. Please drop a comment below or connect with me on Twitter.

Ivan Montiel is the founder and CEO of Clarity Hub — a company that integrates with Intercom to give customer success teams real-time suggestions.

--

--