Skip to content

protean outbox

The protean outbox command group manages the transactional outbox. reconcile repairs the crash window described in ADR-0015 for event-sourced aggregates: an event that reached the event store (the durable anchor of the commit) but whose relational outbox row never committed, leaving the event durable yet unpublished. halted, retry and discard find and release a partition key that an abandoned row has halted.

All commands accept a --domain option to specify the domain module path (defaults to the current directory).

Commands

Command Description
protean outbox reconcile Recreate outbox rows for stored events that are missing them
protean outbox halted List the partition keys that an abandoned row halts
protean outbox retry Send a failed or abandoned row back to pending
protean outbox discard Skip a failed or abandoned row for good, keeping it for audit

protean outbox reconcile

Scans the most recent messages of the event store for events that are durable in the store but have no internal-broker outbox row, and creates the missing rows. It repairs every such event in the scan window that is inside the cleanup retention, including one whose row was lost in a crash after which another process committed its own events.

Only events raised by this domain's event-sourced aggregates on the given provider are repaired. For a state-based aggregate the relational row is the state, and on a transactional provider that row commits or rolls back together with its outbox rows. A state-based aggregate's event with no outbox row has no saved change behind it, so it is skipped whether or not the aggregate row exists. On a provider that keeps writes without a transaction, such as Elasticsearch, the aggregate row can be saved while its outbox row is lost. That change did happen, and its event stays unpublished.

The command also skips messages that never get outbox rows in this provider: another domain's events in a shared store, commands, process manager transition events, and events of aggregates on other providers. A rebuilt row carries the same partition_key the original commit would have set. reconcile is the manual form of the automatic startup sweep. Run it after a suspected crash, or from a cron job as a periodic safety net.

# Reconcile the default provider's outbox
protean outbox reconcile --domain=my_domain

# Reconcile a named provider, scanning a wider window
protean outbox reconcile --provider=analytics --limit=5000 --domain=my_domain

Options

Option Description Default
--domain Domain module path . (current directory)
--provider Provider whose outbox to reconcile default
--limit Most recent event-store messages to scan for gaps, skipped messages included. A snapshot row older than the newest message also counts toward the limit 1000

Output

Reconciled 2 outbox row(s) from the event store.

When the outbox already matches the event store (the common, no-crash case) nothing is rewritten:

Nothing to reconcile: no event in the scanned window is missing its outbox row.

The command reads nothing when no event-sourced aggregate is on the provider. Otherwise every run reads the --limit window and looks up the outbox rows of every event-sourced aggregate's event in it. It repairs each of those events that has no internal-broker row and is inside the cleanup retention, wherever the event sits in the window.

A rebuilt row is created when the run saves it, with priority 0. For a sequential_by key, the processor publishes the key's rows in the order they were created within one priority lane. So an event rebuilt behind a later commit is delivered after that commit's events for the same key when both rows are in the same lane. When the later row is in the other lane, either one can be delivered first.

Reconciliation is idempotent. A run skips a row whose (message_id, target_broker) pair is already saved, so running it repeatedly never duplicates a row. The count that reconcile prints covers only the rows this run wrote.

A run is also safe at the same time as the startup sweep or while commands are committing. When two writers try to save the same row, the first writer keeps its copy and the other writer skips its own. A committing command still commits. The exceptions depend on the provider:

  • On SQLite, one transaction writes at a time. A run that waits for a committing command longer than the busy timeout fails with database is locked. The command still commits.
  • Elasticsearch has no unique index. Two writers that save the same row at the same moment can both keep a row.

When a run saves a row before the command does, the row has the default priority, not the priority of the command's request.

A published event also gets back one row per broker in [outbox].external_brokers, with the same partition_key as its internal row. Reconciliation reads the event's current published flag and the current external_brokers list, because neither is stored with the event. An event counts as missing only when its internal-broker row is missing, since all of an event's rows are saved in one commit. So a broker added to external_brokers later is not back-filled for events that still have their internal row. The count in the output includes the external rows.

Reconciliation skips events older than the shorter of [outbox.cleanup].published_retention_hours (default 168) and [outbox.cleanup].abandoned_retention_hours (default 720). Cleanup deletes rows older than these retentions, except abandoned rows that have a partition key. A deleted row looks the same as one lost in a crash, so rebuilding a deleted row would send its event again. An event lost in a crash and left unrepaired for longer than that retention is not repaired. An event with no stored time is also skipped, because its age is unknown.

The cutoff uses the retentions configured when the run starts. Cleanup deleted rows under the retentions configured when it ran. If you raise the shorter retention after cleanup ran with a lower value, the next run rebuilds the rows cleanup deleted in between, and those events are published again.

A row the reconciler abandons records last_processed_at, so cleanup_old_abandoned() removes it once abandoned_retention_hours has passed. A row abandoned because its partition key is invalid carries last_error["reason"] of "Invalid partition key".

protean outbox halted

Lists the partition keys whose earliest row that still holds the key is abandoned. Rows that are pending, processing, failed or abandoned hold the key. published and discarded rows do not, so they are skipped. A partition key is the value set by a handler's sequential_by, and the processor publishes a key's rows in commit order. Rows are grouped by target broker, stream category, partition key and priority lane, the same groups the processor uses, so a key is listed exactly when its earliest holding row is abandoned. The listing includes a key with no rows behind it yet, because the processor will hold any later row. A key whose earliest holding row is pending, processing or failed also has its later rows held, but it is not listed, because the processor will still publish or retry that row.

protean outbox halted --domain=my_domain
protean outbox halted --json --domain=my_domain

Options

Option Description Default
--domain Domain module path . (current directory)
--provider Provider whose outbox to inspect default
--json Print the result envelope as JSON off

Output

1 halted group(s) of broker, stream category, partition key and lane:
- default shop::order key=cust-42 lane=primary: head row 6f1c2a9e-... is abandoned (broker rejected the payload), 3 row(s) waiting

Each line names the target broker, the stream category, the partition key and the lane, then the abandoned row at the head of the key, the message from its last_error, and how many later rows of the key are waiting. There is one line per group, so a key with rows for two brokers or in both lanes appears once for each. With no halted key it prints No partition key is halted. and exits 0. The scan reads the abandoned rows a page at a time, so a row whose status changes while it runs can be missed or listed twice.

Under --json, data.halted holds one object per halted group. The last_error below is shortened. A real last_error also holds the traceback, failed_at and retry_count of the last failure. It prints across several lines:

{
  "data": {
    "halted": [
      {
        "head_id": "6f1c2a9e-...",
        "lane": "primary",
        "last_error": {
          "message": "broker rejected the payload",
          "reason": "Max retries exceeded"
        },
        "partition_key": "cust-42",
        "status": "abandoned",
        "stream_category": "shop::order",
        "target_broker": "default",
        "waiting": 3
      }
    ]
  },
  "diagnostics": [],
  "status": "pass",
  "version": "0.1.0"
}

lane is primary or backfill. last_error is the row's stored error, or null when it has none.

protean outbox retry

Sends a failed or abandoned row back to pending with its retry count at zero and no retry delay, so it gets the full number of attempts again. A row whose max_retries is 0 gets one attempt. The processor can then claim the row, in the same priority and commit order as any other row, and still only after the earlier rows of its key. The later rows of its key publish after it.

protean outbox retry 6f1c2a9e-... --domain=my_domain

Arguments and options

Argument / Option Description Default
ROW_ID Id of the outbox row, as shown by halted required
--domain Domain module path . (current directory)
--provider Provider whose outbox holds the row default

Output

Outbox row 6f1c2a9e-... is pending and will be published again.

protean outbox discard

Skips a failed or abandoned row for good. The row is never published, so its event is dropped for that broker, and the later rows of its key publish without it. The row stays in the outbox with the discarded status. Its last_error keeps the original error and gains discarded_at and discard_reason. Cleanup deletes it abandoned_retention_hours after it was discarded. The reason cannot be empty.

protean outbox discard 6f1c2a9e-... --reason "payload rejected by the broker" --domain=my_domain

Arguments and options

Argument / Option Description Default
ROW_ID Id of the outbox row, as shown by halted required
--reason Why the row is skipped, stored on the row required
--domain Domain module path . (current directory)
--provider Provider whose outbox holds the row default

Output

Outbox row 6f1c2a9e-... is discarded and will not be published.

Automatic startup sweep

The server runs the same reconciliation once when it boots. So when a crash loses the outbox row of an event-sourced aggregate's event before the relational commit, the event gets its row back on restart without operator action:

protean server --domain=my_domain

The sweep is gated on the outbox being enabled, reads the store as described above, and can never block startup. A failure during the sweep is logged and boot continues. With --workers N it runs once per worker. The sweeps can overlap with each other and with committing commands, with the exceptions listed under the reconcile command above. The sweep covers only the default provider. Run protean outbox reconcile --provider=<name> for aggregates on other providers.

Error Handling

Condition Behavior
Invalid domain path Aborts with "Error loading Protean domain"
Outbox not enabled for the domain Aborts with "Outbox is not enabled for this domain", naming default_subscription_type = "stream" and the older enable_outbox switch, which also needs it
Nothing to reconcile Prints "Nothing to reconcile: no event in the scanned window is missing its outbox row"
halted --json with an invalid domain path or the outbox not enabled Prints the error envelope (status error, the message under data.error) and exits 2
halted finds no halted key Prints "No partition key is halted." and exits 0
retry or discard with an unknown id Aborts with "No outbox row with id ..." exits 1, and changes nothing
retry or discard on a row that is not failed or abandoned Aborts with "Outbox row ... is \<status>. Only a failed or abandoned row can be retried or discarded." exits 1, and changes nothing
retry or discard on a row that changes while the command runs (a worker claims it, an attempt fails, another operator retries or discards it, or the row is deleted) Aborts with "Outbox row ... changed while the command ran" exits 1, and changes nothing
discard without --reason, or with an empty one Usage error, exit code 2
halted, retry or discard with a --provider that has no outbox Prints "No outbox for provider ..." (the error envelope under halted --json) and exits 2

How reconciliation works

The commit sequence appends events to the event store before committing the relational transaction that carries aggregate state and the outbox rows, so the event store is the durable anchor. A crash in the window between the two leaves events stored but their outbox rows uncommitted. Reconciliation reads those events back from the store and re-derives the missing rows of event-sourced aggregates' events. It skips state-based aggregates' events, for the reason given under protean outbox reconcile. The full rationale, including why this ordering was chosen over two-phase commit, is in ADR-0015: Event-Store Append as the Durable Anchor.

See the Outbox Guide for the operational walkthrough.

Domain Discovery

The protean outbox commands use the same domain discovery mechanism as other CLI commands. See Domain Discovery for the full resolution logic.