Event feed¶
The backend never calls payment-, notification- or delivery-service, and calls warehouse-service only for stock commands (ADR 6). The services learn about commerce events by polling a durable pull feed on the backend's internal API; it is their only event transport (ADR 12). Dependencies point one way: warehouse-service → backend, payment-service → backend, notification-service → backend and delivery-service → backend. The decision is recorded in ARCHITECTURE_DECISIONS.md.
| Release | Role |
|---|---|
| backend 3.16.0 | Records every domain event in domain_events (Flyway V16) and serves the feed |
| payment-service 1.1.0 | Consumes card-refund.requested (Flyway V3: cursor and processed events) |
| warehouse-service 1.3.0 | Consumes order.cancelled (Flyway V4: cursor and processed events) |
| backend 3.21.0 | Adds order.reservation-expired and the internal notification.requested, accepts the NOTIFICATION service token, and stores span_id (Flyway V24) so every event carries spanId |
| notification-service 1.0.0 | Consumes every webhook-eligible type and notification.requested |
| backend 3.23.0 and the 1.7.0 / 1.4.0 / 1.1.0 services | Optional broker transport through ActiveMQ Artemis next to the feed (ADR 7, since retired) |
backend-rs 1.0.0 and the -rs services |
The Rust ports keep the feed, its tables and the broker queues unchanged |
| backend-rs 1.2.0, delivery-service 1.0.0 | Accepts the Delivery service token; delivery-service consumes order.paid, shipment.shipped, order.cancelled and shipment.delivered |
| backend-rs 1.4.0, warehouse/payment/notification-service 1.3.0, delivery-service 1.2.0 | The broker transport is removed; the feed is the only transport. Each consumer parks events it can never handle in its own parked_events table |
Producer: the backend¶
Every CommerceEvent is appended to domain_events by a synchronous listener,
in the same transaction as the state change that published it. An event
therefore exists exactly when its change committed: no dual write, no separate
outbox delivery job.
domain_events (sequence BIGSERIAL PK, event_id UUID UNIQUE, type, order_id,
username, occurred_at, recorded_at, payload)
Request¶
GET /internal/v1/events?after={sequence}&limit={n}&types={wire names} with
X-Service-Token set to the warehouse, the payment or (backend 3.21.0) the
notification service token. Like
every internal endpoint it is not routed by the gateway (/internal/ is 404 on
every host) and is absent from the public OpenAPI document.
| Parameter | Rule |
|---|---|
after |
Sequence to continue after; default 0; a non-negative integer, else 400 |
limit |
1–200, default 100, else 400 |
types |
Optional comma-separated wire names (order.cancelled, card-refund.requested, ...); an unknown name is 400 |
Response¶
200 { events: [ { sequence, id, type, occurredAt, orderId, username,
traceId, spanId, data } ],
nextCursor, hasMore }
- Events are ordered by
sequenceascending; every event carries every field (nulls included).traceIdand, from backend 3.21.0,spanIdidentify the request that recorded the event; a consumer starts aCONSUMERspanprocess <type>with that parent, so a cancellation and the refund or webhook it causes are one trace (see the tracing page of the wiki).occurredAtis ISO-8601 UTC with millisecond precision.datais stored once with sorted keys and plain decimals, so a redelivered event has identical bytes. nextCursoris the last returned sequence (orafterfor an empty page); the consumer passes it as the nextafter. It also moves past events thetypesfilter excluded.hasMoreis true when more matching events exist.- 400
{"message": ...}for bad parameters, 401 for a missing or wrong service token, 404 when no service token is configured.
Commit-ordered positions¶
The sequence a consumer sees is the event's feed position, assigned in
commit order. Insert sequences are allocated when a row is inserted, not when
its transaction commits: transaction A can take sequence 10, transaction B
sequence 11, and B can commit first. A consumer that read 11 and stored it as
its cursor would never see 10.
So every feed read first gives the committed events that have no position yet
a position (backend V29 domain_events.feed_position), one reader at a time
under the lock of the single domain_event_feed_head row. A row is visible to
that reader only once its transaction has committed, so A, committing after B
was served at position 11, gets position 12 and is served on the next poll.
Each position is max(last position + 1, insert sequence), so positions equal
the insert sequences whenever transactions commit in insert order. An event
reaches a consumer within one poll interval (5 s) of its commit.
Until backend 3.24.0 the feed held back events recorded less than 3 s ago
(events.settle-delay). That only narrowed the window: a cancellation whose
transaction committed more than 3 s after it inserted its events (for example
under database latency) was skipped by every consumer, and its card refund
was never made. The exploratory chaos session of 2026-11-21 found it.
Retention¶
A scheduled job deletes events recorded more than events.retention ago
(default P30D, checked every PT1H). A consumer that is down for longer than
the retention period misses events; warehouse-service's reconciler is the safety
net for cancellations, and card refunds keep their own record in the backend's
card_refund_requests.
| Property | Environment variable | Default |
|---|---|---|
events.retention |
EVENTS_RETENTION |
P30D |
events.retention-job.enabled |
EVENTS_RETENTION_JOB_ENABLED |
true |
events.retention-job.interval |
EVENTS_RETENTION_JOB_INTERVAL |
PT1H |
The Compose files keep the defaults and set none of these variables.
Event types consumed today¶
| Wire name | Consumer | Handling |
|---|---|---|
card-refund.requested |
payment-service | Refunds the captured card payment; idempotent per requestId; no captured payment or an amount above what is refundable books nothing and is logged |
order.cancelled |
warehouse-service | Cancels the order's shipments (every parcel from warehouse-service 1.5.0) when they are AWAITING_PICK or PICKED (lastActor = "system"); later states are logged and left alone |
order.created, order.paid, order.cancelled, order.reservation-expired, shipment.shipped, shipment.delivered, refund.booked, payment.failed |
notification-service | One signed delivery per matching active user webhook subscription |
notification.requested |
notification-service | Renders and sends the commerce email; idempotent per requestId |
order.paid, shipment.shipped, order.cancelled, shipment.delivered |
delivery-service | Books, hands over, cancels or closes the order's delivery (DELIVERY_SERVICE_CONTRACT.md); never writes to the backend |
From backend 3.21.0 order.cancelled carries data.reason:
CUSTOMER, ADMIN or RESERVATION_EXPIRED.
order.reservation-expired¶
Stock is deducted when an order is created; that deduction is the hold, and
from backend 3.21.0 it expires (orders.reservation.ttl, default PT30M).
ReservationExpiryJob cancels each PENDING order past its
reservationExpiresAt through the normal cancellation path (coupon released,
stock ORDER_RESTORED, gift-card and points tenders returned) and records, in
the same transaction, order.cancelled with reason = "RESERVATION_EXPIRED"
and the webhook-eligible order.reservation-expired with
{orderId, expiredAt, totalAmount}. warehouse-service sees the cancellation as
any other; notification-service delivers both types to subscribers.
notification.requested is internal¶
Where backend 3.20.x sent a customer email itself (payment received or failed,
back in stock, the warehouse notifications of POST /internal/v1/notifications,
loyalty points earned, gift card issued), backend 3.21.0 with
NOTIFICATIONS_DELIVERY=feed records notification.requested instead:
template is a commerce EmailTemplate (PAYMENT_RECEIVED, PAYMENT_FAILED,
BACK_IN_STOCK, ORDER_SHIPPED, ORDER_DELIVERED, RETURN_UPDATED,
SHIPMENT_OUT_FOR_DELIVERY, SHIPMENT_DELIVERY_FAILED,
LOYALTY_POINTS_EARNED, GIFT_CARD_ISSUED); requestId is the idempotency
key. The type never reaches user webhooks. With the default
NOTIFICATIONS_DELIVERY=email (the l20 compatibility stack) the backend keeps
sending these emails and records no such event.
card-refund.requested is internal¶
The backend publishes card-refund.requested with data
{requestId, orderId, amount, reason} when a booked refund has a card leg
(the card-first refund split) and when an order with a captured card payment
is cancelled (requestId = UUID.nameUUIDFromBytes("cancel:" + orderId),
reason Order cancelled). It replaces the 3.15.0 HTTP refund outbox to
payment-service. The type is not listed by
GET /api/v1/webhooks/event-types and never reaches user webhooks; customers
see the refund as refund.booked.
Consumer pattern¶
warehouse-, payment-, notification- and delivery-service implement the same pattern independently (see ADR 3):
EventFeedPollerruns every<service>.events.poll-interval(defaultPT5S) when<service>.events.enabledis true, and asks for its own types only, starting after its stored cursor. It followshasMorefor a bounded number of pages per run (warehouse 10, payment 20).- Each event is handled in one local transaction that locks the
consumer's cursor row, skips the event if its
idis already inprocessed_events, applies the change, records the id and advances the cursor. Handling is idempotent and several instances never process one event concurrently. - Backend unavailable, or a transient database error: the run ends without moving the cursor and the next run retries.
- An event the service can never handle (malformed payload, missing
orderId, a template that does not exist) is parked: copied with the reason into the service'sparked_eventstable, recorded as processed and passed, so one bad event never blocks the feed. - Anything else (a bug, an unexpected error) stops the run like an outage: the cursor stays and the next run retries the same event.
Redrive a parked event once its cause is fixed: delete its row from
processed_events (and from parked_events) and move the consumer's
event_cursors.last_sequence back to just before the sequence stored in
parked_events (the feed position the consumer saw).
The next run reads it again; events after it that were already handled are
skipped by id, so nothing is applied twice.
| Service | Variables | Defaults |
|---|---|---|
| payment-service | PAYMENT_EVENTS_ENABLED, PAYMENT_EVENTS_POLL_INTERVAL |
true, PT5S |
| warehouse-service | WAREHOUSE_EVENTS_ENABLED, WAREHOUSE_EVENTS_POLL_INTERVAL |
true, PT5S |
| notification-service | NOTIFICATION_EVENTS_ENABLED, NOTIFICATION_EVENTS_POLL_INTERVAL |
true, PT5S |
Against a backend older than 3.16.0 the feed request answers 404, which the consumers log on every run.
Testing the feed¶
The feed is internal, so test it through its effects:
- pay an order with
4242 4242 4242 4242, cancel it, and within about ten secondsGET /api/v1/orders/{id}/paymentsshows the paymentREFUNDED; - release an order to the warehouse (
AWAITING_PICK), cancel it as the customer, and within about ten seconds the shipment isCANCELLEDwithlastActor = "system"; - refund a return of a card-paid order and watch the payment become
PARTIALLY_REFUNDEDorREFUNDED; - subscribe a webhook inbox to
order.cancelledandorder.reservation-expired, expire an order withPOST /api/v1/admin/orders/{id}/expire-reservationand read both deliveries from the inbox about ten seconds later; - pay an order and find
PAYMENT_RECEIVEDinGET /api/v1/notifications/emails(and in Mailpit on the full profile).
To read the raw feed in the lightweight profile (the backend image has no shell tools, so the request goes from the gateway container):