Skip to main content

Module replay_guard

Module replay_guard 

Source
Expand description

Protection against JetStream replaying messages the archiver already wrote.

JetStream keeps a durable consumer’s progress (delivered and ack floor sequences) in a small state file next to the stream. The server replaces that file by writing a temporary file and renaming it, without an fsync. After a power cut the file can be left empty or missing. The server then recovers the consumer with no progress, and with deliver policy all it delivers the whole stream again. A consumer whose state file is corrupt is dropped at recovery, and the archiver creates it again, also from the start of the stream.

The archiver therefore keeps its own checkpoint per stream and subject: the highest stream sequence such that every message on the subject up to it has been acked, which means it is inside a closed and fsynced Parquet file. The value is the consumer’s ack floor as reported by the server, saved with an fsync of the file and its directory, so it survives the same power cut. A message at or below the checkpoint is acked without being written again.

The checkpoint is only trusted while it clearly belongs to the same stream: the stream name, its creation time and the subject must match, and the stream’s last sequence must not be below the checkpoint (a recreated or rewound stream reuses sequence numbers). Otherwise it is ignored and every message is written, as without the guard.

Structs§

Checkpoint
Saved archiver progress for one stream and subject.
ReplayGuard
Skips messages at or below a trusted floor and counts them for logging.
SkipRun
A finished run of skipped messages, for one log line.

Functions§

checkpoint_path
Path of the checkpoint file for one stream and subject.
file_token 🔒
Turns a stream name or subject into a safe file name part.
load
Reads a checkpoint file.
save
Writes a checkpoint durably.
trusted_floor
Decides whether a saved checkpoint can be used for the stream as it is now.