Message ordering and deduplication in real-time systems
TCP ordering guarantee, application-level ordering, vector clocks, client timestamps vs server timestamps, idempotency keys, deduplication window, out-of-order display
TCP Orders Bytes, Not Messages
TCP guarantees that bytes arrive in order per connection. But when two clients send messages simultaneously, the server may process them in any order. The server's receipt order becomes the canonical order. Use server-assigned timestamps or sequence numbers as the single source of truth for display order:
// Server: always timestamp on arrival ws.on('message', (data) => { const msg = JSON.parse(data); const stored = { ...msg, serverTs: Date.now(), // authoritative id: generateId(), }; db.messages.insert(stored); broadcastToRoom(msg.room, stored); });
Deduplication on Reconnect
When a client sends a message and disconnects before receiving the ack, it may resend on reconnect, causing duplicates. Fix with idempotency keys:
// Client generates a stable ID per message const msgId = crypto.randomUUID(); socket.emit('chat:send', { id: msgId, text }); // Server deduplicates by ID const seen = new Set(); // or use Redis with TTL socket.on('chat:send', (msg) => { if (seen.has(msg.id)) return; // duplicate, ignore seen.add(msg.id); processMessage(msg); });
