Virtual machines
Compute means processing power: a CPU and memory that run your code. In the cloud that power sits in physical servers in a provider's data centre. You never touch the machine; you rent a slice of it.
A hypervisor is software that splits one physical server into several virtual machines (VMs). Each VM gets its own share of CPU and memory and its own operating system, kept apart from its neighbours.
You pick a size, say 2 virtual CPUs (vCPUs) and 4 GiB of memory, then install your app on it. The VM is yours to run: you update its operating system and decide when to add more VMs.
Requests arrive now and then; between them the VM sits waiting. It is billed for every second it is switched on, busy or idle. Left on for one hour, that is 3600 seconds, even for just 4 requests.
Serverless functions
Serverless turns this around. You upload only a function, like this Python handler, and the provider runs the servers for you. There is no VM to size or update. AWS Lambda is a well-known example.
The first request finds nothing running. The platform starts a fresh copy of your function, called an instance: it loads the code, starts Python and runs any set-up code. That extra wait is a cold start.
Now the handler runs. event is the request, {"name": "Ada"}, and it returns "Hello, Ada". Say that takes 0.12 seconds. You pay for run time, counted to the millisecond, plus a tiny fee per request. The cold-start set-up time is billed too.
Three requests arrive at once. On AWS Lambda each instance handles one request at a time by default, so the platform starts two more copies, side by side. That is scaling out, and it is automatic. Run time: 4 × 0.12 = 0.48 seconds.
When the requests stop, the instances wait idle for a while, then the platform shuts them down. With no code running, the function is billed nothing for its time. The next request brings a cold start again.
Choosing
Same 4 requests: the VM was billed 3600 seconds; the function ran 0.48, plus cold starts. VMs suit steady or long-running work and full control. Serverless suits short, bursty tasks; one Lambda call can run at most 15 minutes.
What happens
;; step 1/10 · server compute = CPU + memory that run your code cloud: real servers in a data centre you rent a slice, on demand the provider owns and fixes the hardware
Counted seconds are not dollars: a second of function time usually costs more than a second of a small VM, so steady, busy traffic can be cheaper on VMs. The meters show time counted, not the bill: the VM is billed for every second it is on, while the function meter shows run time only and leaves out the billed cold-start time. The 0.12 s run time is an assumed example, not a measurement. Real run times depend on the code and the memory setting.