Bus de Eventos
Escrito por Darjus Kazakevic · revisado el
One publish/subscribe interface; deployment decides the transport.
An Event Bus is the publish/subscribe interface your application code uses: publish(event) and subscribe(topic, handler). Its value is what it hides. The subscriber may be a function in the same process, or a consumer reading from Kafka on another machine.
This keeps the transport a wiring decision. A module can start with an in-memory bus and later move behind a broker, with no change to the code that publishes. The rule that makes this work: the interface promises only what the weakest transport can deliver.
En esencia — Publish events through one interface that hides whether subscribers are in this process or behind a broker, so the transport can change without code changes.
Cuándo recurrir a él
Recurre a él cuando
- A module runs in-process now but may move behind a broker later
- Publishers should not know whether subscribers are local or remote
- Tests need an in-memory bus in place of Kafka or RabbitMQ
Reconsidéralo cuando
- The publisher needs a reply, ordering or exactly-once delivery
- Dispatch that will always stay in one process and one context
- You need broker features, like Kafka replay, visible in your code
The moving parts
- The contract
- Two methods: publish() and subscribe(). When publish() resolves, the bus has accepted the event. It does not mean anyone has handled it.
- Topic
- The name subscribers listen to, such as order.placed. Behind a broker it becomes a Kafka topic, a RabbitMQ routing key or a NATS subject.
- Implementations
- An in-memory bus for tests and a single process. A broker-backed bus (Kafka, RabbitMQ, NATS, SNS with SQS) in production. Same interface, chosen at the composition root.
- Delivery semantics
- With a broker, delivery is usually at-least-once: a handler may see an event twice, late, or out of order. Write every handler for that, even while it runs in memory.
Applying it well
- Promise only what the weakest transport can
- publish() resolves with nothing: no ordering, no exactly-once, no promise that a subscriber has run. If it promises more, code will rely on it and break the day a broker replaces the in-memory bus.
- Write local subscribers as if they were remote
- Make every handler idempotent and tolerant of reordering today, even while it runs in the same process. That keeps the move to a broker a wiring change, not a rewrite.
- Treat events as a public contract
- Once another process reads an event, its shape is an API. Add fields freely; never rename or remove one. For a breaking change, publish a new version, such as order.placed.v2.
Errores habituales
- Returning a delivery result from publish(). It becomes false the day a broker is behind it.
- Local handlers that assume they run once and in order. They break after the move.
- Kafka or RabbitMQ types, such as headers or partitions, leaking into the interface.
- Using the bus to ask for data. A request that needs an answer should be a direct call.