Serverless and FaaS
Level 2 · The cloud
How serverless code runs only when something happens, and costs nothing when nothing does. Where it shines, and the cold start that comes with it.
Course contentsLesson 5 of 11
Level 1 · Virtual machines
Level 2 · The cloud
Level 3 · Where it runs
Level 4 · Running and paying
After this lesson you can
- Explain what serverless means
- Write the shape of a cloud function
- Say when serverless is a poor fit
11 mincovers 2 glossary entries
Worth reading first: IaaS, PaaS and SaaS
Code that runs only when something happens
Serverless computing means running code with no server to look after, and paying only while it runs. There are still servers, but the cloud runs them and you never see them. You upload a small function, and the cloud starts a copy each time an event calls for it. With no events, it costs nothing.
An event is anything that should start the code. A photo is uploaded. A web request arrives. A timer goes off every hour. Each one starts a copy of your function, which does its job and stops.
Function as a Service, or FaaS, is the most common kind. You write one function, set what starts it, and upload it. If 300 events arrive at once, the cloud runs 300 copies. At night, it runs none.
The bill is counted in tiny pieces, often by the millisecond, plus a small charge per call. A job that runs a few thousand times a day can cost a few cents a month.
There are limits. A function must finish within a set time, often up to 15 minutes. It keeps nothing in memory between calls. So anything it needs to keep goes in a database or in storage.
The first call after a quiet spell is slower. The cloud has to start a fresh copy, which can add from a tenth of a second to a few seconds. This is called a cold start, and it matters most for pages a person is waiting on.
The mistake is to use it for a job that runs all day. A function busy every second of the day can cost several times more than a small server doing the same work. Serverless is cheapest when the work comes in bursts.
export const handler = async (event) => {
return { statusCode: 200, body: 'Hello' };
};Worked example
A photo shrinker that costs pennies
A recipe site lets users upload photos. Each one must be shrunk to three sizes. Uploads come in bursts after dinner and almost none at night.
A function runs on each upload and takes about half a second. With 2,000 uploads a day, the monthly bill is under a dollar, and no one patches a server.
The words for this lesson2
Check yourself
When does serverless cost more than a small server?
Finished this lesson?
Marking it complete also marks its 2 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.