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 Step Functions

๐Ÿƒ AWS Step Functions: cheat sheet

๐ŸŽจ The analogyA relay race through town, run by an official with a course map who records every handover.
๐ŸŽ“ The regular explanationAWS Step Functions runs workflows defined as state machines in Amazon States Language, calling Lambda functions and AWS APIs step by step, with built-in retries, error branches, parallelism and a recorded history of every execution.
A state machine runs an orderStep 4 of 4
Official's logbookbegkey ExecutionStarted: beg0chkkey CheckOrder succeeded: chk1paykey TakePayment succeeded: pay2endkey ExecutionSucceeded: endโœฆโœฆโœฆโœฆโœฆ34๐Ÿ”ซStarter with the pistol๐Ÿง‘โ€โš–๏ธRace officialsucceeded๐ŸƒRunner 1, checks the order๐Ÿƒโ€โ™€๏ธRunner 2, takes payment๐Ÿƒโ€โ™‚๏ธRunner 3, packs the parcel
  1. The starter fires the pistol with an order on the baton. The official opens a new logbook page and hands back a race number.
  2. The map says the first leg is checking the order. The official hands the baton to runner 1, who checks it and hands it back.
  3. Next on the map is payment. The baton goes to runner 2, now carrying a line that says the order was fine.
  4. Runner 3 packs the parcel. That's the last leg on the map, so the official writes "finished" in the logbook.
Choice, Parallel, Succeed and FailStep 4 of 4
โ†”๏ธ Side-by-side stretch๐ŸƒRunner checks the stock๐ŸชงFork in the roadno rule matched๐Ÿƒโ€โ™€๏ธRunner A, ships it๐Ÿƒโ€โ™‚๏ธRunner B, sends a card๐ŸFinish linefinished๐ŸšฉRed flagOutOfStock
  1. The first runner counts the shelf and writes "3 left" on the note. The baton reaches a fork in the road.
  2. The official reads the note. There's stock, so the race goes up the road to a side-by-side stretch, and two runners set off at once with a copy each.
  3. The official waits until both runners are back, then the race crosses the finish line with both results on the note.
  4. In the next race the shelf is empty. At the fork, the official points down the other road, where a red flag ends the race and writes down why.
Retry with backoff, then CatchStep 4 of 4
Official's logbookf1key TaskFailed, attempt 1: f10f2key TaskFailed, attempt 2: f21f3key TaskFailed, attempt 3: f32catkey Caught, went to TellCustomer: catโœฆโœฆโœฆโœฆโœฆ34๐Ÿง‘โ€โš–๏ธRace officialdetour๐Ÿƒโ€โ™€๏ธPayment runnerโ˜Ž๏ธDetour, phones the customer
  1. The payment runner sets off, but the card machine is busy and the baton drops. The map says wait one second and try again.
  2. Second try, same trouble. The official waits twice as long this time, two seconds.
  3. Third try, and it drops again. The map allowed two extra tries, and both are used up.
  4. The map shows a detour for this, so the race goes to a station that phones the customer, instead of stopping with a red flag.
Standard and Express workflowsStep 4 of 4
Official's logbookbegkey ExecutionStarted: beg0lg1key Task succeeded: lg11endkey ExecutionSucceeded: end234๐Ÿง‘โ€โš–๏ธLong-race officialbilled per handover๐ŸƒRunnerโฑ๏ธSprint officialbilled per run and time๐Ÿƒโ€โ™€๏ธSprint runnerseen it already๐Ÿ—„๏ธ Town records office
  1. A long race starts. It may take up to a year, and the official writes every handover in the logbook as it happens.
  2. Each leg is run exactly once, unless the map asks for a retry. You pay per handover.
  3. A sprint must finish within five minutes. Started and left alone, now and then the whole sprint gets run again from the start, so every runner must spot a baton they've already seen.
  4. The sprint official keeps no logbook. If you ask, a summary goes to the town records office. You pay by sprints run, how long and how much they carry.
Service integrations and callbacksStep 6 of 6
Ticket boxtk9key order 42, task token: tk90123๐Ÿง‘โ€โš–๏ธRace officialresumed๐Ÿ“’ Order-book shop๐ŸŽ‚Bakerydone๐Ÿง‘โ€๐Ÿ’ผManager who approves
  1. The official hands the order straight to the order-book shop, which writes it down and says done at once. No runner needed.
  2. Next the official orders a big cake at the bakery, and stands at the counter while it bakes.
  3. The cake is ready. The official takes it and the race goes on.
  4. A big order needs the manager's say-so. The official drops a numbered ticket in the ticket box, and the race pauses.
  5. Later the manager picks up the ticket from the box and looks at the order.
  6. The manager hands the ticket back saying approved. The official knows exactly which race it belongs to, and that race starts running again.
A saga with compensating stepsStep 5 of 5
Official's logbookhldkey ReserveStock succeeded: hld0pdkey TakePayment succeeded: pd1xkey BookCourier failed, caught: x2rfkey RefundPayment succeeded: rf3rlkey ReleaseStock, then Fail: rlโœฆโœฆโœฆโœฆโœฆ45๐Ÿง‘โ€โš–๏ธRace officialfailed and undone๐Ÿ“ฆStockroom๐Ÿ’ณTillโœ• โœ•๐Ÿ’ซCourier
  1. The stockroom sets the parcel aside for this order.
  2. The till takes the customer's money.
  3. The courier has no van, even after a couple of tries. The race can't finish, but it can't just stop either.
  4. The detour runs backwards. First the till gives the money back, checking the order number so it never refunds twice.
  5. Then the stockroom puts the parcel back on the shelf. Everything is undone, and the official raises the red flag with the reason.

๐Ÿงฉ Key questions

The payment runner took the money but tripped on the way back, so the official sees a dropped baton and has the leg run again. What should the payment runner do?
Check the order number on the note first, and skip the charge if it's already been taken. A retry re-runs a leg that may already have done its job, so each runner checks whether the work was done.
A race has to pause for up to two days until a manager says yes to a big order. Which way of running it fits?
A long race, handing the manager a numbered ticket and waiting for it to come back. Sprints last five minutes at most and can't hand out tickets; a long race can pause for days, and a time limit stops a lost ticket holding it forever.
The stockroom has set a parcel aside and the till has taken the money, then the courier has no van. What should the map do?
Take a detour that refunds the money, then puts the parcel back, then raises the red flag. Each finished leg changed something real, so the detour undoes them, last one first, before the race is marked as failed.
You need millions of tiny races a day, each over in a second, and every runner can safely see the same baton twice. Which kind of race?
Sprints, paid by how many and how long, with a summary sent to the records office. Sprints are made for huge numbers of short races, and the only price, a race now and then run twice, doesn't matter here.