We use analytics cookies.We use cookies to analyze site traffic and improve your experience.
Loading...
System Design Masterclass · Messaging Event Systems
Free preview: the first 2 parts of this 6-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design courseread the rest of this lesson in
System Design Masterclass
Unlock this lesson and the 751 other paid lessons
$25 once, by PayPal
See the System Design coursein Messaging Event Systems
This lesson is part of the full System Design Masterclass. Unlock it along with 769 other lessons for a one-time payment.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime Access18 Foundation lessons are free, no payment needed. Try them now
It is easy and good
“It is really nice and good Please join the course”
An event bus broadcasts announcements: "Something happened." Anyone who cares can listen. Nobody is expected to do anything specific.
A command bus delivers orders: "Do this specific thing." Exactly one handler is responsible for executing it. If no handler exists, that is an error. If two handlers exist, that is also an error.
Think of it this way. An event is like a news headline: "Fire broke out on 5th Street." Anyone can react however they want. The fire department responds, journalists report, neighbors evacuate. A command is like a direct instruction to the fire department: "Dispatch Engine 7 to 5th Street." There is exactly one recipient, and they must execute it.
In software, commands represent intent: CreateOrder, CancelSubscription, SendPasswordReset. Each command has exactly one handler that knows how to fulfill it. The command bus routes the command to that handler.
This distinction (events for notifications, commands for instructions) is the foundation of (Command Query Responsibility Segregation), which separates the "do something" path from the "read something" path in your application.
A command bus has a strict contract:
{ type: 'CreateOrder', userId: '123', items: [...] })// Define a command
class CreateOrder {
constructor(
public readonly userId: string,
public readonly items: OrderItem[]
) {}
}
// Register the handler
commandBus.register(CreateOrder, new CreateOrderHandler());
// Dispatch the command
commandBus.dispatch(new CreateOrder('user-123', items));
Event bus vs. Command bus:
| Event Bus | Command Bus | |
|---|---|---|
| Semantics | "This happened" (past tense) | "Do this" (imperative) |
| Handlers | Zero or many | Exactly one |
| Return value | None expected | Can return result |
| Missing handler | Acceptable (no one cares) | Error (someone must handle it) |
| Example | OrderPlaced | CreateOrder |