LESSON 03 · Connection lifecycle

Place and recover connections

Route each device to a healthy gateway and track where its live channel resides.

SYSTEM VIEW

The message path

Diagram view
Live routes—
Phone cursor—
Auths—

Watch Ben’s phone and laptop land on different gateways, then see a crashed gateway’s clients reconnect and catch up.

  1. Ben’s phone signs in and asks where to connect. Placement picks a gateway that is healthy, near the device and under its limits for sockets, memory and outbound queues, and answers: use Gateway 1.

    Picks
    healthy, has room, near
    Limits
    sockets, memory, queues

    What changed

    • Ben’s phone signs in
    • Placement picks Gateway 1
  2. Ben’s phone connects to Gateway 1 and registers its user and device. Gateway 1 owns the socket while it stays open, and records phone → Gateway 1 in the Route directory, an in-memory store such as Redis.

    Registers
    user + device
    Route store
    in memory, e.g. Redis

    What changed

    • Socket on Gateway 1
    • Route: phone → Gateway 1
  3. Ben’s laptop signs in too, and Placement sends it to Gateway 2. One account now has two live destinations, phone → Gateway 1 and laptop → Gateway 2, so the Route directory keeps a route per device, not one per user.

    Phone
    Gateway 1
    Laptop
    Gateway 2

    What changed

    • Ben’s laptop
    • Socket on Gateway 2
    • Route: laptop → Gateway 2
  4. Gateway 1 fails, and every socket it held vanishes with it, the phone’s included. Messages 42 and 43 still land in Message history, but the phone can’t see them: its cursor stays at 41, the newest message it has safely applied.

    Lost
    Gateway 1’s sockets
    Kept
    messages 42, 43

    What changed

    • Gateway 1 and its sockets
    • 42, 43 stored
    • Phone offline at cursor 41
  5. Ben’s phone reconnects: it authenticates again, Placement gives it a new gateway, Gateway 2, and the phone registers there. The Route directory now says phone → Gateway 2. The phone’s cursor is still 41.

    New gateway
    Gateway 2
    Cursor
    still 41

    What changed

    • Auth again
    • Phone socket on Gateway 2
    • Route: phone → Gateway 2
  6. Ben’s phone asks for everything after cursor 41, and Message history returns 42 and 43, so the cursor moves to 43. Stored history is the recovery source; the Route directory is only a hint for live delivery.

    Cursor
    41 → 43
    Discord
    resumes from last sequence

    What changed

    • Fetch after 41
    • 42, 43 caught up
    • Cursor 43
  7. Gateway 1 holds 100,000 sockets. Try each: if it dies and all retry at once, Gateway 2 faces 100,000 authentications; jitter, a random delay over 60 s, cuts that to about 1,700 a second; a planned drain stops new placements and moves clients in stages.

    At once
    100,000 auths
    Jitter
    over 60 s

    What changed

    • Ben’s laptop, Message history
    • 100,000 clients on Gateway 1
    • Retry at once, jittered or drained
    Retries
  8. This machinery earns its cost only at scale. A few thousand users can connect through a plain Load balancer that spreads their sockets over Gateway 1 and Gateway 2, with no Placement service picking each gateway.

    Users
    a few thousand
    Instead of
    Placement

    What changed

    • Placement
    • Load balancer
    • A few thousand users

Space plays · ← → step · Home resets

THE IDEA

How it works

A chat gateway is a server that holds open client sockets and forwards messages over them. After authentication, a placement service chooses a healthy gateway with capacity near the device. The device connects and registers its user and device identity. A routing directory, often an in-memory store such as Redis, maps the active device to its gateway; the gateway owns the socket while connected.

One account may have a phone and laptop on different gateways. Do not assume “user online” means there is only one destination. Gateways need limits for connection count, memory, and outbound queues. Drain them during deployment, meaning stop new placements and move existing clients in stages, so clients reconnect gradually rather than all at once.

If a gateway fails, the socket disappears. The client reconnects, authenticates again, receives a new placement, and requests messages since its last acknowledged cursor, the position of the newest message it has safely applied. The routing directory is a hint to live delivery; stored history is the recovery source. Discord’s published gateway protocol has this shape: clients send heartbeats, and after a drop they resume with the last sequence number they saw.

The danger is a reconnect storm. If a gateway with 100,000 sockets dies and every client retries at once, healthy gateways face 100,000 authentications in a moment. Retrying with jitter, a random delay spread here over 60 seconds, brings that to about 1,700 per second. This machinery earns its cost only at scale: a few thousand users can connect through a plain load balancer instead of a placement service.

DECISION POINT

A gateway holding 80,000 sockets restarts. What must happen before clients can safely resume?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Track live routes per device, not only per account.
  • Reconnect plus catch-up repairs a broken channel; a load balancer alone cannot recover messages.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What should a live routing directory identify?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.