Microservice Communication

An online shop runs as four separate services: order, inventory, payment, and email. Each service runs its own program on dedicated servers with its own database. When a customer presses "Place order", the order service must confirm that the item is in stock, charge the card, and send a confirmation email. Because the data and logic for those steps belong to the other three services, the order service cannot read their databases directly. Instead, it must ask them over the network.

Why Services Must Talk

Splitting one application into small services, each owning its own data, is called a microservice architecture. The next lesson compares this approach with a monolith, where the entire application runs as a single unit. For communication, one fact matters: a single user action now crosses several services.

Inside a single program, the order code calls reserveStock() as an ordinary function. Across services, that same step becomes a network call where the order service sends a request to the inventory service and reads the reply. A network call differs from a local function call in three ways:

Function call in one processNetwork call between services
TimeNanosecondsAbout half a millisecond for one round trip inside a data center (Dean, 2009), plus the work of the other service
FailureReturns or throws an errorCan also time out, or find the other service down
Unknown resultDoes not happenThe work can succeed while the reply is lost, so the caller does not know the result

The rest of this course focuses on these three differences. A service can make a network call in two ways: wait for the answer, or continue without waiting.

Synchronous Communication

In synchronous communication, the caller sends a request and waits for the response before continuing. The order service asks the inventory service to reserve the item and pauses until the answer returns, then calls the payment service and waits again. HTTP with REST and gRPC are the common protocols for these calls, and the REST, GraphQL, and gRPC lesson compares them.

This execution model is direct because the code reads in order, and any error returns at once so the caller can show it to the user. The trade-off is that the caller depends on every service it calls in three ways:

  • Latency adds up. The customer waits for every call in the chain to finish sequentially. If inventory takes 30 ms, payment takes 120 ms, and email takes 80 ms (illustrative figures), checkout takes at least 230 ms.
  • Availability multiplies. Checkout succeeds only when all four services are working. If each service is up 99.9% of the time, checkout is up about 0.999⁴ ≈ 99.6% of the time, as explained in the Availability lesson.
  • The caller holds resources while it waits. Each waiting request occupies a thread or connection in the order service, so a slow callee can exhaust the caller's capacity. The Synchronous Communication lesson traces this failure step by step.

Asynchronous Communication

In asynchronous communication, the caller sends a message and continues without waiting for the work to finish. The message usually goes through a message queue, which stores each message until another service reads and processes it. The sending service is the producer, and the service that reads the message is the consumer.

The order service does not need the confirmation email to go out before answering the customer. Instead, it puts an event on the queue—a message recording what happened, such as OrderPlaced {order_id: 123}. Once the queue confirms that the message is stored, the order service replies to the customer, and the email service processes the event later.

This decouples the sender from the consumer. If the email service is slow or down, orders still succeed while messages wait in the queue. A single event can also feed multiple consumers; for example, an analytics service can read the same OrderPlaced event without changing the order service.

Asynchronous communication introduces its own trade-offs:

  • No answer now. Because the sender does not receive the result right away, this model cannot handle steps whose output is required immediately, like checking stock.
  • The work happens later. For a brief window, the order exists before the confirmation email is sent. The system is eventually consistent, meaning all services agree once the queued messages are processed.
  • A message can arrive twice. Most queues redeliver a message if the consumer crashes before confirming completion. The consumer must handle duplicates without sending two emails, a technique covered in the Idempotency lesson.
  • One more component to run. The queue requires capacity planning, monitoring, and an operational owner. Tracing a request across a queue is also harder than following a direct call chain.

Comparing the Two on One Checkout

The widget runs the same checkout flow through both designs at 10 orders per second. In the second design, only the email step goes through a queue, while inventory and payment remain synchronous. Press Play, then press "Stop email service", and start it again to watch the queue drain. Next, move the email latency above 200 ms, and finally stop the payment service.

t = 0s
Email service latency80 ms
Synchronous: the order service waits for every call
OrderserviceInventory30 msPayment120 msEmail80 ms
Response time: –orders OK 0/sorders failed 0/semails sent 0/s
Orders per second appear here
Bars: orders per second, green OK and red failed, last 20s.
Asynchronous email: the order service puts a message on a queue
OrderserviceInventory30 msPayment120 msQueue0 waitingEmail80 ms
Response time: –orders OK 0/sorders failed 0/semails sent 0/squeue 0
Orders per second appear here
Bars: orders per second, green OK and red failed, last 20s. Purple line: messages in the queue (top of the chart = 10).
All services run. Synchronous checkout takes 230 ms (inventory + payment + email). Asynchronous checkout takes 155 ms, because it does not wait for the email. Press Play, then stop the email service.
10 checkouts per second. Inventory takes 30 ms, payment 120 ms, and a queue write 5 ms. The email service has 2 workers, so it sends 2000 ÷ latency emails per second. All numbers are illustrative.

When the email service stops, the synchronous design fails every order, even though the customer only needed to place the order. The asynchronous design accepts every order and holds the messages in the queue until the email service recovers. A slow email service also slows down synchronous checkout for every customer. Above 200 ms per email, the email service cannot keep up with 10 orders per second across its two workers (2 ÷ 0.2 s = 10 emails per second). At that point, the synchronous design starts failing orders. In the asynchronous design, checkout keeps its response time while the queue grows and emails arrive late.

Stopping the payment service shows the limit of a queue. Both designs fail because the order service cannot confirm an order without knowing the payment result. A queue helps only when the caller does not need the result right away.

Choosing Between Them

The widget illustrates the decision rule: if the caller needs the result to answer the current request, use a synchronous call. If the work can finish later, make it asynchronous.

StepDoes the caller need the result now?Communication
Check that the item is in stockYes, the order depends on itSynchronous
Charge the cardYes, the order depends on itSynchronous
Load a user's profile to show a pageYes, the page shows the dataSynchronous
Send the confirmation emailNoAsynchronous
Update the search index or analyticsNoAsynchronous
Transcode an uploaded videoNo, and it takes minutesAsynchronous, the client checks the status later

Most production systems combine both approaches. The order service makes synchronous calls along the path the user waits on, and publishes events for work that can finish later. A third option is having two services share one database so one service reads the rows the other writes. While this avoids a network call, both services become coupled to the same table layout, so neither team can change the schema independently. The Database Per Microservice lesson explains why each service owns its own data.

What This Course Covers

The following lessons examine each part of this architecture in detail:

For a deeper look at message formats and broker types, see Asynchronous Communications Through Messaging.

Key idea. A synchronous call waits for the response, causing latencies to add up along the chain and callers to fail when a callee fails. An asynchronous message goes to a queue so the sender can continue even if the consumer is slow or down, though the work finishes later and messages can arrive twice. Use a synchronous call whenever the caller needs the result to answer the current request.

Further Reading