TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to AWS Lambda and serverless

๐Ÿง‘โ€๐Ÿณ AWS Lambda and serverless: cheat sheet

๐ŸŽจ The analogyA kitchen with no cooks waiting around: each order gets a station, and you pay only while it cooks.
๐ŸŽ“ The regular explanationAWS Lambda runs your function in managed execution environments in response to events, scales by adding environments per concurrent request, and bills per request and per millisecond of duration at the memory you configure.
Functions run on eventsStep 5 of 5
The billd1key request 1, 120 ms: d10d2key request 2, 340 ms: d21d3key request 3, 80 ms: d3234๐Ÿ™‹Customer at the counter๐Ÿ“ฆDelivery at the back doorโฐKitchen clock๐Ÿง‘โ€๐ŸณCooking stationidle
  1. A customer at the counter hands in an order. A cook at a station makes the dish from your recipe and hands it straight back.
  2. The bill gets one line for that dish, with the minutes the station spent cooking it.
  3. A delivery is left at the back door. That's an order too, so a station cooks it and the bill gets another line.
  4. At nine o'clock the kitchen clock rings for the morning bread. The clock can order dishes too.
  5. Then nobody orders for an hour. No dishes, no new lines on the bill, nothing to pay.
Cold and warm startsStep 5 of 5
๐Ÿ™‹Customer at the counter๐Ÿฅซ Pantry
  1. The first order of the day, and no station is ready. Someone unpacks the knives and lights the stove first, so this dish is slow. That's a cold start.
  2. The cook fetches what's needed from the pantry, finishes the dish and hands it over.
  3. The next order goes to the same station. The stove is still hot, so the dish comes out quickly.
  4. The cook left some chopped onions on the counter. The next order here might find them, but nobody promises that.
  5. After a quiet spell the station is cleared away, onions and all. Anything worth keeping belongs in the pantry.
Concurrency and throttlingStep 4 of 4
๐Ÿ‘ฅLunch crowd๐Ÿ‘ฉโ€๐ŸณHead chefreserved 3๐Ÿง‘โ€๐ŸณStation 1pre-warmed๐Ÿง‘โ€๐ŸณStation 2pre-warmed๐Ÿง‘โ€๐ŸณStation 3pre-warmed
  1. Three orders arrive at once. A station cooks one dish at a time, so the head chef sets up two more stations.
  2. This recipe may use three stations at most. A fourth customer is told "not right now, try again in a moment".
  3. Those three stations are also kept for this recipe, so a busy neighbour's recipe can never take them.
  4. For a rush you know is coming, you pay to keep stations set up with stoves lit. Orders start cooking straight away.
Synchronous, asynchronous and retriesStep 5 of 5
Ticket trayev1key photo.jpg uploaded: ev10123Failed boxev1key photo.jpg, 3 attempts: ev1โœฆโœฆโœฆโœฆโœฆ0123๐Ÿ™‹Customer at the counter๐ŸššDelivery driver๐Ÿง‘โ€๐ŸณCooking stationtimer rang
  1. A customer at the counter waits for the dish, and would find out straight away if it went wrong.
  2. A delivery driver drops a ticket in the tray. The kitchen says "got it" at once, and the driver leaves.
  3. The cook starts, but the kitchen timer rings before the dish is ready, so it goes in the bin.
  4. Because it came from the tray, the kitchen tries twice more, with a pause before each try. Both fail.
  5. Out of tries, the ticket goes in the failed box you put out, so someone can look at it later. Without a box it would be thrown away.
A Java handler and its roleStep 3 of 3
๐Ÿ™‹Customer at the counter๐Ÿง‘โ€๐ŸณCooking stationrole attached๐Ÿฅซ Pantry๐Ÿท Wine cellar
  1. While the station is set up, the cook clips on a badge that lists the pantry, and gets the pantry door ready, just once.
  2. An order arrives. The cook fetches from the pantry, which the badge allows, and hands back the dish.
  3. The recipe also asks for something from the wine cellar. The badge doesn't list it, so that door stays shut.

๐Ÿงฉ Key questions

The recipe says to light the stove at the start of every dish. Why is that a poor recipe?
A station that stays set up keeps its stove hot, so lighting it once at setup saves time on every later dish. The next ticket usually goes to the same station, so setup work done once pays off again and again.
The cook keeps a tally of orders written on the station's counter. What goes wrong?
Busy times use several stations, and stations get cleared away, so each tally is partial and can vanish. Anything worth keeping belongs in the pantry, not on a station's counter.
Your recipe may use three stations at most, and all three are busy. What happens to a fourth customer at the counter?
They're told not right now, and have to ask again. The set-aside number is also a ceiling, and a customer at the counter isn't put in the tray.
A ticket from the tray keeps failing because the timer rings. Why should the recipe check the ticket number before cooking?
The kitchen tries again, and a dish that was half done before the timer may be made twice. Tray tickets get two more tries, so the same ticket can reach a cook more than once.