Apply these rules when generating or reviewing a Replicant pipeline or sink. Read compatibility before choosing a source or changing dependencies.
Validate start options
- Use the keys in the
Replicant.start_link/1options table, once per list. Replicant rejects unknown and duplicate keys in its own option lists. - Use booleans for
go_forward_only:. Pass source and checkpoint connection settings as Postgrex keyword lists.
For example, set origin: :none and go_forward_only: true. The misspelling
orign: :none and the string go_forward_only: "true" both cause startup to fail.
Use a durable watermark
- Commit sink-owned effects and their checkpoint together. Skip a transaction at or below the checkpoint. Use the complete commit LSN, once per transaction.
- Pick one checkpoint owner. Put library-owned batching under
checkpoint_store: [batch: ...]. Use top-levelbatch_delivery:only for atomic sink-owned batch delivery. - Keep snapshots and start intent consistent. Use
go_forward_only: truefor a new stream from a slot's origin, or choose a snapshot mode. Clear the target table whencontext.first_for_table?is true. - Bind checkpoints to the current replication session. An append sink uses
sink_kind/0andhandle_slot_origin/2to refuse gaps. See delivery guarantees.
# Correct: both writes happen inside the same destination transaction.
destination_transaction(fn ->
if txn.commit_lsn > (read_checkpoint() || -1) do
apply_changes(txn.changes)
write_checkpoint(txn.commit_lsn)
end
end)
# Incorrect: a crash between these writes can apply the transaction twice.
apply_changes(txn.changes)
write_checkpoint(txn.commit_lsn)Protect row values and identifiers
- Keep row values, logical message payloads and snapshot progress tokens out of diagnostics. Store opaque tokens as required by the snapshot callback.
- Pass validated publication and slot names to
Replicant.start_link/1. Publish an explicit table set and keep every named publication present. Validate or quote identifiers in sink-side SQL. Bind values as parameters. - Keep column names as strings. Omit names in
change.unchangedfrom update assignments. Consume spilledchangesonce inside the sink callback. - Keep tenancy and classification in the consuming application.
# Correct: preserve the unchanged columns already stored in the destination.
updates = Map.drop(change.record, change.unchanged)
# Incorrect: fills an unchanged column with NULL.
updates = Enum.reduce(change.unchanged, change.record, &Map.put(&2, &1, nil))Handle retries as normal delivery
- Use
handle_message/2for non-transactional messages and deduplicate their effects bycontext.lsn. Apply transactional messages with their transaction. - Use the delivery mode's guarantee when describing duplication. Plan for a replay after reconnect.
- Treat a halt as an operator decision. Diagnose it with the recovery guide and restart only once the checkpoint and source agree.
- Set
origin:explicitly when stream origin matters. Check the decoder defaults and snapshot coverage.
Monitor intake and retained WAL
- Use
[:replicant, :connection, :paused]and:resumedfor queue pressure. Alert on[:replicant, :connection, :stalled]when queued work stops progressing. - Keep processing credits separate from durable acknowledgments. Monitor
pg_replication_slotsand source disk usage, and set a WAL retention budget usingmax_slot_wal_keep_size. See telemetry.
Monitor receive silence
- Set
receive_timeoutin milliseconds to opt in to silent stream recovery. Check the recovery guide for the effect of the session'swal_sender_timeout. - Use
[:replicant, :connection, :health]to observe probes,:receive_timeoutto observe reconnects, and:slot_release_waitto detect when the previous backend still holds the slot.