Skip to main content

Cold starts

Serverless architecture has many advantages compared to a more traditional hosting solution. It however isn't perfect either. One of the best things of it is the pricing model. Instead of paying constant fee for fixed amount of resourses, even if there is no usage at all serverless functions are billed on usage basis. Every execution is counted and if there are no executions there is no cost either.

Sounds great, but companies offering serverless hosting aren't in it for charity. They aspire to optimize the lifetime of your function environments to be as short and efficient as possible. This means if there are no other calls to your function within certain amount of time your functions execution environment will be shut down. Later, when the next call comes in, if there are no running excecution environments one needs to be started. While these environments are extremely lightweight containers it still takes some time to start them up. Even the lightest functions with practically no dependencies take a few seconds to load.

When there is constant traffic to the site, and hence constant calls to the functions this is not an issue. But when there is less traffic and users the cold start will occur more often. On former case putting every function on it's own environment is a good choice. On the quieter sites it's a bit trickier. You could put everything in one environment, ensuring that an individual user only gets a single cold start. All calls during their session would be handled by that same environment. On the other hand, it could be better to split the functions on their own, ensuring that the cold starts are always as short as possible even if the user might experience a few more of those.