Real-time systems
Connections that drop, messages that arrive twice, and state that has to stay in sync anyway.
Design the reconnect first: sequence numbers, replay and snapshots
Real-time bugs live in the seconds after a connection comes back. A resume protocol built on sequence numbers, idempotent replay and snapshots.
Snippets
Gap-free per-stream sequence numbers in PostgreSQL
Bump a counter row per stream in the same transaction as the change; the row lock makes sequence order equal commit order, with no gaps.
Handle duplicate and missing WebSocket messages on the client
Drop sequence numbers at or below the last applied, resync on a gap or epoch change, and let snapshots replace state instead of applying to it.
Resume a WebSocket stream after reconnect without losing messages
On reconnect, subscribe with buffering first, then replay the gap or send a snapshot, then drain the buffer. The reverse order loses messages.