ADR-0013: Poison transfer replies and facts are dead-lettered, never retried
Status: Accepted
Context
The transfer-reply consumer (warehouse.inventory.events) and the
transfer-fact consumer (warehouse.fulfillment.events) run the fleet's
at-least-once loop: a handler error is TRANSIENT and the same message is
retried forever with capped backoff, blocking its partition, while a
deterministic problem is logged and committed past (ADR 0003, ADR 0005).
The classification had a hole. InterWarehouseTransfer.MarkAllocated and
MarkUnfulfillable refuse a reply that does not match the transfer it names
(another transfer_line_id, origin, SKU, allocation total, no reservation id
or expiry, a rejection reason outside inventory-storage's closed vocabulary)
with a plain fmt.Errorf. usecases.IsDeterministic only recognised
ErrTransferNotFound and IllegalTransitionError, so such a reply was
treated as transient: one malformed reply wedged a warehouse.inventory.events
partition — and with it every later reply of every transfer keyed there —
until someone moved the group's offset by hand. The facts consumer had the
mirror-image gap: a refused or malformed fact was skipped with only a WARN, so
the evidence was lost.
The same bytes always fail the same way. Retrying them never helps; skipping them silently hides a producer/consumer disagreement about a transfer.
Decision
- The domain names the refusals.
transfer.ErrReplyRefusedis the sentinel for an allocation reply the aggregate refuses;ErrReplyLineMismatch(reply for another line) andErrUnknownRejectionReason(reason outsideORIGIN_SITE_UNKNOWN | INSUFFICIENT_USABLE | IDEMPOTENCY_CONFLICT) wrap it. Every check incheckAllocationReplyandMarkUnfulfillablereturns one of them; the existingErrFactRefusedkeeps covering the fact checks. usecases.IsDeterministicincludes refused replies and facts.usecases.IsRefusedReplysingles out the POISON subset (ErrReplyRefused,ErrFactRefused) from the benign skips (unknown transfer, illegal transition — replays and out-of-order messages).- Both transfer consumers dead-letter poison with the fleet's
<topic>.dlqconvention (already used by the analytics projector, ADR 0009, and by wes-work-planning / order-management for consumed sibling topics):warehouse.inventory.events.dlqandwarehouse.fulfillment.events.dlq. The raw message (key, value, original headers) is written withx-dlq-source-topic,x-dlq-consumer(transfer-reply-consumer/transfer-fact-consumer, because a consumed topic's DLQ may be shared with sibling consumers),x-dlq-errorandx-dlq-failed-at. - Poison is: an undecodable payload of a known type (bad JSON shape,
unparsable
expires_at), a rejection with an unknown reason (checked in the adapter before the saga is touched — the same condition as the domain sentinel, the same treatment), aTransferPickedwithout a positive quantity, and any use-case error matchingIsRefusedReply. - The dead-letter write happens INSIDE the unit of work that holds the
processed_eventsclaim. A failed DLQ write rolls the claim back and is returned as transient, so the loop retries and the poison is never lost; a successful write commits the claim, and the offset moves on. A redelivery after commit is a dedupe no-op (no second dead letter). - Non-CloudEvents bytes and unknown types stay a plain skip (full-type dispatch; they are not addressed to this consumer). Unknown transfers and illegal transitions stay WARN-and-skip: they are expected (a transfer this deployment never approved, a replay) and must not flood the DLQ.
Consequences
- A malformed reply can no longer block the saga: the next reply on the same
partition applies. Proven by
TestTransferReplyPoisonIsDeadLetteredAndTheNextReplyApplies(real Kafka + Postgres: two poison replies ahead of a good one on one partition). - The transfer the poison named keeps its state (
ALLOCATINGfor a refused reply); the stuck-transfer health check (ADR 0007) flags it after its threshold, and the DLQ holds the evidence. - Two new topics exist once the first poison arrives (auto-created by the
DLQ writer). They are operational sinks, not contracts: they carry other
contexts' raw messages and are not added to
apis/asyncapi.yaml. - Re-driving a dead letter needs the producer fixed first and the
processed_eventsrow of that CloudEvents id deleted (the claim committed together with the dead letter).