Lesson 2 · Unit 02
Functions
Run on an event, scale to zero, pay per request and per millisecond
What you practise
- Run on an event and scale to zero, cold starts, time and memory limits, statelessness, cost from requests × duration
Try a question from this lesson
Pick one
A webhook handler gets about 200 calls a day, and each takes 50 ms. On an always-on server, roughly what share of the day is it doing work?
Hint
Total the seconds of work, then compare with the 86,400 seconds in a day.
Correct answer
About 0.01%
Why
200 calls of 50 ms make 10 seconds of work in a day of 86,400 seconds. The server is idle for more than 99.9% of what you pay for. Work shaped like this is why functions exist.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
A function per event
You upload a handler. The platform runs it whenever an event arrives: an HTTP request, a queue message, a file landing in a bucket, a timer.
No events: nothing runs, and compute costs nothing. This is scale to zero.
Many events at once: the platform runs many copies side by side.
You never see a server.
AWS: Lambda. Cloudflare: Workers.
event --> [ platform ] --> handler(event)
no events: nothing runs, nothing billedConcept
Limits, state and the bill
Each run has a time limit and a memory size that you configure. Run past the limit and the platform stops you.
Environments are created and thrown away as the platform likes. Memory and local disk may be gone on the next run, and two requests may land in different copies. Keep state in a database, a bucket or a queue.
AWS Lambda bills requests plus duration × memory, counted in GB-seconds. Cloudflare Workers bill requests plus CPU time: waiting on the network is not counted.
10 more exercises in the app
- 1× match the pairs
- 1× order the lines
- 3× multiple choice
- 3× type the answer
- 1× fill the blanks
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.