Sync calls vs events

The first instinct when splitting a service is to have the Order Service call the Inventory Service over HTTP to deduct stock. It feels natural. It is also a trap. You just built a distributed monolith: if Inventory is down, Orders cannot accept traffic either. All of the cost of splitting, none of the benefit.

Events instead of HTTP calls

The Order Service publishes a fact. Any number of services subscribe. Nobody is in the critical path of anybody else.

Two vocabulary terms worth knowing. A command tells a service what to do: 'deduct 2 items from stock'. An event announces what happened: 'an order was placed'. Events are past-tense facts. Commands are present-tense instructions. In microservices, prefer events. The publisher does not need to know or care who is listening.

Name events in the past tense: OrderPlaced, PaymentReceived, StockDepleted. Not PlaceOrder, ReceivePayment, DepleteStock. This tiny naming rule makes the system easier to reason about because every event name is a fact that already happened, not a request that might fail.

Quiz: Quiz

Loading practice…