Realtime Puppeteer — Realtime

Warteg market: 20 levels to trace, fix and design multi-client sync: lost updates, CRDTs (G-Counter, LWW, OR-Set) and OT. A seeded, deterministic simulator.

What you learn in this game

A realtime app connects each phone to the server with a connection that stays open. A WebSocket goes both ways: the phone may send at any time, and so may the server. The server usually broadcasts each change to everyone connected, the sender too.

A lost update happens when two writers read the same value, each change it, then write the result back: the write that arrives later overwrites the other. There are several cures: let the server do the arithmetic, refuse writes made from an old read, or use a data structure that can be merged.

Server-Sent Events are one HTTP response that never finishes: the server writes events one by one (text/event-stream), and the browser reads them with EventSource. It goes one way, server to browser, and reconnects by itself. A WebSocket goes both ways, but reconnecting is the app's job.

Ordering guarantees come in levels: FIFO per connection (built into TCP and WebSocket), causal order (needs vector clocks), and total order (every replica sees the same order, which needs one sequencer or consensus). Use the cheapest one that is enough for what you need.

A CRDT (Conflict-free Replicated Data Type) is a data structure whose copies may each be changed without asking anyone, then merged by a rule that is commutative, associative and idempotent: the merge order, the grouping, and merging again do not change the result. There are families of counters, registers, sets and text.

Shared text is the hardest case: an insert is sent with its position, and that position was counted on the document its writer saw. When two people write at once, the document on the server has already changed by the time the second insert arrives.

What the levels ask

  1. At 10.58 the warteg has just opened. Tia writes down one iced tea on her phone. Tia's phone is connected to the board on the counter laptop over a WebSocket: her slip goes up to the board, the board adds it, then echoes what the board holds to every connected phone. Guess what the board and Tia's notepad say afterwards, and how many slips cross.
  2. Tia writes down one rice, and a second later Mr. Rizal writes down another, long after the first echo reached his phone. Guess what the board ends with and how many slips cross on all the lanes.
  3. At 11.21 the tables are busy. Tia and Mr. Rizal write down one rice almost at once, before either echo reaches the other phone. The app reads the number in the notepad, adds one, then sends the new number to the board, and the board writes the number that comes. Guess what the board says.
  4. On Sunday noon the clean-up crew eats at the warteg, and the neighbourhood head borrows the board for the roll: as soon as the app opens, the phone connects and its name goes on the list. Mr. Jaya, Mr. Tono, then Bagas open the app; soon after, Bagas closes it and goes home. Guess who the board lists as present at the end.
  5. The health post opens in the hall next door, and the mothers wait at the warteg over tea. Mrs. Romlah calls numbers from her phone; the warteg TV, Mrs. Ida's phone and Ms. Sri's phone open an SSE stream (EventSource) from the board. The queue is at number 6, and this morning Mrs. Romlah calls three times. Guess what shows on the TV and how many slips go up from the mothers' phones to the board, besides the request that opens the stream.
  6. The clean-up roll again. This time the board checks each phone with a ping every 10 seconds, and a live browser answers pong by itself. The board closes a connection that has not answered the ping before. Mr. Tono's phone battery dies at second 14, with no chance to say goodbye. When does Mr. Tono's name leave the board?
  7. Tia writes down three orders for table 2 in a row, nasi rames, sweet iced tea, then crackers, each 25 milliseconds after the one before. The warteg wifi is moody: each packet may be held up differently. The board writes the orders in the order it receives them. Guess the rule for the order on the board, one that holds today and on other days.
  8. This month the health post sign-up goes through the board: whoever's slip the board receives first gets the small number. Mrs. Ida at the end of the gang taps Sign up first, then Ms. Sri from inside the warteg, then Mrs. Laras from in front of the hall. Guess the rule that hands out the queue numbers, this month and next.
  9. At 12.51 the spiced beef pot has two portions left. Three customers order spiced beef almost at once: one to Tia, one to Mr. Rizal, one to Mr. Bejo, who delivers from the end of the gang. A waiter promises spiced beef if the app says yes. Choose how the board and the phones keep the spiced beef count so that no customer is promised spiced beef that is not there, today and on other days.
  10. At 13.10 Tia in the back kitchen, without signal, marks sayur asem gone on her phone. Twenty seconds later Mrs. Tarsih at the counter marks it available again: a fresh pot is ready. A minute later Tia's signal returns and her phone sends its write. Choose how to keep that mark so the board and every notepad agree on the latest write by the clock.
  11. This noon three waiters write down rice: Tia three times, Mr. Rizal twice, and Mr. Bejo three times from the end of the gang, where his signal comes and goes. The app sends again every slip the board has not confirmed as soon as the signal returns. Choose how to count so the board and every notepad show the right number of plates.
  12. At 13.48 Tia takes sayur lodeh off the menu board because its pot is empty. At almost the same moment Mrs. Tarsih, who has not seen Tia's write, puts out a fresh pot of lodeh and writes lodeh on the menu. Choose how to keep the menu so lodeh stays on it, today and on other days.

And more, 20 levels in all.