Sub topic 10
Replay IDs: Resuming from a Known Position
This chapter assumes you can already open a Subscribe stream, decode Avro payloads, and use num_requested for flow control, as covered in earlier chapters.
What a replay ID is
Every event the server delivers carries a replay ID. A replay ID is an opaque byte value that marks the event's position in the topic. You can't read meaning into it or do arithmetic on it. Treat it as a bookmark and store it exactly as received.
In the Python client, each ConsumerEvent has a replay_id field next to the event payload. Each FetchResponse also has a latest_replay_id. That field matters when a stream delivers no events for a while, because it lets you advance your bookmark anyway.
Replay presets
The first FetchRequest on a stream chooses where to start, using replay_preset:
| Preset | Starts at | Typical use |
|---|---|---|
| `LATEST` | Only events published after you subscribe | A new subscriber that doesn't need history |
| `EARLIEST` | The oldest event still retained | Backfill, or rebuilding state |
| `CUSTOM` | The event after the replay ID you supply | Resuming after a stop or disconnect |
With CUSTOM, you must also set replay_id on the request. The server then sends events published after that position. The event with that ID isn't sent again.
Only the first FetchRequest on a stream uses the preset. Later requests on the same stream only adjust num_requested.
The resume pattern
A reliable subscriber follows this loop:
- On startup, load the last stored replay ID. If none exists, use
LATESTorEARLIEST. - Open the stream with that position.
- Process each event, then save its replay ID.
- After a disconnect, go back to step 1.
A small durable store
The subscribe example from the previous chapter had a comment where persistence belongs. This sketch uses a file. Real systems usually use a database or a key-value store.
import os
STORE_PATH = "replay_id.bin"
def load_replay_id():
if not os.path.exists(STORE_PATH):
return None
with open(STORE_PATH, "rb") as f:
data = f.read()
return data or None
def save_replay_id(replay_id):
tmp_path = STORE_PATH + ".tmp"
with open(tmp_path, "wb") as f:
f.write(replay_id)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, STORE_PATH)The write goes to a temporary file first, then os.replace swaps it in. A crash in the middle of a write can't leave you with a half-written ID.
Pass the stored value into subscribe(stub, metadata, topic_name, replay_id=load_replay_id()). Inside the loop, call save_replay_id(consumer_event.replay_id) after you finish handling each event.
Save after processing, not before
The order of "process" and "save" decides what happens after a crash.
- Save after processing: a crash between the two steps replays one event you already handled. This is at-least-once delivery.
- Save before processing: a crash between the two steps loses an event. This is at-most-once delivery.
Most subscribers choose at-least-once and make their handlers idempotent. An idempotent handler gives the same result if it sees the same event twice. For example, upsert by record ID instead of inserting.
Common errors
- Replay ID too old. Events are retained for a limited time. If your stored ID points to an event that has aged out, the server returns an error instead of resuming. Detect this, then fall back to
EARLIESTorLATESTand reconcile any gap by another means. - Wrong topic. A replay ID is only meaningful for the topic it came from. Store IDs per topic.
- Modified bytes. Don't convert the ID to text or an integer and back. Store the raw bytes.
- Saving only on events. On a quiet topic, also save
latest_replay_idfrom empty responses so your bookmark doesn't age out.
Exercise
Your subscriber saves the replay ID before it writes the event to your database. The process crashes between the two steps. After restart, what happens to that event, and how do you change the code so it isn't lost?
Show answer
The event is lost, because the stored replay ID already points past it and CUSTOM resumes after that position. Move the save to after the database write. The event may then be processed twice after a crash, so make the write idempotent, for example by upserting on the record ID.