CSE Roadmap

Common tier · about 3 hours · 12 parts

Part 3 of 12 · 2 min read

Making a stream resumable

Numbering every event so a reconnect can pick up exactly where it left off.

Think of a numbered ticket at a deli counter. If you step away and come back, you do not lose your place or get served twice. You just say your number.

In plain words: giving every event a durable id lets a client that reconnects ask for only what it missed, instead of starting over or risking duplicates.

A worked example. A 50-event stream drops after the client has seen 20. Reconnecting blind (no memory of where it left off) replays from the start: the client ends up with 70 events received, 20 of them duplicates. Reconnecting with the id of the last event it saw (Last-Event-ID) gets exactly the missing 30: 50 received, 0 duplicates, 0 missing.

The key lines. Events are kept in a durable, numbered log and replayed from a requested point:

# from code/mini-stream/sse.py
class EventStore:
    def __init__(self): self.runs = {}
    def append(self, run, type_, data):
        log = self.runs.setdefault(run, []); log.append({"id": len(log) + 1, "type": type_, "data": data}); return len(log)
    def since(self, run, last_id=0): return [e for e in self.runs.get(run, []) if e["id"] > last_id]

Read it as: every event gets the next number in line. Resuming is just asking for everything numbered after the one you last saw. This is the same journal idea as durable execution. A run's history is kept somewhere that survives a dropped connection.

Last-Event-ID, replay

Last-Event-ID is the header a reconnecting client sends naming the last event id it saw; replay is the server resending everything after that id.

Example: client disconnects after id 20, reconnects with Last-Event-ID: 20, server sends ids 21 onward only.