Sistema de Chat

Mensajería estilo WhatsApp / Slack

Escrito por Darjus Kazakevic · revisado el

Open sockets in, a sequence number per chat, and a sync on reconnect.

Users send text messages in one-to-one chats and in groups of up to 500 people. They see delivery and read receipts and who is online. A user's phone and laptop both show the full history, including messages that arrived while they were offline.

An online recipient should get a message in under 500 ms. Inside one chat, every member must see the same order. Once the sender sees a tick, the message must never be lost. Presence (online status) may be a few seconds late.

En esencia — Keep a socket open to every online device, number each chat's messages on the server, and let any device catch up by asking for everything after its last number.

Cuándo recurrir a él

Recurre a él cuando

  • Users talk in real time, and every member must see the same order
  • Devices go offline often and must catch up without losing a message
  • You push live updates to many open clients, like a game or a shared doc

Reconsidéralo cuando

  • Updates can wait a minute: polling or email costs far less
  • You need one order across all chats, which a per-chat counter cannot give
  • A group has millions of members: that is a broadcast, so design a feed

The components

WebSocket gateways (uses real-time delivery)
Stateful servers that each hold hundreds of thousands of open connections. A session registry in Redis maps each user's devices to the gateway that holds them.
Message service (uses unique ID generation)
Takes a send, gives it the next sequence number for that chat, stores it, and only then fans it out. The sequence is per chat, so chats never wait for each other.
Message store (uses database sharding)
Partitioned by chat ID and sorted by sequence, so "the latest 50" and "everything after 42" are each one range read. Discord stores its billions of messages in a similar way: by channel and time window.
Fan-out and offline delivery (uses pub/sub)
A pub/sub channel per chat routes a stored message to the gateways that hold its members. A member with no open socket gets a push notification and syncs when the app opens.
Presence service
Each device sends a heartbeat; Redis keeps a key per user with a 60-second TTL. Status changes go only to users who have that chat open.

Key decisions

Order by a server sequence, not by clocks
Phone clocks drift, and two servers disagree by milliseconds. Give each chat a counter that only the message service increments, and sort by it. Every device then shows the same order.
Store the message before you confirm it
Write the message to durable storage before you acknowledge the sender. The tick then means "stored", so a crashed gateway can lose a connection but never a message.
Sync by sequence on reconnect
Each device remembers the last sequence it saw per chat. On reconnect it asks for everything after it. One query covers offline users, new devices and dropped sockets.
Make sends idempotent with a client ID
Mobile networks lose acknowledgements, so clients resend. The client attaches a random message ID; the server keeps a unique key on it and returns the first sequence on a retry.

Errores habituales

  • Ordering by the sender's timestamp. A phone with a wrong clock puts its messages in the past, and members see different orders.
  • Acknowledging before the write is durable. The sender sees a tick, the gateway crashes, and the message is gone.
  • Restarting every gateway at once. Ten million clients reconnect in the same second and overload the sync service. Drain gateways one by one, and make clients reconnect with backoff and jitter.
  • Sending every presence change to every contact. A user with 1,000 contacts on a weak network creates a storm of updates. Send presence only to open chats, and wait a few seconds before announcing "offline".