Skip to content

Synchronous or asynchronous: who waits for whom

Every call between two parts of a system makes the same choice. In a synchronous call, the caller sends a request and cannot go on until the answer comes. In an asynchronous hand-over, the sender passes a message on and goes on, and the work or the answer comes later. Microsoft’s microservices guidance puts it this way: HTTP is a synchronous protocol, because the client waits for a response, while a protocol such as AMQP sends asynchronous messages and the sender usually does not wait for a response.

The guidance adds a second axis: whether a message has one receiver or several. A request that must be processed by exactly one receiver is single-receiver communication, and the guidance’s example is the Command pattern. When a request may be processed by zero or more receivers, the guidance says this type of communication must be asynchronous, as in publish/subscribe, which the messaging module built.

Mental model. A synchronous call is a phone call: you cannot do anything else on that line until the other person answers. An asynchronous hand-over is a letter: you post it and carry on, and the reply, if there is one, comes some other way.

Synchronous calls compose badly. When service A waits for B, and B waits for C, the caller of A waits for all three. Microsoft’s guidance calls a long request chain between services an anti-pattern: the services stop being autonomous, and the overall response time gets worse with every synchronous dependency, as soon as one service in the chain performs badly.

Availability suffers too. If a request succeeds only when every service it waits for succeeds, the availabilities multiply, as the availability lesson shows. Say each service is up 99.9% of the time and they fail independently:

  • Three in a row: 0.999 × 0.999 × 0.999 = 0.997, or 99.7%.
  • Five in a row: 0.999 to the power 5 is 0.995, or 99.5%.

Each service alone looks excellent, and a request through five of them fails about five times as often as a request through one. The guidance states the goal as each service being available to its clients even when the other services are down or unhealthy. Anything on the path that the caller waits for takes some of that away. This is arithmetic under an assumption of independence, and a shared dependency would make the figures worse, not better.

An asynchronous design moves the work out of the request. There are two common forms.

Hand-over with a queue. The service saves what it must and puts a message on a queue. It answers the user at once. Another service reads the message when it is ready, and if it is down for an hour, the message waits. The sign-up form does not fail because the mail service is down. The price is the one the messaging module described: the message may be delivered more than once, so the receiver must be idempotent, and the system is only eventually consistent. Microsoft’s guidance recommends copying the data that a service needs from the services that own it, and accepts eventual consistency, instead of asking for it with a synchronous call on each request.

Accepted now, done later. The caller makes a normal HTTP request for work that takes a long time. The server checks it, starts the work and answers at once. RFC 9110 has a status for this: 202 Accepted, meaning that the request has been accepted for processing but the processing has not been completed. It also says that there is no facility for the server to send a status code later, so the answer must come another way. The caller can poll an address that the server gave, as in the polling lesson, keep a channel open, or be called back with a webhook. The request is synchronous and short, and the outcome is asynchronous.

The two forms answer the question “how does the caller find out?” differently. A hand-over with a queue often never tells the caller, because nobody asked. A request that has been accepted tells it, by whichever of the three ways the design picked.

Eight situations. For each, ask what the caller can usefully do while it waits.

Challenge

Wait for the answer, or hand it over?

A synchronous call waits for the answer before it goes on. An asynchronous hand-over sends a message and goes on, and the work is done later. Put each situation under the style that fits it.

Sorting needs JavaScript.

Categories

Synchronous call
The caller needs the answer now to go on.
Asynchronous hand-over
The caller can go on, and the work or the answer can come later.
  1. Line 1 of 8While a user types a new username, the form shows whether it is already taken.
  2. Line 2 of 8After sign-up, a welcome email is sent.
  3. Line 3 of 8A checkout page needs to know whether the card was accepted before it shows the order as confirmed.
  4. Line 4 of 8A user asks for a report that takes ten minutes to build.
  5. Line 5 of 8An order has been placed, and billing, email and analytics must each hear about it.
  6. Line 6 of 8A product page needs the current price of the item to draw itself.
  7. Line 7 of 8A user uploads a photo, and thumbnails in three sizes must be made.
  8. Line 8 of 8Before a record is deleted, the service asks whether the user may delete it.

Check your answers at least once before looking at the answers.

What happens if everything is asynchronous?

Section titled “What happens if everything is asynchronous?”

Handing over is not free. A request that was once one call, with one answer and one error, becomes several steps in different places. The user may see “order received” and learn later that the card was refused. A message can arrive twice or late, and a failure shows up in a queue, not in a response. A system that answers “accepted” to everything has moved its errors out of sight, which is only an improvement if someone looks at the dead-letter queue.

The balance is a choice per call, not per system. Wait when the next thing that the caller does depends on the answer, and set a deadline for it. Hand over when the work can happen after the caller has moved on, when several services need the same event, or when a slow or absent receiver must not make the sender fail. Microsoft’s guidance gives the common mix: synchronous request and response for most queries, and lightweight asynchronous messaging for updates that cross several services.

  • A synchronous call makes the caller wait for the answer. An asynchronous hand-over lets the sender go on and the work happen later.
  • A request that waits on several services is only as available as the product of their availabilities: three at 99.9% give 99.7%, and five give 99.5%.
  • A chain of synchronous calls also adds up response times, and one slow service slows all of them.
  • A queue lets the work happen later and brings duplicates and eventual consistency. A 202 Accepted answer lets the caller poll, hold a channel or be called back.
  • Choose per call: wait when the next step needs the answer, with a deadline; hand over when it does not, or when several services need the same event.

Next: recap and quiz, the module on one page.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.