Packages

Framework-agnostic Elixir CDC consumer for Postgres logical replication (pgoutput) with zero-loss delivery to a pluggable sink — exactly-once for transactional sinks, at-least-once (duplicate-bounded) for non-transactional sinks.

Current section

Files

Jump to
replicant usage-rules.md
Raw

usage-rules.md

<!--
SPDX-FileCopyrightText: © 2026 BaseLabs Holdings LLC

SPDX-License-Identifier: MIT
-->

# Replicant usage rules

Apply these rules when generating or reviewing a Replicant pipeline or sink.
Read [compatibility](documentation/topics/about/compatibility.md) 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](documentation/topics/delivery/snapshot-coverage.md).
  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](documentation/topics/delivery/invariants.md).

```elixir
# 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.

```elixir
# 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](documentation/topics/delivery/invariants.md)
  when describing duplication. Plan for a replay after reconnect.
- Treat a halt as an operator decision. Diagnose it with the
  [recovery guide](documentation/how-to/recover.md) and restart only once the
  checkpoint and source agree.
- Set `origin:` explicitly when stream origin matters. Check the
  [decoder defaults](documentation/topics/about/decoders.md#filter-by-replication-origin)
  and [snapshot coverage](documentation/topics/delivery/snapshot-coverage.md#replication-origins).

## 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](https://hexdocs.pm/replicant/Replicant.Telemetry.html).

## Monitor receive silence

- Set `receive_timeout` in milliseconds to opt in to silent stream recovery.
  Check the [recovery guide](documentation/how-to/recover.md#recover-a-silent-stream)
  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.