Replicant usage rules

Copy Markdown View Source

Apply these rules when generating or reviewing a Replicant pipeline or sink. Read compatibility before choosing a source or changing dependencies.

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-level batch_delivery: only for atomic sink-owned batch delivery.
  • Keep snapshots and start intent consistent. Use go_forward_only: true for a new stream from a slot's origin, or choose a snapshot mode. Clear the target table when context.first_for_table? is true.
  • Bind checkpoints to the current replication session. An append sink uses sink_kind/0 and handle_slot_origin/2 to 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.unchanged from update assignments. Consume spilled changes once 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/2 for non-transactional messages and deduplicate their effects by context.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.

Monitor intake and retained WAL

  • Use [:replicant, :connection, :paused] and :resumed for queue pressure. Alert on [:replicant, :connection, :stalled] when queued work stops progressing.
  • Keep processing credits separate from durable acknowledgments. Monitor pg_replication_slots and source disk usage, and set a WAL retention budget using max_slot_wal_keep_size. See telemetry.

Monitor receive silence

  • Set receive_timeout in milliseconds to opt in to silent stream recovery. Check the recovery guide for the effect of the session's wal_sender_timeout.
  • Use [:replicant, :connection, :health] to observe probes, :receive_timeout to observe reconnects, and :slot_release_wait to detect when the previous backend still holds the slot.