Course contents

11. Microservices Lesson 6 of 10

Inter-Service Communication

See how a slow service cascades up a synchronous REST or gRPC call chain, and how a queue turns the same slowness into a harmless backlog.

15 minAdvanced7 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A synchronous caller waits and holds a worker; its latency and availability include everything below it.
  • REST (JSON over HTTP) and gRPC (protobuf over HTTP/2) differ in contract, encoding and multiplexing, but both keep the caller waiting.
  • A slow callee fills its callers’ worker pools one level at a time until the top of the chain rejects requests: a cascading failure.
  • Publishing to a queue removes the consumer from the request path: slowness or downtime becomes a backlog instead of errors.
  • Async answers come later, correlated by id; the process needs pending states and idempotent consumers. Choose per call.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

Inter-service communication is how the services of a distributed system ask each other for things. There are two basic styles:

  • Synchronous request/response. The caller sends a request and waits for the answer before it continues: REST over HTTP, gRPC, GraphQL. Caller and callee must both be up at the same time.
  • Asynchronous messaging. The sender puts a message on a queue or publishes an event to a broker and carries on. The receiver processes it whenever it can. Sender and receiver do not need to be up together; the broker sits between them.

Most real systems use both, chosen per interaction.

Why it exists

Once a system is split into services, a function call becomes a network call, and every network call can be slow, fail or partly succeed. The style of communication decides how those problems spread. In a synchronous chain the caller inherits the callee's latency, availability and capacity problems. With messaging the producer is isolated from the consumer, at the price of not getting an answer straight away.

How it works

Synchronous calls. The caller holds a resource while it waits: a thread, an async request slot, a connection from a pool. Latency adds up along the chain. Availability multiplies: three services at 99.9% give roughly 99.7% for the request. Worse, a slow callee is more dangerous than a dead one. In the lesson, Fraud becomes four times slower; Pricing's 24 workers fill up waiting for Fraud, then Orders' 24 workers fill up waiting for Pricing, and Orders starts rejecting orders. Every service that calls a slow service, directly or indirectly, loses capacity. That is a cascading failure.

REST and gRPC. REST usually sends JSON over HTTP with resources and verbs, described by an OpenAPI document. It works everywhere, including browsers, and is easy to inspect. Over HTTP/1.1 a connection carries one request at a time (pipelining is effectively unused), so clients keep a pool of connections. gRPC sends Protocol Buffers over HTTP/2, with client and server code generated from a .proto file. HTTP/2 multiplexes many concurrent streams over one connection, and gRPC supports server, client and bidirectional streaming plus deadlines that propagate to the callee. Its costs: browsers need gRPC-Web or a gateway, binary payloads need tooling to read, and long-lived HTTP/2 connections defeat simple L4 load balancing (each connection pins to one backend, so you need L7 or client-side balancing). Neither changes the fundamental coupling: the caller still waits.

Asynchronous messaging. The producer sends a message to a queue (point-to-point: one consumer handles it) or a topic (publish/subscribe: every subscriber gets a copy) and gets an acknowledgement from the broker, not from the consumer. The consumer pulls messages at its own pace. If it is slow, the queue grows; if it is down, messages wait. In the lesson, a Fraud worker that handles about 50 checks per second against 80 arriving only builds a backlog, and stopping it for two seconds fails no order.

Request-reply over messaging. When the producer eventually needs the outcome, the consumer publishes a reply to a reply queue or an event, carrying a correlation id (here the order id). The producer updates its state when the reply arrives: PENDING becomes CONFIRMED or CANCELLED.

When to use it

  • Synchronous: the caller cannot continue without the answer (reads, validations, a price shown to a user), the operation is quick, and the chain is short. Use gRPC for internal, high-volume, typed service-to-service calls; REST for public APIs, browsers and broad compatibility.
  • Asynchronous: the work can finish after the user has been answered (fraud checks, emails, invoicing, search indexing), several consumers need the same event, load is bursty and should be smoothed, or the consumer must be allowed to be down.

When not to

  • Do not make a call async when the user needs the result now and there is no meaningful "pending" state; you only add a reply channel and polling.
  • Do not build deep synchronous chains (A → B → C → D) for one user request; each level multiplies failure modes. Collapse them, cache, or move parts off the request path.
  • Do not use a queue as a remote procedure call with a blocking wait for the reply; it adds latency and keeps the temporal coupling.

Trade-offs

Choice You gain You pay
Synchronous call Immediate answer, simple control flow, easy errors Temporal coupling; latency and failures propagate; caller capacity tied to callee speed
REST/JSON Universal tooling, browsers, human-readable Larger payloads, looser contracts, one request per HTTP/1.1 connection at a time
gRPC/protobuf Typed contracts, compact payloads, multiplexing, streaming, deadlines Browser support via proxy, harder debugging, L7 load balancing needed
Asynchronous messaging Availability and load decoupling, fan-out, smoothing bursts Eventual consistency, pending states, at-least-once duplicates, broker to operate, harder tracing

Alternatives

  • Do not call at all: replicate the data a service needs into its own store via events, so the request path reads locally.
  • Batch or cache the callee's answers so most requests skip the call.
  • GraphQL or a backend for frontend to aggregate several calls for a client in one place.
  • Event streaming (a partitioned log such as Kafka) instead of a queue when consumers need replay and ordering per key.

Failure modes

  • Cascading failure: a slow dependency exhausts worker pools up the chain. Mitigate with timeouts below the caller's own deadline, circuit breakers, bulkheads (separate pools per dependency) and load shedding.
  • Retry storms: every layer retrying multiplies the load on the struggling service; retry only idempotent calls, at one layer, with backoff and budgets.
  • Backlog growth: an async consumer slower than the arrival rate grows its queue without bound; monitor queue age, scale consumers, and set retention limits.
  • Lost or duplicated messages: at-least-once delivery means duplicates; publishing next to a database write needs an outbox.
  • Long-lived HTTP/2 connections pinned to one backend leave new instances idle unless balancing is per request.

In production

Propagate a deadline or timeout budget through synchronous calls (gRPC does this natively), and set each hop's timeout lower than its caller's. Give each dependency its own connection pool so one slow callee cannot take every thread. Emit traces across both sync calls and messages (put the trace context in message headers). For messaging, publish via an outbox, make consumers idempotent, alert on the age of the oldest message rather than queue length alone, and send poison messages to a dead letter queue.

Common misconceptions

  • "gRPC is asynchronous." gRPC has async client APIs, but the interaction is still request/response: the callee must be up and answer.
  • "A slow service only hurts its own latency." Its callers pay with their worker capacity; that is how slowness becomes outages.
  • "Async is always faster." It answers the user sooner, but the work itself finishes later, and the end-to-end outcome may take longer.
  • "A queue makes the system highly available." It moves the dependency to the broker, which then must be replicated and monitored.

Interview angle

For each arrow in your design, say whether it is sync or async and why: does the caller need the answer now? Point out deep synchronous chains and explain how a slow leaf cascades upward, then name the protections (timeouts, breakers, bulkheads) or move the call off the request path. Choose REST for public and browser-facing APIs and gRPC for internal high-throughput calls, mentioning HTTP/2 load-balancing implications. For async flows, mention pending states, correlation ids, outbox publishing and idempotent consumers.

Model assumptions

Parts of this lesson are simulated and parts are scripted. Simulated by the queueing engine: a checkout sending 80 orders per second (random arrivals, 2 s timeout); Orders and Pricing with 24 workers and 5 ms of work each, Fraud with 8 workers and 40 ms (160 ms when four times slower) and short queues that reject when full; each caller holds a worker while it waits for its callee. In the async design, Pricing's publish is acknowledged by the queue and a Fraud worker with 8 slots consumes it. Hops between services take 2 ms, the checkout link 20 ms. Scripted (hand-placed packets): the REST versus gRPC request patterns and the asynchronous verdict returned through the Fraud Verdicts queue; the engine does not model protocols, encodings, connection multiplexing or reply channels. Numbers show behaviour and orders of magnitude, not benchmarks.

The lesson as textEvery step of the simulation, written out.
  1. 1. Three services, one request

    A checkout sends 80 orders per second. Orders asks Pricing for the total, and Pricing asks Fraud for a risk score before it answers. Every call is synchronous request/response: the caller sends a request and keeps one of its workers waiting until the answer comes back.

    While everything is healthy this is simple and fast. The order’s latency is the sum of every hop and every service’s work: about 100 ms, most of it the network to the checkout and Fraud’s 40 ms of work. Nothing fails.

    Scene: The checkout sends orders to Orders, which calls Pricing, which calls Fraud, each waiting for the answer. Orders take about 100 ms and none fail.

    Key idea: Synchronous means the caller waits: its latency includes everything below it.

  2. 2. REST or gRPC on the wire

    Synchronous calls come in two common flavours. REST sends JSON over HTTP, described by an OpenAPI document; it works from any language and any browser. Over HTTP/1.1 a connection carries one request at a time, so the first three packets go strictly one after another; clients open a pool of connections to get concurrency.

    gRPC sends Protocol Buffers over HTTP/2, generated from a .proto contract. One connection carries many streams at once: the last three requests leave together and their answers return together. gRPC adds streaming and deadlines that propagate to the callee, but browsers need a proxy (gRPC-Web), and binary payloads are harder to inspect. Both are still synchronous: the caller waits either way. *Scripted illustration.*

    Scene: Orders sends three REST requests to Pricing one after another, each waiting for the previous answer. Then it sends three gRPC requests on one HTTP/2 connection at the same time, and the three answers return together.

    Key idea: REST and gRPC differ in contract, encoding and connection use; both keep the caller waiting.

  3. 3. A slow service deep in the chain

    Traffic is back. One second in, Fraud becomes four times slower (a slow database query, a noisy neighbour). It does not crash; it just takes 160 ms per check instead of 40, more than its 8 workers can keep up with, so calls start queueing inside Fraud.

    Watch the services *above* it. Pricing does 5 ms of work per call, yet all its workers are busy: they are waiting on Fraud. Then Orders’ workers fill up waiting on Pricing, and Orders starts rejecting orders at the top of the chain. A slowdown in one service has used up the capacity of every service that calls it. This is a cascading failure.

    Scene: Fraud becomes four times slower and calls queue up inside it; Pricing and then Orders run out of workers because they are all waiting, and Orders begins to reject orders. The error rate climbs.

    Key idea: In a synchronous chain, a slow callee exhausts its callers’ workers: the slowdown climbs to the top.

  4. 4. Put a queue in front of Fraud

    Does Pricing really need the fraud score before it answers? Here it does not: the order can be accepted and checked a moment later. So Pricing now publishes a message to a Fraud Checks queue and answers at once; a Fraud worker consumes the queue at its own pace.

    Fraud is still four times slower, but no order fails and latency is back to normal, because no request waits for Fraud any more. The slowness has moved somewhere harmless: the queue grows, since 8 slow workers handle about 50 checks per second and 80 arrive.

    Scene: Pricing publishes fraud checks to a queue and answers immediately. The slow Fraud worker falls behind, so the queue grows, but no order fails and latency is normal.

    Key idea: A queue takes the consumer out of the request path: its slowness becomes a backlog instead of errors.

  5. 5. The consumer can even stop

    Fraud is fixed, then one second in its worker stops completely for two seconds (a bad deploy). With a synchronous call every order would fail; here orders keep succeeding and the checks wait in the queue.

    When the worker comes back it works through the backlog much faster than new checks arrive, and the queue drains. Producer and consumer no longer need to be up at the same time: that is temporal decoupling. The queue itself is now a dependency, which is why brokers run replicated.

    Scene: The Fraud worker stops for two seconds. Orders keep succeeding while fraud checks pile up in the queue; when the worker returns it drains the backlog.

    Key idea: Asynchronous messaging decouples availability: the consumer can be down without failing the producer.

  6. 6. The answer comes later

    Asynchrony has a price: the checkout gets “order received” before anyone knows whether it is fraudulent. The verdict travels back as another message, on a Fraud Verdicts queue, carrying the order id as a correlation id, and Orders updates the order from PENDING to CONFIRMED or CANCELLED.

    So the business process must have that pending state, the user must be told later (email, push, a status page) and a late rejection must be handled. This is request-reply over messaging: reliable and decoupled, but eventually consistent. *Scripted illustration.*

    Scene: The checkout’s order goes through Orders and Pricing, which publishes a fraud check and answers. The checkout receives “order received” while the Fraud worker is still checking; later its verdict travels through the Fraud Verdicts queue back to Orders.

    Key idea: Going async turns one answer into two: “accepted” now, the outcome later, tied together by a correlation id.

  7. 7. Choose per interaction

    Use synchronous calls (REST, gRPC) when the caller truly needs the answer to continue: reads, validations, anything the user is waiting for. Protect them with timeouts shorter than the caller’s own deadline, retries with backoff only for idempotent calls, circuit breakers and bulkheads, and keep chains short.

    Use asynchronous messaging (queues, events) for work that can finish later: commands that may wait, notifications, fan-out to several consumers. Accept the backlog, the pending states and at-least-once delivery that come with it. Most systems use both, chosen call by call.

    Scene: The checkout calls Orders and Pricing synchronously; Pricing hands fraud checks to a queue that the Fraud worker consumes, and verdicts flow back to Orders. Everything is healthy.

    Key idea: Sync for answers you need now, async for work that can wait; decide per call, not per system.