Skip to content

Command Bus

Spicy — senior dev territoryArchitecture

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.