LOAD-BEARINGfree
Low-level design dojoTier 13 · unit 31 of 31

In-process event bus

A publish/subscribe bus inside one process. Modules publish domain facts — OrderPlaced, UserRegistered — and any number of handlers in the same application react. No broker, no network, one process. It is the first thing a team builds to decouple a monolith, and the first thing that surprises them at 3am.

What does an in-process event bus actually buy, compared with calling the handlers directly?

  • Reliability, because events are recorded before delivery
  • The publisher stops knowing who consumes its events
  • Parallelism across machines
  • Ordering guarantees between modules
Why

The only thing it genuinely provides is decoupling of knowledge: OrderService announces a fact and does not import the six modules that care. It provides no durability — a process restart loses everything undelivered — no distribution, and no ordering beyond whatever your dispatch happens to do. Every serious incident with one of these comes from a team that assumed otherwise because the API looked like Kafka.

Assign each responsibility.

Hold the mapping from event type to interested handlers
EventBus
Pair one handler with the token that removes it again
Subscription
Decide whether a handler runs inline or on a worker
DispatchPolicy
Bound the pending work and decide what happens when it is full
DispatchPolicy
Receive events whose handler failed every attempt
FailureSink
Why

Dispatch is a policy object rather than a mode flag on the bus because one application legitimately needs both: an audit handler must run inline inside the caller’s transaction, and an email handler must not. Subscription is a real object rather than a bare function because unsubscribing is what stops a long-lived bus from holding every short-lived object that ever registered — the most common memory leak this design produces. The failure sink exists so that "the handler threw" has somewhere to go other than a log line nobody greps.

Handlers run inline, and OrderService publishes inside its database transaction. One handler calls a payment provider that is slow today. What has happened?

  • Nothing — the bus isolates the publisher
  • The publisher’s latency now includes that call, and its database connection is held open across a network hop
  • The handler is automatically moved to a background thread
  • The event is buffered until the transaction commits
Why

Synchronous dispatch is a function call chain wearing a message-broker costume. The publisher’s p99 becomes the sum of every subscriber’s p99 — Tier 0’s dependency arithmetic, now invisible because no call site names the dependency — and publishing inside a transaction pins a connection for the duration, which is Lesson 3.06’s pool exhaustion arriving from a direction nobody was monitoring. Anything doing I/O belongs off the caller’s thread, and anything that must be consistent with the write belongs in an outbox (6.05).

Five handlers subscribe to OrderPlaced. The third one throws. What should the bus do?

  • Propagate the exception to the publisher and abort the publish
  • Isolate each handler: catch, record, route to the failure sink, continue with the rest
  • Re-publish the event so all five handlers run again
  • Unsubscribe the failing handler automatically
Why

The order was placed. That fact is already true, and a broken invoice handler does not un-place it — so propagating lets one buggy listener fail an unrelated write, which is the exact coupling the bus was introduced to remove. Re-publishing re-runs the two handlers that already succeeded, importing Tier 6’s at-least-once problem into a single process with none of the idempotency machinery. Isolate, record, and let retry be a per-subscription policy that ends at the failure sink.