Dirigida por Eventos

EDA (en proceso)

Escrito por Darjus Kazakevic · revisado el

Modules publish events inside one process, with no broker to run.

When an order is placed, several things may need to happen: send a receipt, update stock, record analytics. Calling each one from the order code ties orders to every other module. Instead, the order module publishes an order.placed event on an in-memory bus, and each interested module subscribes.

You get loose coupling without a broker such as Kafka or RabbitMQ. But you do not get what a broker gives. Events are lost on a crash, and handlers run in the same process, so a slow or failing handler can hurt the publisher. Both need a decision, not an assumption.

En esencia — Let modules in one process communicate through an in-memory event bus, so a producer never knows who listens and no network is crossed.

Cuándo recurrir a él

Recurre a él cuando

  • One action triggers side effects owned by unrelated modules
  • You want decoupled modules, but a broker is not worth running yet
  • A modular monolith whose modules may later become separate services

Reconsidéralo cuando

  • Events must survive a restart: an in-memory bus loses them
  • The caller needs the result to continue: that is a call, not an event
  • Only one module ever reacts: a direct call is easier to follow

The parts

Event
A fact about the past, named in the past tense: OrderPlaced, UserRegistered. It carries the data subscribers need, and the publisher expects no reply.
In-memory bus
A small object that maps topic names to lists of handlers. publish() calls every handler for that topic. Node's EventEmitter, Spring's ApplicationEventPublisher and MediatR notifications are common versions.
Handler isolation
Each handler runs inside its own try/catch, so one failure does not reach the publisher. Without it, a failing analytics handler can roll back an order.
Sync vs. async dispatch
Synchronous dispatch runs all handlers before publish() returns: simple, but slow handlers slow the caller. Asynchronous dispatch queues them: faster, but the caller cannot know they ran.

Reglas de diseño

Isolate every handler
Catch and log each subscriber's failure inside the bus. Without that, a throwing analytics handler rolls back the order, and the bus is just a hidden function call.
Publish after the transaction commits
Otherwise a handler may email a receipt for an order that then rolls back. Many frameworks support this, such as Spring's @TransactionalEventListener.
Do not claim durability you do not have
An in-memory bus loses in-flight events on restart. If losing one costs money, write it to an outbox table in the same transaction, or use a broker.
Use events for side effects, calls for answers
If the publisher needs a result to continue, make a direct call. An event that waits for a reply is a slow, hidden function call.

Errores habituales

  • Handlers that throw into the publisher. An analytics bug cancels real orders.
  • Publishing inside an open transaction. Customers get receipts for orders that were rolled back.
  • Events that trigger events that trigger events. Nobody can explain why one click caused twelve writes.
  • Assuming delivery after a crash. Some orders never get a receipt, and nothing logs why.