Delivery guarantees and sink invariants

Copy Markdown View Source

This page explains what Replicant guarantees and what a sink must do to preserve those guarantees. Read it before choosing a delivery mode or implementing a sink.

Keep values out of diagnostics

Treat row values, logical-message content and prefix, and snapshot progress tokens as sensitive data. Keep them out of errors, logs and telemetry. Keep column names as strings to avoid creating atoms from an unbounded schema. Replicant reports decode faults as a value-free Replicant.Error, and Replicant.Telemetry validates the allowed keys and shapes at emission. ADR-0003 records the boundary.

Validate identifiers before building SQL

Replicant validates slot, publication and replication-set names with Replicant.Identifier.validate/1 before using them in SQL. A failure reports only the shape of the invalid input. Quote identifiers obtained from PostgreSQL catalogs with server-side format('%I', ...) or quote_ident. Bind data values as parameters.

Keep effects and checkpoints together

The transaction's commit LSN is its watermark. Skip transactions with commit_lsn <= checkpoint. A state mirror applies rows by primary key and commits its effects and checkpoint atomically. Delivery can repeat after a crash. When the sink uses the watermark to deduplicate replay, the resulting effect is once per transaction. ADR-0004 explains why.

ModeContract
Sink-owned per transactionReplicant.Sink.handle_transaction/1 commits the changes and their checkpoint together.
Sink-owned batchbatch_delivery: routes ascending commit LSNs to Replicant.Sink.handle_batch/1. Commit all changes and the final LSN atomically. A callback failure halts and leaves the batch eligible for replay.
Library-owned checkpointcheckpoint_store: persists the checkpoint after the sink reports success. A crash between the two writes can replay one transaction. The sink implements handle_transaction/1; Replicant supplies the checkpoint.
Library-owned batched checkpointNest batch: [max_transactions: n, max_delay_ms: ms] under checkpoint_store:. The sink still receives each transaction individually. A crash or stop can replay up to one batch. The batch flushes on transaction count, elapsed delay or its lag-safety limit.
Snapshot and incremental backfillAn atomic sink-owned chunk is effect-once. With library-owned progress, a crash can replay one chunk. See snapshot coverage and resume.
Spilled streamed transactionThe active delivery mode governs replay. Consume changes once inside the callback, using it as an enumerable. Avoid materializing it or keeping it after the callback returns. See ADR-0005 for scratch storage precautions.

Use either batch_delivery: or checkpoint_store:. A top-level batch: is rejected. See Replicant.start_link/1 for configuration and Replicant.Sink for callback requirements.

Match acknowledgments to the sink

A connection owns the replication slot and answers keepalives while a separate applier calls the sink synchronously. Advancing the delivered frontier requires a durable checkpoint. An idle state mirror can acknowledge WAL that the publication filters out. An append log keeps the acknowledgment at its durable delivered frontier so it can detect an out-of-band slot advance on reconnect.

An append sink declares :append_log from Replicant.Sink.sink_kind/0 and implements Replicant.Sink.handle_slot_origin/2 to compare the slot's origin with its durable log. A quiet append publication can release retained WAL by committing a normal published heartbeat. Bind checkpoints to the actual source with Replicant.Sink.handle_session_identity/2, which runs before checkpoint lookup on each connection.

Deduplicate logical messages at the correct boundary

Set messages: true with pgoutput. Transactional messages ride Replicant.Transaction.messages and inherit the transaction's delivery mode. Non-transactional messages reach Replicant.Sink.handle_message/2 at least once. Key the durable effect on context.lsn. An idempotent replay succeeds without repeating the effect. A one-time nonce rejects a legitimate retry. ADR-0001 records why their guarantees differ.

Preserve unchanged TOAST columns

Use Replicant.Change.unchanged to omit untouched TOAST columns from the destination update. The corresponding value is absent from Replicant.Change.record. Writing NULL in its place destroys data. See the reference pipeline for an upsert that preserves these columns.

Keep the CDC pipeline tenant-blind

Keep tenant scopes, classification and Ash resources in the consuming application. AshReplicant provides an Ash sink for those concerns.