Side Reads: The Recent-History Buffer

How the Recent-History Buffer Actually Works

The Gateway and Channel Servers part’s account of what owning a channel means gave the Channel Server a buffer of “some amount of history of channels” and left the mechanism alone. With the subscriber list and the re-registration path both established there, it is worth finishing, because the buffer is the piece people most often mis-model — usually as a cache for scrollback, which it is not. That one clause is everything Slack published; the rest of this side read is reasoning.

It is not one log on the host. It hangs off the same per-channel object that already holds the subscriber list and the ordering counter:

CS-47's map:

  C456 ─┬─ subscribers: [GS-3, GS-17, GS-88, …]
        ├─ ordering:    …
        └─ recent:      ring buffer, capacity K
                        ┌──────┬──────┬──────┬──────┬──────┐
                        │ …198 │ …201 │ …205 │      │      │  ← write head
                        └──────┴──────┴──────┴──────┴──────┘
                          oldest                      (wraps: overwrites oldest)

A ring buffer — a fixed-size array plus a write index that wraps around — is the right structure here for three reasons: append is O(1) with no allocation, the memory is bounded at creation and never grows, and eviction is automatic, because writing entry K+1 overwrites entry 1 and nothing has to decide what to drop. Two bounds apply, whichever binds first:

Count — the last K eventsStops one pathologically busy channel from consuming the host's RAM on its own.
Age — a window of secondsThe bound that actually matters, because the buffer's job sets it: covering a Gateway Server reconnect, which takes seconds. There is no reason to hold an hour. Trim lazily on append rather than with a background sweeper — no timers, the work is proportional to traffic, and a quiet channel simply holds harmlessly stale entries, which is cheap precisely because it is quiet.

The catch-up itself is a comparison, not a search. A re-registering Gateway Server does not just say “subscribe me to C456” — it says “subscribe me to C456, and the last thing I saw was ts=…198.” The CS compares that against the oldest entry still in the ring:

GS's last-seen tsWhat the CS doesCost
≥ the oldest buffered eventReplays the events after it down the registrationA read from RAM — microseconds
< the oldest buffered event, or the channel has no buffer at allSignals that it cannot cover the gapThose clients heal over HTTP from MySQL

That second row is the important one, and it is why “how far back it reaches is not published” is a perfectly comfortable thing not to know. The HTTP fallback is always there, so the buffer is safe at any size — including zero. It cannot produce a wrong answer, only extra load. The depth is a tuning knob trading RAM against how often a reconnect degrades into history fetches, which is exactly the kind of number a company changes quarterly and never writes down.

What the Buffer Is Really Protecting

Not the user's experience directly — the Gateway and Channel Servers part’s “past versus future” split is explicit that the socket is never involved in history you navigate to. What it protects against is correlated reconnects.

A Gateway Server holds on the order of 16,000 clients. When one blips and comes back, it re-registers for every channel those clients had open — thousands of channels in a burst. Served from RAM, that is free. Failed, every one of those clients independently hits the Webapp and MySQL for history: the expensive operation the boot payload part is entirely about, arriving all at once from one host's worth of users. So the buffer is a load-shedding device sitting in front of the database, and its contribution to correctness is zero.

That also explains why it is per-channel rather than one global ring on the host. A global LRU would let one busy channel's traffic flush every quiet channel's history out — and the quiet channels are exactly the ones whose catch-up is cheap and therefore worth keeping.

← Back to Gateway and Channel Servers, One WebSocket Per Client