From 86d69cacb9298b107a97a7e4848e0596967667e4 Mon Sep 17 00:00:00 2001 From: dillonstreator Date: Sat, 28 Nov 2026 13:33:34 -0600 Subject: [PATCH] docs: explain that fan-out fixes an event's handlers, and how to change them safely Deliveries are created once, from the handler map of whichever processor fans the event out. Document what that means for rolling deploys that add or remove a handler, for {} handler maps, and for several services on one store, with a Postgres back-fill for the deploy overlap. --- README.md | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2da02ed..647043b 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ process.on("SIGTERM", async () => { }); ``` -The handler map must list every event type in `eventSchemas` (TypeScript enforces this). An empty object (`OrderPlaced: {}`) is valid and means "no side effects". +The handler map must list every event type in `eventSchemas` (TypeScript enforces this). An empty object (`OrderPlaced: {}`) is valid and means "no side effects": a processor with that map marks `OrderPlaced` events fanned out without creating any deliveries, so don't use one in a processor that shares its store with processors that do handle the type (see [Changing handlers](#changing-handlers)). ## How It Works @@ -211,7 +211,7 @@ publish(tx) ──► txob_events ──fan-out──► txob_deliveries ──c ``` 1. **Publish.** `outbox.publish(tx, ...)` inserts one row into `txob_events` inside your transaction. Publishing doesn't need to know which handlers exist. -2. **Fan-out.** A processor leases unfanned events of the types it handles and, in a single statement, creates one **delivery** per registered handler and marks the events fanned out. Fan-out is idempotent: deliveries are keyed by `(event_id, handler)`. +2. **Fan-out.** A processor leases unfanned events of the types it handles and, in a single statement, creates one **delivery** per handler registered **in that processor** and marks the events fanned out. Each event is fanned out once, so which handlers it reaches is decided then (see [Changing handlers](#changing-handlers)). Fan-out is idempotent: deliveries are keyed by `(event_id, handler)`. 3. **Claim.** The processor claims pending deliveries up to its free capacity, highest priority first, within each handler's concurrency, per-key concurrency and rate limits. Limits count deliveries leased by **all** processors, and claims for a limited handler are serialized in the store, so they hold however many processors run. Claiming sets a lease (`leaseMs`, default 30s) and a fresh lease token, then commits. No transaction is held while handlers run. 4. **Run.** Claimed deliveries run immediately. Leases of in-flight deliveries are renewed every `leaseMs / 3`. 5. **Record.** Success marks the delivery `completed`. Failure schedules a retry with backoff, or marks it `dead` after `maxAttempts` (or immediately for `ErrorUnprocessableEventHandler`). Every write is conditional on the lease token, so a processor that lost its lease can't overwrite the work of the processor that took over. @@ -222,6 +222,26 @@ If a processor crashes, its leases expire and other processors pick the work up. txob guarantees **at-least-once** delivery per handler. A handler can run more than once for the same event if the process crashes after the side effect but before the result is recorded, or if a lease is lost (for example, a long GC pause or network partition). **Make handlers idempotent**, e.g. by using `` `${event.id}:${handlerName}` `` as an idempotency key with external APIs. +### Changing handlers + +The guarantee is per handler that the event was **fanned out to**. Fan-out happens once per event, in whichever processor leases it first, and creates deliveries only for the handlers in that processor's handler map. Nothing re-fans an event later. So every processor that shares a store must run the same handler map, and changing it needs care: + +- **Adding a handler during a rolling deploy.** While old and new processors overlap, events fanned out by an old processor get no delivery for the new handler, and never will, even after the rollout completes. No error, dead delivery or metric shows it. Either stop the old processors before starting new ones (a `Recreate` deployment for the processors; publishers can keep rolling, since `publish` doesn't depend on handlers), or back-fill the overlap afterwards. On Postgres: + + ```sql + -- Deliveries for 'crm' on UserCreated events fanned out since the deploy started. + -- Set priority and concurrency_key as the handler's functions would, if it uses them. + INSERT INTO txob_deliveries (event_id, event_type, handler) + SELECT id, type, 'crm' FROM txob_events + WHERE type = 'UserCreated' AND fanned_out_at >= '2026-01-01T12:00:00Z' + ON CONFLICT (event_id, handler) DO NOTHING; + ``` + + The new handler must be idempotent for this, as always. MongoDB, Redis and DynamoDB need the equivalent writes in their own format. + +- **Removing a handler.** Old processors keep creating deliveries for it during the overlap, and the new ones never claim them, so they stay `pending`. Delete them once the rollout completes (`DELETE FROM txob_deliveries WHERE handler = 'crm' AND status = 'pending'`). +- **Several services on one store.** A processor fans out every event type in its handler map, including types mapped to `{}`. Two services with different handler maps on the same tables each fan out some of the other's events and drop the other's handlers for them. Give each service its own tables (`tablePrefix`, `collectionPrefix`, `prefix` or `tableName`) and publish to each outbox, or run one processor deployment that holds every handler. For many independent consumers, a message broker fed by txob is the better fit. + ## Handler Configuration Each handler is either a function or a config object: