Design a vending machine
medium 90 minBuild a vending machine around its money: coins in escrow, change from a limited box, stock by place, a slow motor that can jam, and several events that arrive at once.
Watch lesson4:45
Where it shows up
The same pattern appears across everyday software.
MDB/ICP
The multi-drop bus used inside vending machines, published by the National Automatic Merchandising Association. Coin changers and card readers hang on one bus, behind one protocol, as our `Motor` and `Tender` hang behind interfaces.
Stripe manual capture
A payment can be authorised now and captured later. The authorisation expires, and the amount is released if nobody captures it. That is hold, take and release.
Square delayed capture
`autocomplete: false` makes an approved payment that can be completed or cancelled. After `delay_duration` an automatic action follows.
Square money amounts
Amounts are integers in the smallest unit of the currency: 50 means 0.50 USD, and 2 means 2 yen, because the yen has no smaller unit.
Odoo cash rounding
The point of sale rounds the total when the smallest coin is larger than the minimum unit of account, for example to five cents, when people pay in cash.
SQLite rollback journal
SQLite writes a journal and flushes it to storage before it changes the database file. After a crash the journal tells what to undo: intention first, then act.
PostgreSQL SKIP LOCKED
`SELECT ... FOR UPDATE SKIP LOCKED` lets several sellers take different rows at once. Nobody waits for a row that somebody is already claiming.
Stripe idempotency keys
A request with the same key gives the same result, so a capture whose answer was lost can be sent again.