Skip to content
Lesson 34

Serverless

What serverless really means, when paying per use is cheap and when it isn't, why cold starts happen, and how a burst of functions can take down a shared database.

The lesson in text

Click a paragraph to hear it.

01Sunday evening

Sunday, 19:20, and Omar, the on-call engineer, posts in the team chat: search is slow. Normally, almost every search answers within 0.4 seconds. Tonight, the slow ones take 2.6 seconds.

It's the same every Sunday. Hotels prepare their week, and between seven and nine in the evening they upload about 4,000 photos. Each photo is resized into smaller versions, on the same web servers that answer the guests' searches.

Karim, the CTO, asks: should we add two servers just for Sunday? Or is this a job for serverless? In this lesson, you'll learn what serverless really means, when it's cheap and when it isn't, and the one thing it can break outside its own code.

At StayHub, that thing broke for nine minutes. And for nine minutes, nobody could book a room.

02Your own cars, or taxis

Imagine your company needs to drive people around. One option: buy a fleet of cars. They're always there, ready to go. And you pay for every one of them, every day, busy or parked.

The other option: taxis. When ten people need a ride, ten taxis come. When nobody needs one, you pay nothing. You pay per ride, and someone else looks after the cars.

Which is cheaper? It depends on the shape of your trips. A team that drives the same route all day is cheaper with its own cars. Rare, sudden rushes are cheaper by taxi.

Servers are the fleet. Serverless is the taxis. And there's a catch you know from real taxis: when none is nearby, you wait for one to arrive.

Your own cars, or taxis
Your own fleet (servers)Taxis (serverless)
You payFor every car, every day, busy or parkedPer ride: runs × time × memory
A sudden rushOnly as many cars as you ownAs many taxis as you need, in seconds
Nobody travellingYou still payNothing (scale to zero)
WaitingA car is always readySometimes you wait for one (a cold start)
Looking after the carsYou do (patching, sizing)The provider does
Cheaper forSteady traffic all dayRare, sudden rushes
Neither is always cheaper. The shape of the trips decides, and whether someone is standing on the pavement waiting.

03What serverless means

First, the name is misleading. There are still servers. You just don't manage them. The cloud provider owns them, patches them, and decides how many to run.

You give the provider a small piece of code, called a function, plus a trigger: what should start it. A trigger can be a web request, a file arriving in storage, a message in a queue, or a time of day.

When the trigger fires, the provider starts a copy of your function, called an instance, runs it, and stops it. If four hundred photos arrive at once, it can start hundreds of instances side by side.

When nothing happens, nothing runs, and you pay nothing. That's called scaling to zero. You pay per run: how many times the function ran, for how long, and with how much memory.

The life of one function run
  1. 1
    Trigger
    A web request, a file landing in storage, a queue message, a time of day
  2. 2
    Start an instance
    If none is warm, this is the cold start (StayHub: 0.3 to 1.5 s)
  3. 3
    Run the function
    At most 15 minutes per run on AWS Lambda
  4. 4
    Save what matters
    To the database or storage: the instance keeps nothing
  5. 5
    Back to zero
    No events, nothing runs, nothing to pay
Many runs can happen side by side: 400 photos can mean hundreds of instances at once (AWS's default limit is 1,000 per Region).

Two rules come with it. An instance can vanish at any moment, so it keeps nothing in its own memory: anything that matters goes to the database or to storage. And each run has a time limit. On AWS Lambda, it's fifteen minutes at most.

04Waiting for a taxi

Now the catch. When no instance is running, the next trigger has to wait while the provider starts a fresh one. That's a cold start: the wait for a taxi to arrive.

At StayHub, cold starts took between 0.3 and 1.5 seconds. For a photo, nobody notices: the hotel owner isn't watching the resize. For a guest who just pressed Book, a second and a half is a long time.

So here's the first question to ask about any job: is someone waiting for it? If nobody waits, cold starts don't matter.

05Do the math

The second question: what shape is the traffic? StayHub's photos trickle in at about 300 a day, then 4,000 arrive in two hours on Sunday. That's about 24,000 photos a month, with one huge spike a week.

Servers big enough for Sunday would sit idle the rest of the week. Two extra servers would cost about $120 a month.

With functions, you multiply. 24,000 photos, 1.5 seconds each, 1 gigabyte of memory: that's 36,000 gigabyte-seconds. At AWS's public price, about sixty cents a month.

Do the math: StayHub's photos
24,000 photos × 1.5 s × 1 GBruns × seconds × memory
= 36,000 GB-seconds
36,000 × $0.0000166667 ≈ $0.60compute
24,000 requests × $0.20 / 1M ≈ $0.005requests
WorkloadShapeBetter on
Photo resizing300 a day, then 4,000 in two hours on Sunday; nobody waitsFunctions: ~$0.60 vs ~$120 a month for two extra servers
Booking API~40 requests a second, all day; guests wait on every requestServers: steady load, no cold starts on checkout
AWS Lambda's public price on 2026-10-21: $0.0000166667 per GB-second and $0.20 per million requests, before the monthly free allowance. Prices change; check the provider's page.

Now the booking API. It answers about forty requests a second, all day long. That's steady traffic: the team that drives the same route. Paying per request there costs more than owning servers, and cold starts would land on checkout. So the booking API stays on servers.

06Moving the photos

So the team moved only the photo resizing. The hotel's dashboard asks StayHub for a one-time upload link, and sends the photo straight to storage. The big file never touches the web servers.

When the file lands, storage fires a trigger: a photo was created. That starts the resize function, once per photo. It saves the small versions to storage, and marks the photo as ready in the database.

One photo, serverless
Hotel dashboard
StayHub app
Storage
Resize function
I want to upload a photo
one-time upload link
the photo itself
trigger: photo created
small versions saved
The big file never touches the web servers. The function is a thin wrapper around StayHub's own photos module, and running it twice gives the same result.

Triggers like this one can fire twice for the same photo. So the function must be safe to run twice: same file names, same result. Running it again changes nothing.

One more choice. The resize code itself stays in StayHub's normal photos module. The function is only a thin wrapper around it, like the adapters from the last lesson. If StayHub ever changes provider, only that wrapper changes.

The next Sunday, search stayed fast. The web servers didn't even notice the photos.

07Four hundred taxis

Then came the incident. The first version of the function opened its own new connection to the database on every run. And nobody had set a limit on how many could run at once.

Think of a database connection as a parking space at the database's door. StayHub's database had 200 of them, and the main app already used about 100.

One afternoon, a hotel chain uploaded 6,000 photos in five minutes. The provider happily started about 400 instances. Four hundred taxis, heading for a car park with a hundred free spaces.

The spaces ran out, and they were shared. Searches and bookings couldn't get a connection either. For nine minutes, nobody could book a room. Failed resizes retried, and asked for even more spaces.

Four hundred taxis, a hundred free spaces
Before: one new connection per run, no cap
  • · 6,000 photos in 5 minutes → ~400 instances
  • · The database allows 200 connections; the app already uses ~100
  • · Spaces run out for everyone: bookings fail for 9 minutes
  • · Failed resizes retry and ask for even more
After: pooled, capped, patient
  • · One connection per instance, opened once and reused
  • · Through a connection pooler that shares a few connections
  • · At most 50 instances at once
  • · The rest of the photos wait their turn; bookings never notice
The provider knows nothing about your database. It will start as many instances as there are photos, and every one of them asked for its own connection.

The fix had three parts. First, each instance opens one connection and reuses it, through a connection pooler, a gatekeeper that lets many instances share a few connections. Second, a cap of fifty instances at once. Third, the other photos simply wait their turn. The provider scales in seconds. Your database doesn't.

08Your job as lead

When Claude Code suggests serverless, ask three questions. Is someone waiting for this job? What shape is the traffic? And what shared thing does it touch, like the database?

Ask it to show the cost as arithmetic, not as a claim: runs, times seconds, times memory, times the price. Then compare it with the servers you'd pay for instead.

In the code, check two things. The database connection is opened once, outside the code that runs for each photo, through the pooler, with a cap on instances. And running the function twice on the same photo gives the same result.

Before you accept a serverless change
Ask or checkGood answer
Is someone waiting for this job?No, or the cold start was measured and is acceptable
What shape is the traffic?Bursty or rare; steady all-day traffic stays on servers
What does it cost, as arithmetic?Runs × seconds × memory × price, compared with the servers
What shared thing does it touch?One pooled connection per instance, opened outside the per-photo code, and a cap
What if it runs twice?Same file names, same row: nothing changes
Can it finish in time?Well under the limit (15 minutes on AWS Lambda)

Let's recap. Serverless is taxis: you pay per ride, they come in any number, and sometimes you wait for one. Great for bursty work nobody waits for. Costly and slow for steady traffic that guests wait on. And never let a burst of taxis fill the database's car park. Next: how to write a decision like this one down, so the team still knows why in a year.

Check yourself

0/10 answered
  1. 1What does "serverless" really mean?

  2. 2What is a cold start?

  3. 3Photos arrive at about 300 a day, then 4,000 in two hours on Sunday, and nobody waits for the resize. Karim asks: two more servers, or functions?

  4. 4Karim then proposes moving the booking API (about 40 requests a second, all day) to functions. What's the best answer?

  5. 5Why must a function keep nothing important in its own memory, like a running count of photos per hotel?

  6. 624,000 photos a month, 1.5 seconds each, 1 GB of memory. How many gigabyte-seconds is that?

  7. 7Claude Code's resize function opens a new database connection every time it runs, with no limit on instances, "so it scales freely on Sundays". What do you ask for?

  8. 8Users see bookings failing for several minutes while a hotel chain uploads thousands of photos. What's the most likely cause?

  9. 9The storage trigger sometimes fires twice for the same photo. What should the function guarantee?

  10. 10Claude Code proposes running a 40-minute nightly report as a single AWS Lambda function. What's wrong?

Free account

Save your progress — create a free account

Free. No card. Your finished lessons follow you to any device, and you can ask questions in the community.

Create my free account
Founding members

Every lesson is free while we build. Lock in €9/month for later.

The first 100 members keep the founding price for as long as they stay. Reserving is free: no card, nothing is charged.

See the founding offer

Questions from the community

All questions →