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.
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.
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.And more, 20 levels in all.