Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Events vs Commands
HLD

Events vs Commands

Two message types that look identical and mean opposite things — the vocabulary that shapes event-driven designs.

The Distinction

 COMMAND: an INSTRUCTION — "do this" — directed at ONE receiver.
   CancelOrder(order-42)
   → order service MUST handle it; it's addressed to them.

 EVENT: a FACT — "this happened" — broadcast to anyone listening.
   OrderCancelled(order-42, reason, at)
   → past tense; producer doesn't know or care who reacts.

 [checkout] ──CancelOrder──► [orders]        command: 1 target,
                             must obey/refuse    imperative

 [orders] ──OrderCancelled──► {email, analytics, inventory}
                              events: N observers, informative

Why the Confusion Is Expensive

 teams blur them and build subtle wrongness:

 treating commands as events:
   publish RefundRequested hoping SOMEONE processes it
   → no owner → dropped refunds. commands need GUARANTEED
   delivery to a RESPONSIBLE handler.

 treating events as commands:
   OrderPlaced consumed by inventory which then MUTATES stock...
   fine! but now inventory's behavior is coupled to checkout's
   event FORMAT — rename a field, break a stranger.

 the naming test:
   past tense verb = event (OrderShipped ✓)
   imperative verb = command (ShipOrder ✓)

The Flow of a Proper Design

 commands enter, events radiate:

 user ──PlaceOrder──► [order svc]     ← command boundary
                          │ decides, persists, emits

                    OrderPlaced ──► {inventory, email, analytics}
                          
 inventory REACTS to the fact (reserves stock), 
 emits StockReserved ──► ... chain continues as facts.

 rule of thumb: COMMANDS at system edges (user intent, service
 requests); EVENTS inside/between domains (facts propagating).

Practical Consequences

PropertyCommandEvent
Deliverymust-reach-handler (retries/DLQ critical)interested-parties subscribe
Failurehandler rejects → caller informedconsumers fail independently
Couplingproducer↔handler contract tightloose; additive evolution
Validationhandler validates + respondsnone possible post-facto
 error handling differs radically:
 rejected command → tell the SENDER (synchronous-ish path)
 failed event consumption → consumer's own retry/DLQ;
 sender neither knows nor cares.

Interview Framing

Design reviews score this vocabulary silently: candidates who label arrows correctly (command into the domain, events out) draw architectures whose failure semantics actually work. Use the words precisely — “the checkout issues PlaceOrder; orders emits OrderPlaced” — and the interviewer stops worrying about your fundamentals. One sentence to keep: “commands are requests, events are receipts.”

My Private Notes

Notes are auto-saved locally to this device.