Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

What serverless code is good for

After this lesson: I can say what serverless code is good for.

45 min lessonNo commands

Thirty photos a day

A furniture shop lets staff upload product photos. Each photo needs a small copy for the website. About thirty arrive a day, mostly in the morning.

A server running all day for this would sit idle for 23 hours. Serverless code runs only when a photo arrives, and the shop pays only for those runs.

By the end of this lesson

Note

I can say what serverless code is good for.

Key words

WordWhat it meansExample
Serverless functioncode the provider runs for you when needed
Eventsomething that happens, such as a file arriving
Triggerthe link that starts a function on an event
Invocationone run of a function
Time limitthe longest one run may take15 minutes
Cold starta short delay after a quiet spell

The cells students fill in are left blank in this preview.

A function runs when an event happens

With a serverless function, you write one small function and upload it. You do not choose or look after a server.

A trigger links the function to an event, such as a new file in a bucket. Each time the event happens, the provider runs the function once. That is one invocation. You pay for each invocation and for how long it runs.

Try it: a function and its events

Code: python
# A serverless function is an ordinary function.
# The provider calls it with the details of the event.

def handle(event):
    key = event["key"]
    size_kb = event["size_kb"]
    if size_kb > 5000:
        return "Too big, skipped " + key
    return "Made a small copy of " + key

# Two pretend events, as the provider would send them.
print(handle({"key": "photos/sofa-grey.jpg", "size_kb": 2400}))
print(handle({"key": "photos/table-oak.jpg", "size_kb": 7100}))

# Add a third event for photos/chair-red.jpg, size 900, then press Run.

Short jobs that come and go suit it

Serverless is good for short jobs that come and go. Think of resizing a photo, sending a text when an order ships, or making a report once a night.

It is a poor fit for long jobs. Each run has a time limit, often 15 minutes. It can also cost more than a server when the work never stops. A cold start adds a short delay after a quiet spell.

Worked example: is serverless a good fit?

JobGood fit?Why
Resize each uploaded photoYesShort, and starts on an event
Send a text when an order shipsYesShort, and comes and goes
Make a sales report at 2amYesRuns once a night, on a timer
Turn a two-hour film into a smaller fileNoLonger than the time limit
A busy shop’s main website, all dayOften noSteady work costs less on servers

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think serverless means there are no servers. There are servers. The provider owns them, runs them and patches them, and you never see them. Your code still runs on a real machine, with real limits on time and memory.

You pay for each run

A serverless bill counts the invocations and the time they take. Many providers give a free allowance each month, such as the first million runs.

For thirty photos a day, the bill can be close to nothing. For millions of runs every hour, add it up and compare it with a server.

Worked example: a month of small copies

What runsAmountExample priceCost
Function, furniture shop900 runsFirst 1,000,000 runs free£0.00
Function, busy shop3,000,000 runs16p per 1,000,000 after the free ones£0.32
A server left on all month720 hours5p an hour£36.00

The cells students fill in are left blank in this preview.

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.