Topic 147 / Core
Inter-service communication
Chapter 9.3 · Communication Topologies
What and why
Once services own separate data (→ 146), every cross-service interaction becomes a network call instead of a function call — with everything Part 8 teaches about partial failure now live on every request. Inter-service communication is the deliberate choice, per interaction, between synchronous calls (the caller waits for an answer) and asynchronous messaging (the caller hands off and moves on). Builds on: → 146 (database per service).
Diagram
| Need | Style | Mechanism | Cost |
|---|---|---|---|
| "Give me the current price" (need the answer now) | Synchronous | REST (→ 31) or gRPC (→ 33) | Caller blocks; failure propagates immediately |
| "Order was placed" (a fact, others react on their own time) | Asynchronous | Events (→ 113) over a broker | Caller never blocks; consumers may lag |
| "Process this, I don't need the result now" | Asynchronous | Queue (→ 106) | Decouples load, adds latency |
How it works
- Classify the interaction: does the caller need an answer before it can proceed (query), or is it reporting a fact others may care about (event)? (→ 105)
- Default queries to synchronous REST or gRPC — simplest to reason about, easiest to debug.
- Default workflows and fan-out to asynchronous events or queues — decouples services in time, so one service being briefly down doesn't block another's unrelated work.
- For every synchronous call added, add it to the call chain's latency and failure budget — three sequential synchronous calls at 50 ms each is 150 ms minimum, and any one failing fails the whole chain unless mitigated (→ 151, → 152, → 153).
- Prefer the shortest synchronous chain: compose in parallel where possible, or push the aggregation to a layer designed for it (→ 148) rather than nesting calls three services deep.
Practical example
(illustrative). A checkout request calls Catalog (price, 20 ms) and
Inventory (stock, 20 ms) in parallel — 20 ms total — then calls Payment synchronously (it
must know the result, 80 ms) before responding. It then publishes OrderPlaced
asynchronously for Shipping and Notifications to react to on their own schedule — those two
services add no latency to the checkout response, and their being briefly unavailable does
not fail checkout.
When to use it
- Use synchronous when: the caller genuinely cannot proceed without the answer, and the chain stays short (1–2 hops).
- Use asynchronous when: the caller doesn't need to wait, multiple services may react, or decoupling availability matters more than immediacy.
- Failure scenarios: a long synchronous chain where one slow service degrades every caller above it (→ 126 partial failure, → 152 cascading failure); using asynchronous messaging for something the caller actually needed an immediate answer for, which just relocates the wait into polling.
- Simpler alternative: if every call in the chain is to services owned by the same team with no need for independent scaling, a single in-process function call inside a modular monolith (→ 53) removes the network entirely.
Practice
(Decision kind — judge 3–5 scenarios and defend the choice; 15 min). For each interaction — (a) "get current stock level before showing the buy button," (b) "notify the warehouse an order was placed," (c) "charge the customer's card," (d) "update a recommendation model after a purchase" — choose synchronous or asynchronous and state the one consequence of choosing the other way.
Done when every choice names what would break (an unwanted wait, or a caller blocked on something it didn't need) if the opposite style were used.
Key takeaway
Choose communication style per interaction, not once for the whole system — synchronous for queries that must block, asynchronous for everything that can be handed off. Done when you can: classify five real interactions correctly and total the latency and failure budget of a synchronous chain. Used next in → 148 (BFF), → 151 (circuit breaker), → 152 (retry and timeout).
References
A pattern language for microservices — see references.md.
Source directory