Karyo
Documentation/ Decision records

ADR 0009: State changes leave the process through a transactional outbox

Karyo 2.1.1rendered at build time
On this page

Status: Accepted

Context

Integrating systems subscribe to Karyo's state changes as webhooks (Webhooks and the event catalogue). A notification must go out for every change that committed and for none that rolled back. Sending from inside the business transaction cannot promise both: the commit can succeed and the send fail, or the send succeed and the commit roll back.

The copilot (ADR 0027) also needed a feed of each goods owner's recent activity; it is retired. ADR 0041 expected a later agent's recent-activity tool to read the same table; ADR 0047 reads the inventory journal instead.

Decision

  • A state change that matters outside its module is written to karyo.outbox_events inside the same transaction as the change, through OutboxService.publishInFacility or OutboxService.publishForBusinessUnit. Where in-process observers also need the change, the same payload is fired as a CDI event in the same transaction (ADR 0008), for example GoodsReceiptService.fireLineReceived.
  • A row carries the aggregate type and id, the event type, the payload serialised to JSON exactly as fired - not wrapped in an envelope - and tenant_id, which is the goods owner (V1__create_outbox.sql, outbox_events).
  • The outbox has two readers:
    • The webhook relay. WebhookFanoutScheduler reads rows past a stored cursor, matches each against the active subscriptions of the same goods owner by event type, and records one delivery per subscription and event (WebhookFanoutScheduler.scheduled, WebhookFanoutScheduler.runOnce). WebhookDeliveryScheduler hands each due delivery to its own transaction, with retries and a capped backoff (WebhookDeliveryScheduler). Both run every karyo.webhooks.poll-interval; the interval, batch size, timeout, attempt limit and backoff are in application.yaml, karyo.webhooks.
    • A recent-activity read, OutboxEventRepository.recentForTenant, which returns a goods owner's rows for the last N minutes (libs/karyo-events/src/main/kotlin/com/karyo/events/outbox/OutboxEventRepository.kt, OutboxEventRepository). Its only production caller, the retired copilot's tool, is gone; only a test calls it now.
  • The system owner, client 0, owns no goods and never receives a webhook (WebhookFanoutScheduler.runOnce).
  • Fan-out progress is the cursor, not the row's published flag (OutboxReader, in WebhookFanoutScheduler.kt).

Consequences

  • A committed change is delivered at least once; a rolled-back change is never delivered. Each delivery carries a stable id, so a receiver can discard a retry it has already processed.
  • There is no broker to operate. The relay is two scheduled beans inside the application.
  • Delivery latency is bounded below by the poll interval, five seconds by default.
  • Each delivery runs in its own transaction, so a slow or failing endpoint cannot roll back deliveries that already succeeded.
  • The relay requires a single application instance. Neither scheduler locks rows, and ConcurrentExecution.SKIP only stops a tick overlapping the previous tick in the same JVM. A second instance against the same database would deliver everything twice. The supported deployment runs one instance (ADR 0022).
  • published is written false and never set. The partial index on unpublished rows and OutboxEventRepository.findUnpublished serve nothing (V1__create_outbox.sql, outbox_events, OutboxEventRepository).
  • Nothing deletes outbox rows in a running installation; only the demo-data reset truncates the table (DemoDataService.loadedProfile). The table grows with every state change.
  • DomainEvent<T> in libs/karyo-events defines an envelope that nothing constructs; the relay builds its own envelope when it delivers (DomainEvent).
  • Event types use two naming forms, PascalCase and dotted lowercase. The reason is not recorded. The delivered envelope's id is derived from the delivery, so the id of the source outbox row is not on the wire and two subscriptions cannot correlate the same event; whether that was weighed is not recorded.

Alternatives considered

  • Publishing to a message broker, or calling subscribers, from inside the business transaction. Rejected: a dual write cannot be atomic, which is the problem the outbox exists to solve.
  • Change data capture from the database log. Not taken: a polling relay is simpler to build and needs no further component, at the price of latency bounded by the poll interval.
  • Delivering a whole batch in one transaction. Rejected: one slow or failing endpoint could time the transaction out and roll back deliveries already made (WebhookDeliveryScheduler).

Evidence

  • libs/karyo-events/src/main/kotlin/com/karyo/events/outbox/OutboxService.kt, OutboxService - the single write path
  • services/karyo-app/src/main/resources/db/migration/common/V1__create_outbox.sql, outbox_events - the table
  • services/order-service/karyo-orders-core/src/main/kotlin/com/karyo/orders/service/GoodsReceiptService.kt, GoodsReceiptService.fireLineReceived - a dual publish: outbox row and CDI event in one transaction
  • services/integration-hub-service/karyo-webhooks-core/src/main/kotlin/com/karyo/webhooks/relay/WebhookFanoutScheduler.kt, WebhookFanoutScheduler - fan-out
  • services/integration-hub-service/karyo-webhooks-core/src/main/kotlin/com/karyo/webhooks/relay/WebhookDeliveryScheduler.kt, WebhookDeliveryScheduler - delivery
  • services/karyo-app/src/main/resources/application.yaml, karyo.webhooks - relay settings
  • Events and the outbox
  • ADR 0008 - the in-process half of the same notification
  • ADR 0010 - why the outbox is a delivery log, not the source of state
  • ADR 0022 - the single-instance deployment the relay depends on
  • ADR 0041 - the retired copilot's successor; ADR 0047 reads the journal, not the outbox