Chat System
Keep connections open, persist a message before claiming it was sent, deliver to online and offline devices, and recover after a gateway fails. Also covers ordering, group fanout, and presence.
Lessons
- 01Define the conversationDecide whose messages move where before selecting a transport or database.
- 02Choose the live transportCompare polling, long polling, server-sent events, and WebSocket against update frequency and connection cost.
- 03Place and recover connectionsRoute each device to a healthy gateway and track where its live channel resides.
- 04Follow one message end to endPersist a message once, then try live delivery or notify an offline recipient.
- 05Order and sync the historyGive each conversation a stable sequence and each device a recovery cursor.
- 06Treat presence as a hintUse leases and heartbeats so brief network gaps do not make status flicker.
- 07Fan out to a small groupWrite one group message, then deliver it to eligible members and devices.
- 08Design the failure pathRecover from broken sockets and stalled workers without losing or duplicating messages.