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
| Word | What it means | Example |
|---|---|---|
| Serverless function | code the provider runs for you when needed | |
| Event | something that happens, such as a file arriving | |
| Trigger | the link that starts a function on an event | |
| Invocation | one run of a function | |
| Time limit | the longest one run may take | 15 minutes |
| Cold start | a 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
# 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?
| Job | Good fit? | Why |
|---|---|---|
| Resize each uploaded photo | Yes | Short, and starts on an event |
| Send a text when an order ships | Yes | Short, and comes and goes |
| Make a sales report at 2am | Yes | Runs once a night, on a timer |
| Turn a two-hour film into a smaller file | No | Longer than the time limit |
| A busy shop’s main website, all day | Often no | Steady 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 runs | Amount | Example price | Cost |
|---|---|---|---|
| Function, furniture shop | 900 runs | First 1,000,000 runs free | £0.00 |
| Function, busy shop | 3,000,000 runs | 16p per 1,000,000 after the free ones | £0.32 |
| A server left on all month | 720 hours | 5p an hour | £36.00 |
The cells students fill in are left blank in this preview.