Serverless: code without looking after machines
You hand over a function and say what wakes it up. The platform runs it only when something happens, copes when everyone turns up at once, and bills you for the time it really runs. It sounds too good, and the price is cold starts and some limits. Let's see them.
What you'll take away
- Explain what a cold start is and why a quiet API meets one almost every time
- Know why five requests at once get five environments, up to a ceiling and a 429
- Weigh provisioned concurrency: no cold start, but billed every hour
- Spot when a plain server wins: steady traffic, or jobs past the 15-minute timeout
The 1-minute version
Tap a point and the picture below plays that step.
Press 5 at once twice: amber outlines (cold starts) the first time, none the second. Wait until the buildings go, send again: a cold start again
An API on Lambda gets two or three visitors an hour, and every one of them says the first load is slow. Why?
The picture: the function is a block of empty lots up the avenue from the users. Nothing stands there until a request comes; then a building is put up for it. An amber outline and a filling bar are its cold start, lit windows are the handler running, and a teal bar runs down while it sits idle, until it is taken away. A gold roof is kept warm. Red is a timeout, or a 429 at the API. Times are shortened; a real cold start is anything from about a hundred milliseconds to seconds.
Compare them all: Serverless
The same day of traffic three ways: a quiet night, a lunch rush, a steady afternoon. Each bar is the requests running at once in that half hour. See who pays what, who gets cold starts, and who has to queue.
Use it when Steady traffic all day, long-lived connections (WebSockets), or work that runs past serverless limits
Skip it when You pay all night with nobody there, and a rush past what the machine takes has to queue
Made-up prices; only their shape is real: a server bills by the hour, serverless by the time it runs, warm ones by the hour too. A half hour with nobody at all counts as every environment reclaimed. Press any tile to see it large above, with when it fits.
What you get, what you give
| Serverless function | Your own server | |
|---|---|---|
| Nobody using it | Nothing runs, nothing billed | Billed every hour |
| A sudden rush | More environments, by themselves, up to a limit | What the machine takes, then a queue |
| First request after a quiet spell | A cold start | Already running |
| Long jobs | Up to a timeout (15 minutes on Lambda) | As long as you like |
| State between requests | Only by luck; keep it outside | In memory, if you accept the risk |
| Who patches the OS | The platform | You |
Triggers are the point
A function waits for an event: an HTTP request, a file landing in a bucket, a message on a queue, a schedule. Many triggers retry when the function fails, so the same event can arrive twice. Write handlers that can run twice without doing the work twice (idempotent).
Keep the outside small
Everything above the handler runs at each cold start. Open database clients and read config there so warm calls reuse them, but keep it light: a big bundle or slow set-up makes every cold start slower.
In real code
// An AWS Lambda function behind an HTTP API (Node.js 22). The code outside the handler runs once per
// execution environment, at its cold start; the handler runs for every request that environment takes.
const bootedAt = Date.now();
let served = 0;
interface HttpEvent { rawPath: string; queryStringParameters?: Record<string, string> }
interface HttpResult { statusCode: number; headers: Record<string, string>; body: string }
export async function handler(event: HttpEvent): Promise<HttpResult> {
served += 1;
const wait = Number(event.queryStringParameters?.wait ?? 0); // ?wait=3000 makes a slow job
if (wait) await new Promise((done) => setTimeout(done, wait));
return {
statusCode: 200,
headers: { "content-type": "application/json" },
body: JSON.stringify({ path: event.rawPath, cold: served === 1, served, warmForMs: Date.now() - bootedAt }),
};
}
invoke.sh runs it in AWS's Lambda image for Node.js 22 (type-checked with tsc too)A Lambda function behind an HTTP API, in TypeScript. The two lines at the top run once per environment, at its cold start; the handler runs for every request that environment takes, which is how the reply can say whether it was cold.
Common questions
Does serverless mean there are no servers?
There are servers; you just don't look after them. You hand over a function and say what wakes it up. The platform runs it when something happens, adds environments when a rush comes, patches the OS, and bills for the time it really runs.
What is a cold start?
When no environment is free, the platform makes one: it loads your code, starts the runtime and runs the code outside the handler before the handler gets to work. That's anything from about a hundred milliseconds to seconds slower than a warm start, depending on the language and the code's size.
Why is an API with few visitors slow almost every time?
Idle environments get reclaimed after a while, so with visitors far apart nearly every request is a cold start. Fixes: provisioned concurrency (pay to keep some warm), a smaller bundle and less set-up, or just accept it where speed doesn't matter.
Can I keep state in a variable outside the handler?
It survives between warm calls to the same environment, but every environment has its own copy, and it's gone once that one is reclaimed. Fine for reusing a database client; anything you must really count belongs outside, in a database or Redis.
What limits does Lambda have?
A ceiling on how many run at once: past it, requests are throttled (429). And a timeout of 15 minutes at most: a longer job is stopped where it is and its environment thrown away. A 40-minute job has to be split into pieces passed along a queue, or run in a container instead.
What does provisioned concurrency cost you?
Environments are made ahead and kept warm, so requests that land on them skip the cold start. You pay for the ones you reserved every hour, used or not, and past that number requests cold-start as usual.
Why must a handler be safe to run twice?
Many triggers retry when the function fails, so the same event, an upload or a queue message, can arrive twice. Write the handler so running it twice doesn't do the work twice (idempotent), for example by remembering the ids you've done.
When is a plain server the better deal?
Steady, high traffic all day, long-lived connections like WebSockets, or work that runs past the timeout: there a server you keep running is usually cheaper and simpler. Serverless suits traffic that comes and goes, and work triggered by events.
Next lesson
Keeping files in object storageLevel: Basic
A function keeps no files of its own; next, where files go instead: buckets, cheaper tiers and uploads straight from the browser.