Reliable Recovery for Multiplayer Quiz Channels
Краткое содержание
Pixel’s announcement explains how to safely close multiplayer quiz channels when answers, timers, and reconnects are still in flight. It recommends versioned open, closing, and closed states with an idempotent event log, helping prevent late writes, lost terminal events, and incorrect scores.
DEV Community
Realtime Channel Teardown: Idempotent Recovery for Multiplayer Quiz Sessions
Short answer: treat channel deletion as a recorded state transition, not as a socket operation, and make every reconnect replay the same ordered event log. That decision keeps a multiplayer quiz game correct when a host closes a room while answers, timers, and reconnect requests are still in flight. I build payment and ledger backends, so I am suspicious of the phrase “delete the room.” Deletion sounds like one write, but it actually crosses the room registry, presence state, answer submissions, timer workers, and the audit trail. A player can receive a final question after the host has ended the game; a reconnect can arrive after the delete request but before its broadcast; a retry can execute the same command twice. The failure is not cosmetic. It can change a score. The invariant is a state transition, not a vanished row Give each quiz channel a monotonic version and an explicit lifecycle: open, closing, closed. A delete request first appends ChannelClosing with an idempotency key, then rejects new writes whose observed version is older than the closing version. A separate transition appends ChannelClosed after the server has stopped accepting answers and timer jobs. The registry may eventually remove the room, but the event record remains long enough for reconciliation and support evidence. The exact-once mindset matters here. Network delivery is at-least-once, and a WebRTC data channel can close while a message is buffered; the W3C recommendation describes connection and data-channel state, but it does not turn application messages into a transaction. Your application must attach ordering and replay semantics above the transport. The boundary should be visible in one decision record: Versions are cheap. Choice Useful property Failure it leaves you to handle Fit for a quiz room Hard delete plus broadcast Small initial implementation Lost terminal event, late writes, no audit evidence Poor default Tombstone with ordered log Reconnect can backfill a final state; retries are deduplicated Requires retention and compaction policy Recommended Per-room in-memory state only Low latency in one process Process loss erases truth; horizontal scaling races Only for disposable prototypes Database transaction plus outbox Atomic state and durable publication Outbox worker can lag and needs monitoring Strong foundation There is a catch: a tombstone is not suitable when rooms contain regulated data that must be erased immediately. In that case, keep a minimal hashed audit receipt, apply the applicable retention rule, and make the deletion boundary explicit to compliance reviewers. The receipt should prove that a close command was accepted without preserving answer text, player identifiers, or network metadata beyond the approved window; otherwise an apparently careful audit trail becomes a second copy of the data that the erasure request was meant to remove. For a casual lobby with no durable score, an in-memory room can be a reasonable choice; stick with the simpler model when recovery has no user-visible consequence. How should a multiplayer quiz game handle realtime channel deletion safely? Start with a command whose result is stable under retries. The client sends a channel identifier, the last event version it has seen, and an idempotency key. The service locks the channel record, checks that the key has either been completed or is new, writes the lifecycle event and an outbox row in one transaction, and returns the resulting version. A second request with the same key returns that recorded result rather than creating another close event. Here is the critical path in Go. The repository and outbox interfaces are intentionally generic; the important part is the transaction boundary and the version check. package quiz
import "context"
type Channel struct {
ID string
Version int64
State string
}
type CloseCommand struct {
ChannelID string
Expected int64
IdempotencyKey string
}
type CloseResul...

Powered by Readybot.io