ealeng

Episode 37 of 55 · Serverless

Serverless cold starts: a 1.6 ms handler took 3 seconds on its first call, and servers win above 15.7 requests a second

A Lambda function whose handler runs in 1.6 ms took 2,984 ms on its first call from a laptop, and Lambda billed 2,364 ms of it. Three imports at the top of the file did most of that: the start-up went from 68 ms to 2,361 ms, and 8 times the memory did not make it faster. Bursts caused the cold starts, not the amount of traffic. Then the bill: a request cost $1.24 per million, $1.00 of it the API front door, and two always-on containers with a load balancer cost $51.10 a month, so they were cheaper above 15.7 steady requests a second.

Lab 16 · Serverless and Event-Driven Compute11 min read
Video transcript10:40

0:00Cold open

0:01

I put a function with a tiny handler on AWS Lambda. Its handler, the code that answers each request, runs in one point six milliseconds.

0:12

But a first call to it took almost three seconds, from my laptop.

0:18

And a typical first call was billed two thousand three hundred sixty-four milliseconds.

0:25

Then I modeled the monthly bill. Above about sixteen steady requests a second, around the clock, two always-on servers would cost less.

0:35

So where did those three seconds go? And when are functions actually cheaper?

0:42Intro

0:44

Hands-on system design, part thirty-seven.

0:47

Today: cold starts, where their time goes, what they cost, and when servers beat functions on price.

0:55Meet Lucía

0:56

I’m Lucía, a platform engineer. My team wants to move a small web API to serverless functions.

1:04

With a function, you upload your code, and the platform runs it when a request arrives. You pay per request and per millisecond, and nothing when nothing happens.

1:15

Each running copy of your code is called an instance. An instance handles one request at a time, so ten requests at once need ten instances.

1:25

When a request finds no idle instance, the platform starts a new one, and that request waits for it. That wait is a cold start.

1:35

Think of taxis. A warm instance is a taxi waiting at the stand. A cold start: none at the stand, so one comes from the depot. And a server is your own car: paid every month, used or not.

1:5001 · What you need

1:52

To run it yourself, you need an AWS account. By my estimate, the whole run cost a few cents. The offline tests need no account, and pass in under two seconds.

2:04

Everything here ran on real Lambda, in Amazon’s Virginia region, called from my laptop over the internet.

2:1402 · The test

2:15

The test: six set-ups. Python and Node.js, small and large memory, a tiny package, and a thirty-three megabyte one with three big Python libraries.

2:27

Twenty rounds. Each round changes a setting on every function, so its next call has to start a new instance. Then I call it twice: cold, then warm.

2:39

After every call, Lambda reports its own times. “Init Duration” appears only on a cold start: starting the runtime, the Python or Node program that runs your code, and running the code outside the handler, such as imports.

2:55

My laptop times each call too, from asking to answer. That includes connecting to Virginia and back: a warm call took about a hundred thirty-five milliseconds.

3:0803 · The runtime

3:09

First, the tiny functions. Python’s runtime started in sixty-eight milliseconds, typically. Node.js in a hundred forty-four.

3:20

From my laptop, a cold call took three hundred thirty-five milliseconds, and a warm one a hundred thirty-seven. So the cold start added about two hundred.

3:31

Lambda’s report counts only sixty-eight of those two hundred as init. The rest isn’t counted at all. Keep that in mind.

3:4204 · Imports

3:43

Now the same tiny handler, with numpy, pandas and Pillow imported at the top. It never uses them.

3:52

The init went from sixty-eight milliseconds to two thousand three hundred sixty-one. From my laptop, almost three seconds.

4:0205 · In code

4:03

Here’s the code. Three import lines at the top. The handler below them just returns an answer.

4:11

But Python runs the top of the file when an instance starts. So every cold start loads these three libraries: about a hundred twenty megabytes, unpacked.

4:22

The handler runs in under two milliseconds. The init is almost all imports.

4:2906 · More memory

4:31

On Lambda, more memory also buys more processor time. So I gave the same function eight times the memory: one gigabyte, instead of a hundred twenty-eight megabytes.

4:43

How much faster do you think the start-up got?

4:48

Not at all. Two thousand three hundred ninety-four milliseconds, against two thousand three hundred sixty-one before.

4:57

This test shows that it didn’t help, not why. Whatever slowed the imports here, the memory setting didn’t change it.

5:0707 · The package

5:08

One more. The same thirty-three megabyte package, but the code imports nothing at the top. It would import a library inside the handler, only when a request needs it.

5:20

Lambda’s init: eighty-two milliseconds. Almost as fast as the tiny function.

5:27

But my laptop waited seven hundred thirty-four milliseconds. About four hundred more than for the tiny package.

5:35

That time isn’t in Lambda’s init. Likely, it’s fetching and unpacking the bigger package first. So the package size probably costs time too.

5:46

So, the three seconds: about two point four in the init, nearly all imports. About half a second Lambda doesn’t report. And the trip from my laptop.

5:5808 · The bill

6:00

Now the bill. A warm call of the tiny function was billed two milliseconds. A cold one, seventy-one. The init is on the bill.

6:10

The heavy function’s cold call: two thousand three hundred sixty-four milliseconds billed. That’s three dollars ninety-four more per million cold starts.

6:21

At one gigabyte: about thirty-two dollars more. Lambda bills memory times time. So eight times the memory cost eight times as much, for a start-up that was no faster.

6:3509 · Real traffic

6:36

How often does real traffic hit a cold start? I sent two hundred forty requests in a minute, through an HTTP front door, to a new function that works a tenth of a second on each.

6:49

Steady, one every quarter second: three cold requests. Likely because the first one’s start-up was still running when the next two arrived.

6:59

Then bursts: eight at once, every two seconds. The same average. Eight instances, and eight cold requests.

7:08

Most likely, the first burst needed all eight at once, and after that they stayed warm. Bursts make cold starts, not the amount of traffic.

7:18

The cold ones took about eight hundred sixty milliseconds from my laptop, against two hundred forty-five for a typical steady one. This function loads Amazon’s own library at the top, which likely explains part of it.

7:3410 · One request

7:36

Now the second question: when are servers cheaper? I measured a real request on that same function, this time reading one item from a database. Ten point seven milliseconds billed, on average. A million of those cost one dollar twenty-four.

7:53

One dollar of that is API Gateway, the HTTP front door in front of the function.

8:01

Twenty cents is Lambda’s fee per call. And the ten point seven milliseconds billed for running: four cents.

8:09

So a faster handler barely moves this bill. The front door does.

8:1611 · The crossing

8:17

The servers: two containers, small machines paid for every hour they run, busy or not. Two, so one can fail. Plus a load balancer that splits requests between them. Fifty-one dollars ten a month, from zero traffic up.

8:33

Lambda starts at zero, and adds three dollars twenty-five a month for every extra request per second, around the clock. By my model, the lines cross at fifteen point seven.

8:46

Without API Gateway, calling the function through its own free web address, the crossing moves to eighty-two.

8:55

I assumed one container handles two thousand requests a second. Even at two hundred, sixteen a second still fits in the minimum two. Both numbers are assumed, not measured.

9:0912 · Bursty traffic

9:10

But traffic is rarely steady. Say bursts of a hundred requests a second, and nothing in between. One busy hour on Lambda costs forty-five cents.

9:21

So Lambda stays cheaper below a hundred fifteen busy hours a month. About four hours a day.

9:29

The model leaves out the free tier, data transfer, and the database, which costs the same either way. It keeps the containers running all month, and charges the load balancer at least about six dollars a month. Without that floor, the crossing is near fourteen.

9:47Back to Lucía

9:49

So, three lessons.

9:51

A slow cold start is mostly your own code: the imports at the top, and likely the package size. More memory didn’t fix it.

10:01

Bursts make cold starts, and the start-up is on the bill.

10:06

And busy, steady traffic gets cheaper on servers sooner than you’d think. Functions win when traffic comes and goes.

10:15

Next time: a pipeline of functions, where the same image arrives three times, and a job that runs longer than the time limit.

10:25

For today: import late, and count the front door.Lucía: Import late. Count the front door.

10:29Summary

10:31

Slow cold starts are mostly imports, bursts make them, and busy steady traffic is cheaper on servers.

Key takeaways

  • ✓A cold start is a request that finds no idle instance, so the platform creates one while the request waits: it fetches the code, starts the runtime, and runs the code outside the handler. A tiny Python function started its runtime in 68 ms (median), and a warm call of the same function ran its handler in 1.6 ms.
  • ✓The imports were the cost: with numpy, pandas and Pillow imported at the top of a handler that never used them, Lambda's Init Duration went from 68 ms to 2,361 ms, the first call from the laptop took 2,984 ms, and 2,364 ms was billed.
  • ✓Eight times the memory did not help: the same start-up took 2,394 ms at 1,024 MB against 2,361 ms at 128 MB, and the extra memory cost $31.93 more per million cold starts, against $3.94 at 128 MB.
  • ✓The package costs time that Init Duration does not show: the same 33 MB package with nothing imported reported an 82 ms init, but the laptop waited 734 ms, about 400 ms more than for the tiny package.
  • ✓Bursts make cold starts, not traffic: 240 requests a minute in steady spacing needed 3 instances and had 3 cold requests (1.25%), while the same 240 in bursts of eight needed 8 instances and had 8 cold requests (3.3%).
  • ✓A request on Lambda cost $1.24 per million, of which $1.00 was API Gateway, the HTTP front door; two containers and a load balancer cost $51.10 a month from zero traffic, so the lines crossed at 15.7 steady requests a second, or 82 without the front door.

A Lambda function whose handler takes 1.6 ms to run took about 3 seconds on its first call. This chapter follows that first call apart: what a cold start is, where its time goes, what it costs, and how often real traffic hits one. Then it asks the second question: at what steady traffic does an always-on server cost less than paying per request?

The examples come from Lab 16 (Serverless and Event-Driven Compute) of the system design labs. The cold-start numbers come from 01_cold_starts.py, which deployed its test functions to real AWS Lambda in Amazon's us-east-1 region, on arm64 processors, using Python 3.13 and Node.js 22, and called them from an Apple M4 Max laptop over the internet. The costs come from 06_cost.py, which applies the public prices the lab fetched from the AWS Pricing API. Each configuration is one recorded run. The whole lab took about 45 minutes and cost a few cents on an AWS account.

What a cold start is

A function is code the platform runs once per request, on an instance: one running copy of your code. An instance handles one request at a time, so ten simultaneous requests need ten instances. Idle instances are kept for a while and then reclaimed, and you pay only while an instance runs.

When a request arrives and no idle instance is waiting, the platform starts one, and the request waits. That wait is a cold start. A cold start has three parts: fetching and unpacking the code, starting the runtime (the Python or Node.js program that will run your code), and running the code outside the handler, such as imports and connections. Lambda reports only that last part as Init Duration, in the report it writes at the end of each call. The handler itself, the function that answers the request, is reported separately.

Think of taxis. A warm instance is a taxi already waiting at the stand. A cold start is a request for which no taxi is waiting, so one has to come from the depot first. A server is your own car, paid for every month whether you drive it or not.

What the first request pays

The lab's test used six functions. Two were tiny: hello-py in Python and hello-node in Node.js, each about 0.3 KB of code, with nothing imported. heavy-py was a 33 MB package with three large libraries, numpy, pandas and Pillow, imported at the top of the file, although the handler never used them. lazy-py was the same package, with the imports moved inside the code that needs them. For each variant, the test forced 20 cold starts, changing a setting so the next call had to start a new instance, and then made one warm call.

Variant Init Duration, median Billed, cold call Laptop, cold call Laptop, warm call
hello-py, Python, 128 MB 68 ms 71 ms 335 ms 137 ms
hello-node, Node.js, 128 MB 144 ms 158.5 ms 426 ms 138 ms
heavy-py, imports at the top, 128 MB 2,361 ms 2,364 ms 2,984 ms 136 ms
lazy-py, nothing imported at the top 82 ms 84.5 ms 734 ms 138 ms

Every cold call reported an Init Duration and no warm call did, across all 20 rounds. A warm call of hello-py was billed 2 ms, and its handler ran in 1.6 ms. The runtime alone takes tens of milliseconds. The laptop's warm call of 137 ms is mostly the network to Virginia and back, and the cold call of 335 ms adds about 200 ms to it, of which Lambda reports 68 ms as Init Duration. The rest is not in Lambda's report.

Where the three seconds go

The heavy-py function is the story. Its first call from the laptop took 2,984 ms at the median, and the billed time was 2,364 ms. Breaking the laptop's cold call into parts gives roughly 2.4 seconds of Init Duration, which is nearly all imports, about half a second that Lambda does not report, and the 136 ms round trip of a warm call.

The code is three import lines and a handler that returns an answer. Python runs the top of a file when an instance starts, so every cold start loaded about 120 MB of libraries to serve a handler that takes under 2 ms.

Try it · Where a cold call's time goesrecorded run, 01_cold_starts.py, 20 rounds
Init Duration, p50
2360.7 ms
Billed, cold call
2363.5 ms
Laptop, cold p50
2,984 ms
Laptop, warm p50
136 ms
One cold call from the laptop, split (scale: 3,100 ms)
init
not reported

Init 2360.7 ms (reported) · not in Init Duration 487.3 ms (derived: cold − warm − init) · the trip (a warm call, 136 ms). The split is a model; the three numbers it uses are recorded.

Package: 33 MB zip, 120 MB unpacked, 128 MB. Extra bill per million cold starts: $3.94 (billed time times memory, list price). Every cold call reported an Init Duration and no warm call did; one run of 20 rounds per variant.

Two tests tried to make the start faster, and they show what does and does not matter. More memory first: on Lambda, memory also buys processor share, so the same function was run at 1,024 MB, eight times the memory. Its init was 2,394 ms, no faster. The test shows that memory did not help this start-up, but not why. The second test was the package. lazy-py shipped the same 33 MB and imported nothing at the top, so Lambda's Init Duration was 82 ms, almost as fast as the tiny function. The laptop still waited 734 ms. The gap is most likely Lambda fetching and unpacking the bigger package before the init starts, which Init Duration does not count. A big package can cost time even when it imports nothing.

The start-up is on the bill

Lambda bills memory times time, and since August 2025 it bills the init phase as well, as the lab's README notes. So a cold start costs money as well as time. The tiny function's cold call was billed 71 ms against 2 ms warm. The heavy function's cold call was billed 2,364 ms, which is $3.94 more per million cold starts at 128 MB. At 1,024 MB, the same 2,397 ms of billed time cost $31.93 more per million, because eight times the memory is billed at eight times the rate, for a start-up that was no faster.

How often real traffic hits a cold start

The second test sent 240 requests through an HTTP front door (API Gateway) to a function that works for 100 ms on each request, once in steady spacing and once in bursts. Both took a minute.

  • Steady, one request every 250 ms (four a second): 3 instances, and 3 cold requests, 1.25% of the total. The median request took 245 ms and the cold ones about 860 ms, measured from the laptop.
  • Bursts, eight requests at once every two seconds: the same average rate, but 8 instances and 8 cold requests, 3.3% of the total. The cold ones took about 869 ms at the median.

Both patterns had 240 requests and all 240 succeeded. The likely explanation, which the lab's run is consistent with, is that the first burst needed all eight instances at once and after that they stayed warm. Steady traffic reached 3 instances, most likely because the first requests overlapped while the first start was still running, and after that each next request found a warm instance. Bursts make cold starts, and the traffic volume alone does not. A cold start also lives in the tail: it did not move the median, but it set the p99, the time the slowest 1% of requests take.

When a server is cheaper

A request on Lambda is billed for its duration and memory, and for the HTTP front door in front of it. The lab measured a warm read of one item from DynamoDB, a database, at 256 MB: 10.7 ms billed on average over 300 calls. At list prices, a million of those cost $1.24. Of that, $1.00 was API Gateway, $0.20 was Lambda's fee per request, and only $0.04 was the 10.7 ms of compute. Making the handler faster barely moves the bill. The front door does.

The alternative is two containers (Fargate tasks, Amazon's always-on container service) of 0.5 vCPU and 1 GB each, behind an Application Load Balancer. Two, so that one can fail. cost.py prices them at $28.84 for the tasks and $22.27 for the load balancer, $51.10 a month from zero traffic up. Lambda starts at zero and adds $3.25 a month for every extra request a second, around the clock. So the lines cross where $51.10 ÷ $3.25 ≈ 15.7 requests a second. Without API Gateway, calling the function through its own web address, the crossing moves to 82.

The container capacity, 2,000 requests a second per task in one model and 200 in another, was assumed rather than measured, and it does not change the crossing at these rates, because both sizes still run the minimum of two tasks. It only changes how fast the container bill grows after the crossing.

Traffic is rarely steady. For bursts of 100 requests a second and nothing in between, one busy hour costs about 45 cents on Lambda, so Lambda stays cheaper below about 115 busy hours a month, around four hours a day.

Try it · Steady traffic: who is cheaper per monthrecorded run, 06_cost.py, prices of 4 Nov 2026
Lambda + API Gateway
$32.50
Lambda, function URL
$6.22
2+ containers, load balancer
$51.10
Crossover, API Gateway
15.72 req/s
Crossover, function URL
82.16 req/s
Per million requests
$1.24
Busy hours, 100 req/s bursts
< 115 h/mo

Lambda rows at 10 req/s steady, all month. Containers are two 0.5 vCPU, 1 GB tasks plus an Application Load Balancer, at $51.10 a month with no traffic. The Lambda side uses the recorded 10.7 ms billed mean at 256 MB. The rows are the cost model of the lab applied to list prices, not a bill.

The workflow and the results

  1. Set up. 00_setup.py fetched the prices and deployed the functions to us-east-1. The laptop then calls the real Lambda endpoints.
  2. Part A, cold versus warm. For each of 20 rounds and each variant, change a setting so the next call starts a new instance, call once cold, then once warm. Lambda's own report gives Init Duration and billed time; the laptop's stopwatch gives the client view.
  3. Part B, traffic. Send 240 requests in steady or bursty patterns through API Gateway, and count the instances that served them, and how many were first requests on their instance.
  4. Cost. Measure a warm read, then price the monthly bill at a range of steady request rates for Lambda and for the containers, and find the crossover.

The cost arithmetic uses the lab's price file, fetched from the AWS Pricing API on 4 November 2026.

Caveats

  • One account, one region, one architecture. The numbers are for us-east-1, arm64 (Graviton) processors, and a laptop about 130 ms away. A function in a closer region, or on x86, would show different numbers.
  • Init Duration is not the whole cold start. The "not reported" part, about 0.5 s for the heavy function and about 0.5 s for the lazy one, is the gap between the laptop and Lambda's report. The lab does not separate download, unpacking and network time.
  • Small samples. Part A has 20 cold calls per variant. Part B has one run per pattern, one function and one request rate, four a second for steady traffic.
  • Not tested: idle time. The lab did not measure how long an instance stays warm before Lambda reclaims it. The "cron once a minute" case is therefore a reasoned guess, not a result.
  • Not tested: the fixes. Provisioned concurrency (instances kept warm, billed by the hour), SnapStart (restoring a saved start-up), and container images were not run. The lab lists them as exercises.
  • The cost model is a model. It uses list prices, keeps the containers running all month, and leaves out the free tier, data transfer, and the database, which costs about the same either way. Without the load balancer's minimum capacity charge, about $5.84 a month, the crossover would be near 14 requests a second, by our arithmetic on the same model.
  • Concurrency and connections were not part of this run. The lab's account allowed only 10 instances at once, so the lab's connection-spike exercise ran on a local platform, not on Lambda, and is not part of these numbers.
  • Real deployments differ. Production functions often attach to a private network, call databases, and load bigger libraries than these tests. Each of those can change the start-up, and each is worth measuring on your own function before you decide.

The checklist

  1. Measure the cold start on your function. Read Init Duration from the report, then time the first call from the caller's side. The gap is the part the report does not show.
  2. Import at the top only what every request needs. Move the rest to where it is used, and measure the start-up again.
  3. Do not assume memory fixes a slow start. Test it, because you pay for the memory on every cold start.
  4. Expect bursts to cost cold starts. Traffic that arrives in spikes needs new instances at each spike, and the start-up is billed.
  5. Count the front door and the steady rate. For steady traffic above the crossover, containers are cheaper; for traffic that comes and goes, functions usually are.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.A function's handler runs in 1.6 ms, but its first call took about 3 seconds. Which part of that wait does Lambda report as Init Duration and bill?

  2. 2.Doubling a function's memory eight times made its import-heavy start-up no faster, and each cold start cost more. What is the right conclusion?

  3. 3.Using the lab's cost model, at what steady request rate does two 0.5 vCPU containers behind a load balancer ($51.10 a month) become cheaper than Lambda with API Gateway?

Check your understanding

Try each question before opening its answer.

A handler runs in 1.6 ms, yet the first call took about 3 seconds from the laptop. Where did the other seconds go?

Most of them were Lambda's Init Duration for the heavy function, about 2.4 seconds: starting the runtime and running the top-level imports of numpy, pandas and Pillow. About half a second was not reported by Lambda at all, most likely fetching and unpacking the package, and the rest was the round trip from the laptop, about 136 ms for a warm call.

Why did giving the heavy function eight times the memory make its cold starts more expensive without making them faster?

Memory buys processor share, but the start-up here was not held back by it: the init took 2,394 ms at 1,024 MB against 2,361 ms at 128 MB. Lambda bills memory times time, so eight times the memory was billed at eight times the rate, $31.93 more per million cold starts against $3.94 at 128 MB.

Steady traffic at four requests a second needed 3 instances, but bursts of eight needed 8. Why does the same average rate cause more cold starts when it comes in bursts?

A steady stream lets an instance finish one request and be ready for the next, so a few instances cover it, and only 3 of 240 requests were cold. A burst of eight at once needs eight instances at the same moment, and none are idle, so all eight start cold. After that they stay warm. The cold starts come from the spike, not the volume.

Why does making the handler faster barely change the monthly cost of a Lambda API?

Because the bill per request is mostly the front door and the request fee, not the compute. For the lab's read of one item, $1.00 of $1.24 per million requests was API Gateway, $0.20 was Lambda's per-request fee, and only $0.04 was the 10.7 ms of compute. Shaving milliseconds off the handler moves the $0.04.

Next up: Chapter 38, idempotent pipelines and dead-letter queues. An image pipeline of functions where the same upload arrives three times and one image is poison.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.