Concurrent Kitchen — System Design

Order the cooks' tools in Pak Tono's hajatan kitchen: 18 levels of mutexes, data races, deadlocks and lock ordering. 18 levels, right in the browser.

What you learn in this game

Several cooks (goroutines) work one kitchen at the same time. If two cooks change the same dish (shared data) at the same moment, the result is a mess: that is a data race. The cure is a mutex, a tool only one person can hold. A cook must hold the guarding tool before touching its dish. Set each cook's tools, then start cooking: the engine tries every order of events, and if one goes wrong, the kitchen plays that order step by step. If every order is safe, the kitchen cooks for real in rounds: each round every cook takes one step (taking a tool, cooking, or giving the tool back), and every dish must be ready before the rice on the stove is done.

A mutex is tied to one particular piece of data. Holding the wrong lock gives a false sense of safety: the data is still unprotected. Make sure you hold that dish's guard, not whatever tool happens to be in your hand.

A deadlock happens when two cooks each hold a tool the other needs and neither lets go: a circle of waiting. Mrs. Ijah holds the pestle and waits for the wok; Mrs. Yanti holds the wok and waits for the pestle. The kitchen freezes for good, and the rice burns. The classic cure is a global lock order: every cook takes the tools in the same order, say always the pestle before the wok, so the circle can never form.

Locking too much (coarse locking) is safe from races, but it kills parallelism: other cooks wait for a tool you aren't even using. Good granularity: lock only the data you touch, for as short a time as you can. Today the kitchen has a rule: no holding a tool that doesn't guard your dish.

The dining philosophers problem: cooks in a ring, each two neighbours sharing one tool. If everyone takes the tool on their left first, each cook holds one and waits for one: a perfect circle of waiting, and everyone freezes. The cure is a total order over all the tools. Number them pestle, wok, knife, and have every cook take the lowest number first. The last cook ends up going against the ring, and the ring breaks.

Careful when patching a race: adding locks on reflex can cause a deadlock if the order isn't the same everywhere. Here both cooks lack the wok, so the mi goreng is unguarded. Add the wok to both, but in the same order. Fixing one concurrency bug without planting another takes both disciplines at once.

What the levels ask

  1. Give each cook pestle.Lock() before they touch the sambal, then start cooking.
  2. The pestle guards the sambal, the wok guards the mi goreng. Add the missing tool, without making one cook wait for the other.
  3. The knife guards the spiced beef. Nobody touches it without the knife in hand.
  4. Hold the pestle and the wok before touching the sambal and the mi goreng.
  5. Only a cook who touches a dish needs to hold its guard.
  6. The sambal is safe only when a cook holds the pestle, not some other tool.
  7. Make both cooks take their tools in the same order, so there is no circle of waiting.
  8. Pick one global order and use it for every cook, not the order that feels natural.
  9. A single cook breaking the order can freeze everyone. Make the order the same.
  10. Set a global order between the pestle and the knife, then use it for both.
  11. Put down any tool that doesn't guard a dish you touch.
  12. Each cook holds only the guards of the dishes they touch.

And more, 18 levels in all.