Documentation/ Decision records
ADR 0009: State changes leave the process through a transactional outbox
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_eventsinside the same transaction as the change, throughOutboxService.publishInFacilityorOutboxService.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 exampleGoodsReceiptService.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.
WebhookFanoutSchedulerreads 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).WebhookDeliverySchedulerhands each due delivery to its own transaction, with retries and a capped backoff (WebhookDeliveryScheduler). Both run everykaryo.webhooks.poll-interval; the interval, batch size, timeout, attempt limit and backoff are inapplication.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 webhook relay.
- 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
publishedflag (OutboxReader, inWebhookFanoutScheduler.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.SKIPonly 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). publishedis writtenfalseand never set. The partial index on unpublished rows andOutboxEventRepository.findUnpublishedserve 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>inlibs/karyo-eventsdefines 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 pathservices/karyo-app/src/main/resources/db/migration/common/V1__create_outbox.sql,outbox_events- the tableservices/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 transactionservices/integration-hub-service/karyo-webhooks-core/src/main/kotlin/com/karyo/webhooks/relay/WebhookFanoutScheduler.kt,WebhookFanoutScheduler- fan-outservices/integration-hub-service/karyo-webhooks-core/src/main/kotlin/com/karyo/webhooks/relay/WebhookDeliveryScheduler.kt,WebhookDeliveryScheduler- deliveryservices/karyo-app/src/main/resources/application.yaml,karyo.webhooks- relay settings- Events and the outbox