Serverless and cold starts

Why you need it

Most apps are quiet for hours: at night, at weekends, between bursts. An autoscaler that never goes below one copy still bills you for that copy all night while nobody visits. Going down to zero saves the money, but then someone has to wait for the first copy to start.

Think of it like this

A light on a motion sensor in a hallway. Nobody there, the light is off and costs nothing. The first person to walk in stands in the dark for a moment before it turns on.

Where you’ll find it

What to know first

Autoscaling. A program changes the number of copies to match how busy they are. See Autoscaling.

Starting a copy. Finding a server, loading the code, starting the language, then running the app's own setup. Each step takes time.

How it works

  1. Zero copies when nobody comes. With no visitors, the platform stops every copy, and you pay nothing. This is the night with zero copies in the animation.
  2. The first visitor starts a copy. When a request arrives and no copy runs, one must start before it can answer. That wait is the cold start in the animation. Later visitors find it running: a warm start.
  3. Copies stay warm for a while. After answering, a copy is kept around for some minutes in case more visitors come. Only after a long quiet does it go back to zero.
  4. Make the start shorter. Smaller code, fewer libraries, and less work at startup all cut the wait. Some platforms can save a snapshot of a started copy and resume from it.
  5. Or keep one warm, and pay. Ask for at least one copy always running. Nobody waits, but the night costs money again, as at the end of the animation. Pick per app: a nightly report can wait 3 seconds; a checkout should not.

An example

A shop gets no visitors from midnight to 7:00. One always-on copy costs, say, 5 cents an hour: 35 cents a night for nothing.

With zero copies, the night costs 0. At 7:00 the first visitor waits 3 seconds for a cold start. Everyone after waits 0.2 seconds.

With one copy kept warm, the first visitor also waits 0.2 seconds, and the 35 cents is back.

Further reading