Bus de Mensajes de Dominio
Dispatcher en proceso
Escrito por Darjus Kazakevic · revisado el
In-process dispatch: commands go to one handler, events go to many.
A Domain Message Bus is a small dispatcher inside one process. You send it a command (an instruction, such as PlaceOrder) and it calls the one handler for it. You publish an event (a fact, such as OrderPlaced) and it calls every subscriber. The model can announce what happened without importing the code that reacts.
Everything runs in memory: no network, no broker, no durability. If the process crashes, messages not yet handled are gone. That is the price of its simplicity. It is the first of three steps in the messaging ladder below, which shows where to go when that price is too high.
En esencia — Dispatch commands to exactly one handler and events to any number of subscribers, in memory inside one process, so the model never imports who reacts.
Cuándo recurrir a él
Recurre a él cuando
- Code in one context must react without the model importing it
- Commands and events are mixed together in ad-hoc callbacks
- Modules of a modular monolith react to each other in memory
Reconsidéralo cuando
- The reaction runs in another service or process: use a broker
- One caller and one handler, where a direct call is clearer
- A message must survive a crash: memory is lost on restart
The moving parts
- Command
- An instruction, named in the imperative: PlaceOrder, CancelBooking. It has exactly one handler, and that handler may refuse it with an error.
- Event
- A fact that already happened, named in the past tense: OrderPlaced. It has zero or more subscribers, and none of them can undo it.
- The dispatcher
- A map from message type to handlers, filled once at startup. send() calls the one command handler; publish() calls every event subscriber in turn. Plain function calls, no serialization.
- The messaging ladder
- Three rungs, and each adds one thing. This bus dispatches inside one process. An event bus hides whether a subscriber is local or remote. A message broker stores messages, so they survive a crash.
Applying it well
- Commands may refuse; events may not
- A command has one handler, and its error is the caller's answer. An event already happened, so a subscriber that throws cannot stop it. Log the subscriber's failure instead of passing it up.
- Fail at startup on a duplicate handler
- Two handlers registered for one command is a wiring bug. Reject the second registration when the app boots, instead of letting dispatch quietly pick one at runtime.
- Publish events after the commit
- Collect events while the use case runs and dispatch them after the transaction commits. Otherwise a rollback leaves subscribers acting on something that never happened.
Errores habituales
- An event subscriber that throws to stop the action. The fact already happened.
- Publishing before commit, so subscribers act on data that was rolled back.
- Handlers that send more commands, building long chains nobody can see in the code.
- Treating it as durable. Work queued in memory is lost when the process dies.