Lesson 7 of 17
38. Design a Chat App (WhatsApp)

38.6 How does the WebSocket gateway work?

3 min read+10 XP

Step 5: Deep dives (10 minutes)

As in Chapter 21: gateways are thin servers that only hold connections, authenticate them, and pass messages to and from the chat service.

  • About 2,000 gateways for 200 million connections at an assumed 100,000 each; event-driven servers (Erlang, Go, Netty) are the usual choice.
  • A session registry maps (user, device) → gateway, written on connect and expired by TTL if heartbeats stop.
  • The chat service sends a recipient's messages to their gateway (through the registry or a pub/sub backplane).
  • Heartbeats detect dead mobile connections; reconnects use backoff with jitter; deploys drain gateways gradually.

Memory code: "Gateways hold sockets, the registry knows who's where."