Skip to content

Chat after disconnection

10 min reading

A recovery design for reconnect, duplicates, ordering and offline synchronization

Page details

System Design · Compose complete systems

10 min reading · 25 min with practice

Sources

Published · Last edited:

Prerequisite: Chat: durable messages and live delivery. For basics, use the independent System Design foundations.

More
20px

Hypothetical scenario. A gateway rollout disconnects Northwind chat devices while new messages continue arriving. Dev offers every returning device its whole backlog immediately. Priya follows the recovery traffic into the live path and asks whether membership is still valid for each reader.

Dev: “Each device has a saved sequence position.”

Maya: “That tells us where it left off, not whether it may still read the conversation.”

Connect bounded catch-up to live delivery using the accepted message identity. Define cursor meaning, permission checks and a separate recovery-capacity budget. The reconnect cohort is a selected stress interval, not a revised claim about ordinary chat users.

What does the returning device’s cursor establish?

The conversation log is authoritative. A device supplies its last confirmed sequence and requests a bounded later range. The server authenticates the device’s principal and checks the membership rule for that conversation and requested history. A valid sequence does not grant access after revocation.

Northwind chooses current membership checks for new catch-up reads in this example. Whether a newly joined member sees earlier history is a separate product rule. The service must also state what deletion means for already delivered copies. It can deny a future read but cannot erase a message a former member already saw.

Device progress advances only after the defined application acknowledgement. A gateway attempting a socket write does not establish that progress. If the acknowledgement is lost, the device may receive the same accepted message again. The device uses message identity to avoid duplicate display without suppressing two genuinely distinct sends with identical text.

Connect the catch-up boundary to live delivery

A reconnect can race with new messages. Establish a durable history boundary for the catch-up range and preserve notification or log coverage after it. Do not read the old tail, then subscribe later and leave an unobserved gap between those actions. The selected protocol must explain how overlaps become recognizable duplicates and how missing live notifications are repaired.

One possible application protocol captures a log position, catches up through it with bounded pages and then continues from that position while checking later history as needed. Its precise subscription and buffering behavior depends on the implementation. The invariant is coverage: every accepted permitted message after the device cursor remains recoverable, even when a notification is lost.

Catch-up and live delivery share one durable sequenceThe device last confirmed sequence 117. History is durable through 120 when catch-up begins. An authorized read retrieves 118 through 120. New sequence 121 remains covered by the continued log or notification protocol. The device advances its cursor on its defined acknowledgement, not on a gateway write attempt.Device cursor 117Reauthorize readCatch up 118–120Bounded historyNew message 121Coverage continuesAcknowledge durable position; replay recognizable repeats
Sequence coverage and permission both survive a socket replacement.

Close the gap between catch-up and live messages

Hypothetical scenario. A returning device has cursor 100. Register a live subscription and establish durable high-water mark H = 120 under a protocol that buffers or can replay messages around that boundary. Fetch authorized history 101 through 120; then release buffered events above H and deduplicate by conversation sequence.

Close the gap between catch-up and live messages — selected design evidence
Sync fieldExampleObligation
Client cursor100Last incorporated accepted sequence
High-water mark120Catch-up / live boundary
Result identityconversation + sequenceDeduplicate overlap
Retention floorearliest recoverable cursorRequire resync below floor
The boundary explains every accepted permitted message across catch-up and live delivery.The boundary explains every accepted permitted message across catch-up and live delivery. Device to Sync authority: 1 · cursor 100 + authority; Sync authority to Live buffer: 2 · establish H120; Sync authority to Device: 3 · history through H; Live buffer to Device: 4 · events above H.DeviceSync authorityLive buffer1 · cursor 100 + authority2 · establish H1203 · history through H4 · events above H
The boundary explains every accepted permitted message across catch-up and live delivery. Enlarge the diagram to inspect its labels; the surrounding records and explanation give the text alternative.

Fetching history and only afterward opening a live connection leaves a gap for messages accepted between those actions. Subscribing first without a boundary can instead duplicate messages. Duplicates are acceptable only when message identity and cursor advancement make them harmless; silent gaps are not.

Membership can change during catch-up. Apply the chosen permission rule to historical pages and queued live events, including revocation before disclosure. If cursor 100 is older than retained history, return an explicit resynchronization requirement rather than pretending the first available message is complete coverage. Admit reconnect work separately from active sends so a recovery storm cannot starve current conversation traffic.

Derive pages per device before adding them

Hypothetical worked example. Choose one thousand reconnecting devices, each missing forty messages. The payload transfer count is 1,000 × 40 = 40,000 messages. At five hundred bytes each, payload is 40,000 × 500 = 20,000,000 bytes, or twenty decimal MB, before framing and metadata.

Choose an authorized page limit of one hundred messages per device request. Each device needs one page because its forty messages fit within that limit. The wave therefore requires 1,000 × 1 = 1,000 page requests. Dividing the global forty-thousand-message total by one hundred would incorrectly assume messages from different device requests can be packed into one authorized page.

For a selected hundred-second recovery target, required average successful page rate is 1,000 ÷ 100 = 10 pages per second. Payload transfer rate is 20 ÷ 100 = 0.2 MB per second under these assumptions. Membership lookup, conversation routing and response envelopes remain additional work; message bytes alone cannot establish recovery capacity.

If live work leaves only five successful catch-up pages per second, the same wave takes 1,000 ÷ 5 = 200 seconds. A larger queue cannot meet the earlier target. Increase justified useful capacity, reduce admitted competing work or revise the target explicitly. Inconclusive permission checks do not count as completed pages.

A large conversation concentrates another dimension

Per-conversation ordering can concentrate accepted writes on one owner, while fan-out concentrates delivery work across many recipient devices. These are related but distinct bottlenecks. More unrelated conversation partitions do not divide the ordering requirement of one hot conversation automatically.

Keep delivery workers bounded and isolate large-conversation fan-out where the workload justifies it. A slow device should accumulate recoverable lag rather than retain an unlimited gateway buffer. Define retention and the response when its cursor falls before retained history. Resynchronization may be required; silently declaring the missing range delivered would invent completion.

Recheck membership and attachment access

A live connection established before revocation is not indefinite authority. Decide when current permission is evaluated for subsequent events and catch-up pages, and how cached membership becomes ineligible. During an authorization outage, return the permitted unavailable result when eligibility cannot be established.

If attachments are introduced later, they need their own download authorization and retention design. Embedding a permanent public object URL in a protected conversation would create a separate disclosure path. The baseline excludes attachments precisely so that text-history correctness does not pretend to solve object access.

In the wild: startup demand includes metadata

Slack’s Flannel report describes lazy metadata loading and cached startup information. It supports examining startup pressure separately from steady message delivery. The report’s discussion of later event-fan-out work must not be read as proof that every proposed extension was already deployed in that account.

The old cursor bypasses a revoked membership

Hypothetical failure scenario. A gateway misses a membership invalidation. A former member reconnects with a valid old cursor. The catch-up endpoint verifies the cursor format but returns conversation history before checking current eligibility. The sequence is correct and the disclosure is unauthorized.

Make the permission decision part of the read boundary. Preserve enough scoped evidence to investigate denials and stale membership without copying private message bodies into unrestricted logs. A successful reconnect metric must not count an unauthorized page as a useful outcome.

Use recovery as a controlled workload

Measure accepted sends, pending deliveries, cursor lag, catch-up age, membership failures and resource saturation separately. Reserve a declared recovery share and observe successful permitted pages while increasing admission. Randomized reconnect timing can spread arrivals but does not reduce the total missing history or make the permission decision optional.

This design does not prove arbitrary-volume capacity, global message order or erasure from offline clients. It supplies a recoverable text-history contract under stated authority and retention assumptions. The next case changes the product to a feed, where the central trade becomes when to materialize each reader’s candidate list.

Challenge the proposed mechanism

The transition carries both cursor evidence and current disclosure authority.The transition carries both cursor evidence and current disclosure authority. Cursor retained to Catch up to H: authorized page range; Catch up to H to Live above H: deduplicate boundary overlap.Cursor retainedCatch up to HLive above Hauthorized page rangededuplicate boundary overlap
The transition carries both cursor evidence and current disclosure authority. Enlarge the diagram to inspect its labels; the surrounding records and explanation give the text alternative.

A device is revoked after the history request starts but before a buffered message is sent. What decides disclosure?

Reveal the design reasoning

The declared membership/revocation contract must govern both paths. Recheck or enforce the relevant permission version before release; a previously opened socket is not perpetual authority. Keep retention and permission gaps explicit.

Decision and reconsideration: Use a replayable high-water-mark handoff. Bound reconnect work and require resnapshot when the cursor leaves the retention contract.

Defend catch-up

  1. One thousand devices each miss forty messages. With a hundred-message page limit, are four hundred requests enough?

    Respect per-device requests

    No. Each device needs its own authorized page. The stated cohort requires one thousand requests; unused capacity in one device’s page cannot serve another device.

  2. A device’s cursor is valid but membership was revoked. Should the page be returned?

    Check present authority

    No under this chapter’s current-membership rule. A cursor identifies history, not permission. Return the policy’s denial or unavailable outcome instead.

  3. A hot conversation dominates fan-out. Do additional log partitions solve it?

    Separate ordering and delivery

    Not automatically. Preserve its accepted order while bounding recipient work and recovery. Unrelated partitions do not split one conversation’s authority by themselves.

Chat recovery has separated source history, derived observations and bounded reading. Page 45 uses those distinctions for a supplier feed, where publication-time copies and read-time assembly move the work to different places.

Northwind decision log

Recover permitted history from durable positions with gap-free coverage.
Budget pages per device and preserve current membership checks.
Next, choose feed materialization and pagination semantics.

Go deeper: ordering and recovery assumptions