ADR-0023: Outbox lease claim and per-handler checkpoints
- Status: accepted
- Date: 2026-10-08
- Amended: 2026-10-11 — the lease is also the fence on every bookkeeping write, and every sibling handler runs on each attempt (see Amendment below)
- Refines: ADR-0018 (the transactional outbox stays; this decides how the processor dispatches, retries and fails). It does not supersede it.
Context
Section titled “Context”ADR-0018 moved domain-event handlers behind an outbox: OutboxWriteInterceptor writes a row in the
aggregate's transaction and OutboxProcessor dispatches it afterwards. The first processor claimed a
batch with FOR UPDATE SKIP LOCKED, ran every handler of every message inside that claim transaction,
and saved all the bookkeeping with one SaveChanges at the end. Four forces made that shape wrong, each
observed in the audit:
- The retrying execution strategy re-runs its delegate.
AddPersistenceenablesEnableRetryOnFailure, so the delegate passed toExecuteAsyncis re-executed after a transient failure. With dispatch inside it, a blip at the finalSaveChangesorCommitrolled every row back to pending, re-claimed the same rows and ran every handler again, e-mail included; theCompletedHandlerscredits were lost with the rollback. Savepoints inside the transaction would not have changed this — only taking dispatch out of the retried delegate does. - One transaction per batch means one failure per batch. Deserialization and the
CompletedHandlersparse ran before the per-messagetry, so an unreadable row (an event type removed in a deploy, a payload that no longer parses) threw out of the whole batch every five seconds: the dead-letter counter never fired, and the other messages' handlers re-ran each time. The row locks and the connection were also held for the whole batch's handler I/O. AmbientTenantwas reset per message but saved per batch, so under--use-multitenancya handler's writes and their cascaded outbox rows were stamped with no tenant.- Cancellation was classified by exception type.
catch (Exception e) when (e is not OperationCanceledException)let a handler'sHttpClienttimeout — anOperationCanceledExceptiontoo — escape the processor as if the host were stopping: no attempt counted, no retry, and theBackgroundServicestopped the host.
Smaller things rode along: the knobs were constants (5 s poll, 20 rows, five attempts, 30 s base delay — dead letter after about 7.5 minutes, a short window for a real downstream outage), the loop slept after a full batch (~4 messages/s), spans never carried an error status, and there was no backlog signal.
Constraints: Result over exceptions for expected failures; Infrastructure and Persistence stay peers;
no new package; the OutboxMessages schema does not change (the shipped InitialCreate and
MigrationDriftTests stay as they are); the claim SQL and its partial index, the persisted
traceparent and the "counters are incremented only from committed events" property are kept.
Decision
Section titled “Decision”- Lease-based claim; dispatch outside the transaction. The claim transaction is unchanged SQL
(
FOR UPDATE SKIP LOCKED … ORDER BY "OccurredAtUtc" LIMIT BatchSize) and does one more thing: it stampsNextAttemptAtUtc = now + LeaseDurationon the claimed rows and commits. To every other processor a leased row is simply "not due" — the predicate the claim and the partial index already use, so no new column. The transaction lasts milliseconds; the retried delegate is pure database work and re-running it is harmless. - One DI scope and one bookkeeping save per message. Each message is dispatched in its own scope
(fresh
DbContext, fresh scoped handlers) and finishes with its ownSaveChanges, inside the ambient tenant scope. A failed message leaves its neighbours untouched. - Per-handler checkpoint.
IDomainEventDispatcher.Dispatch(event, completed, checkpoint, ct)calls back after each handler and before crediting it; the processor's checkpoint serialises the credit and saves the scope's tracked writes with it. "Credited" therefore always means "durable": a handler's write is never lost once it is credited, and a write that cannot be committed leaves its handler uncredited so it runs again. - Unreadable rows are dead-lettered at once, as a
Result. An unknown wire name or a payload /CompletedHandlerscolumn that does not parse is a permanent, expected failure (OutboxErrors.UnknownEventType,OutboxErrors.PoisonPayload): the row is dead-lettered withAttemptsuntouched and the batch goes on. - A dispatch budget and a derived lease. Handlers see a token linked to the host's token and a
timer of
DispatchTimeout.LeaseDurationis not a knob:DispatchTimeout × (BatchSize + 1), the longest a batch can legitimately take plus one slot for bookkeeping. A lease therefore expires only on a crashed or hung process. A graceful stop releases the leases of the rows it did not finish (ExecuteUpdatewithCancellationToken.None, best effort), so a rolling deploy does not delay them. - Cancellation is classified by token, not type, at every background-service and decorator site:
when (e is not OperationCanceledException || !stoppingToken.IsCancellationRequested), keyed on the host's token, never on the linked dispatch token. A downstream timeout and a budget expiry are retryable failures; only a stopping host is not.ConventionTestsscanssrc/andtools/for the type-only form. - Validated
OutboxOptions(Outboxsection):PollInterval5 s,BatchSize20,MaxAttempts8,BaseRetryDelay30 s,MaxRetryDelay15 min,DispatchTimeout30 s — DataAnnotations on the POCO, the host runs them at boot, the cross-field rule (MaxRetryDelay ≥ BaseRetryDelay) sits next to the binding, exactly asRetentionOptions. The back-off is a pure, capped, overflow-safe function (OutboxBackoff): 30 s, 1 m, 2 m, 4 m, 8 m, 15 m, 15 m, dead letter on the eighth failure (≈45 min). - Drain loop. A poll that claimed a full batch claims again at once; the loop sleeps only after a short one. A failing claim counts as zero and sleeps, so a broken database cannot hot-loop it.
- Telemetry. The span starts before deserialization, carries
trellis.outbox.message_id,trellis.outbox.event_type,trellis.outbox.attemptandtrellis.outbox.outcome(processed/retry_scheduled/dead_lettered), and getsSetStatus(Error)+AddExceptionon failure and dead letter. The meter gainstrellis.outbox.retries,trellis.outbox.dispatch.durationand two gauges sampled by the processor on each poll (trellis.outbox.pending,trellis.outbox.oldest_pending_age); every instrument declares its unit, and the metric attributeevent.typebecomestrellis.outbox.event_type(a reserved semantic-convention namespace is not ours to use). A retry logs a Warning with the exception; a dead letter stays Critical. - Re-drive is a documented SQL runbook (
docs/deployment.md, "Outbox operations"), not an admin endpoint: an endpoint needs a permission, an OpenAPI entry, contract tests and a UI decision, and the table is the outbox's console anyway.
Consequences
Section titled “Consequences”- No migration.1 The lease reuses
NextAttemptAtUtc; only its documentation changed. - Delivery stays at-least-once, now with a tighter bound. The residual duplicate-dispatch risks are a
process that crashes after a handler's side effect and before its checkpoint, and a handler that
ignores its token past the lease. After a crash, in-flight rows reappear within
LeaseDuration(10.5 min with the defaults); shortenBatchSizeorDispatchTimeoutto shorten it. - A failed bookkeeping save costs one message, not the batch: when the database fails a message's failure, dead-letter or Processed save, the processor logs a Warning and goes on with the next message while that row keeps its lease (dispatched again when it expires, credited handlers skipped), anything else that ends a batch early releases the unreached rows' leases before it propagates, and since the budget bounds handlers rather than saves, a slow database can push a batch past its lease as well.
- Handler rules (
.claude/rules/application.md): handlers run at-least-once; honour theCancellationToken; do unit-of-work writes before external side effects (a write that fails at the checkpoint un-credits the handler, so an e-mail sent before it would go out again); never callSaveChangesyourself — the outbox commits per handler. - Operators notice: the dead-letter window grows from ≈7.5 to ≈45 minutes; the
Outboxsection is the place to tune it; dashboards built onevent.typemove totrellis.outbox.event_type; a stalled drain is visible astrellis.outbox.oldest_pending_ageclimbing long before anything dead-letters. - Tests:
OutboxRetryTests(throwing, timing out, writing and unreadable messages; two concurrent processors; a stopping host; the gauges and the spans),OutboxBackoffTests,OutboxOptionsTests, the dispatcher checkpoint tests, and the writing-handler case inTenancyTestson the all-options leg.
Amendment (2026-10-11): the lease is also the fence; siblings run on every attempt
Section titled “Amendment (2026-10-11): the lease is also the fence; siblings run on every attempt”Decision 1 made a claimed row invisible to the other processors by stamping NextAttemptAtUtc, and
the consequences admitted the residue: a processor that outlived its lease — a handler ignoring its
token, a database slower than the budget — kept writing. Its failure path ran context.Update(message)
and rewrote every column, so a stale processor could set a row the next claimant had finished back to
Pending, or dead-letter the new owner's row. The fix reuses the same column once more:
NextAttemptAtUtcis theOutboxMessageconcurrency token. Every bookkeeping save of the message scope — each checkpoint, Processed, the back-off, the dead letter — is anUPDATE … WHERE "Id" = … AND "NextAttemptAtUtc" = <the lease this processor stamped>. A re-claim stamps a later lease, a finished row keeps the new owner's, a re-drive stampsnow(): none equals the stale lease, so the save affects no row and EF raisesDbUpdateConcurrencyExceptionfor the outbox entry. The processor books that aslease_lost(the span's outcome and Error status, thetrellis.outbox.dispatch.durationoutcome, Warning 1303), drops its tracker and writes nothing; the credits it checkpointed before losing the lease were saved under it and stand. A handler's own stale row at a checkpoint is not a lost lease — only an exception entry of typeOutboxMessagecounts; anything else is that attempt's ordinary, retryable failure.- A batch stops short of its lease. Before each message the processor checks that a whole
DispatchTimeoutstill fits beforeleaseUntil; if not, the rest of the batch is handed back (Warning 1306, the same release a stopping host does) instead of being dispatched into a certainlease_lost. - Signals follow the save. The
trellis.outbox.retriesandtrellis.outbox.dead_letteredincrements and the retry (Warning 1301) and dead-letter (Critical 1302) lines are emitted after the row was written, not before: a save that fails — the database gone, the lease lost — announces nothing. Every outbox line carries an event id in the 1300 range (OutboxLog), and the span'serror.typeis the failing exception's full type name, or theOutbox.*code of an unreadable row. - Claim order is
ORDER BY "OccurredAtUtc", "Id", and the rows of one save are written one microsecond apart, so an Issued event is claimed before the Paid event of the same save and two replicas see one order. - The model snapshot changed; the table did not. See the footnote under Consequences.
The residual duplicate-dispatch window is as before (a crash between a side effect and its checkpoint,
a handler past the lease); what changed is that the stale processor can no longer corrupt the row.
OutboxRetryTests proves it: a processor that wakes up after its lease was taken over fails to book
either a back-off or a dead letter over the Processed row.
Decision 3, refined: every sibling handler runs on each attempt; only the failed ones are retried.
The dispatcher used to stop at the first handler that threw, so an SMTP outage behind
SendReceiptOnInvoicePaid held RecordMetricsOnInvoicePaid hostage through 45 minutes of back-off.
IDomainEventDispatcher.Dispatch(event, completed, checkpoint, onHandlerFailed, ct) now catches each
handler's own exception — keyed on the token, when (e is not OperationCanceledException || !cancellationToken.IsCancellationRequested) — reports it through onHandlerFailed, leaves the handler
uncredited and goes on to the next one. The outbox's hook clears the message scope's tracker and
re-attaches the message, so the next checkpoint commits its own handler's writes alone (nothing of the
failed handler rides along), and logs the handler by name (Warning 1304). After the last handler a single
failure is rethrown as it was (error.type and the retry line keep their meaning) and several are one
AggregateException naming the handlers, which the row's Error then shows. What is not isolated: a
checkpoint failure (nothing after it could be committed either) and a cancellation of the dispatch token
(a budget expiry or a stopping host), which ends the dispatch at once — a handler that times out still
consumes the shared budget. Attempts still counts one per claim, however many handlers failed in it.
Footnotes
Section titled “Footnotes”-
Amended 2026-10-11: the concurrency-token annotation of the amendment below changed the EF model snapshot, so the shipped
InitialCreatewas regenerated (pre-release convention) with theOutboxMessagesDDL unchanged. A database already migrated with the earlierInitialCreateadds a migration instead (migrations add OutboxLeaseFence), which comes out empty apart from the snapshot. ↩
The documentation of Trellis.Templates 0.2.0 · release notes