Command Bus
ELI5 — The Vibe Check
A command bus is the postal service for 'please do this' messages. You drop a command on the bus ('CreateOrder'), and the bus delivers it to the right handler. The sender doesn't need to know who handles it — just that it gets done.
Real Talk
A messaging pattern that decouples command senders from handlers by routing command objects through a centralized bus/dispatcher. The command bus receives command DTOs, resolves the appropriate handler, and optionally applies middleware (logging, validation, authorization) before execution. Used in CQRS implementations.
Show Me The Code
class CreateOrderCommand {
constructor(
public readonly userId: string,
public readonly items: OrderItem[]
) {}
}
class CreateOrderHandler {
async handle(cmd: CreateOrderCommand) {
const order = Order.create(cmd.userId, cmd.items);
await this.repository.save(order);
}
}
await commandBus.dispatch(new CreateOrderCommand('user-1', items));
When You'll Hear This
"The command bus decouples our API controllers from domain logic — the controller just dispatches commands." / "We add authorization and logging as command bus middleware instead of repeating it in every handler."
Related Terms
CQRS
CQRS says: the way you write data and the way you read data should be separate systems. Writing (commands) goes to one model optimized for transactions.
Event Handler
An event handler is the code that says 'oh, something happened? Let me react to that.
Message Queue
A Message Queue is a waiting room for tasks. Producers drop tasks in the queue, consumers pick them up and process them one at a time.