Course contents

07. Messaging and Event-Driven Systems Lesson 1 of 10

Synchronous vs Asynchronous Communication

See a synchronous service chain add up latency, spread one slow service upstream and multiply downtime, then decouple it with a queue.

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

Summary

  • A synchronous caller waits for everything it calls; latencies add up along the chain.
  • Waiting holds workers, so a slow dependency exhausts healthy callers upstream.
  • A synchronous chain is only as available as the product of its parts.
  • A queue takes deferrable work off the request path; slowness becomes backlog and delay.
  • Async APIs answer “accepted” and report the outcome later; consumers must handle at-least-once delivery.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

In synchronous communication the caller sends a request and waits for the answer before it continues: an HTTP or gRPC call, a database query. In asynchronous communication the caller hands the work to an intermediary (a queue, a topic, a log) and continues without waiting; someone else does the work later and the outcome is reported separately, if at all. The difference is temporal coupling: whether both sides have to be up, and fast, at the same moment.

Why it exists

Synchronous calls are simple: one code path, an immediate answer, errors returned to the caller. But in a chain of services, every call adds its latency and its failure probability to the caller’s request, and every waiting caller holds a thread, connection or worker. Asynchronous messaging exists to take work that does not have to finish now (emails, labels, indexing, analytics) off that path, so the user-facing request depends only on what it truly needs.

How it works

Latency adds up. In a chain A → B → C, A’s response time is its own work plus B’s full response time, which includes C’s, plus every network hop. In the lesson: 10 + 20 + 60 ms of work and six hops give a median of about 150 ms.

Slowness spreads upstream. A caller that waits holds a worker for as long as the callee takes. If C becomes eight times slower, B’s workers are busy eight times longer, B’s capacity collapses even though its code is fine, its queue fills, and A sees timeouts. Timeouts, bulkheads (separate pools per dependency) and circuit breakers limit this; they do not make the slow work faster.

Availability multiplies. If a request needs n independent services each available a, it succeeds with probability aⁿ: three services at 99.9% give about 99.7%, roughly three times the downtime of one. Real failures are often correlated (shared databases, deploys), which can make it worse.

A queue in between. The producer writes a message and answers once the broker has stored it. Consumers pull messages at their own pace and acknowledge them after processing. The request path no longer includes the work, so a slow or failed consumer shows up as backlog (queue depth, age of the oldest message) instead of user latency or errors. The queue absorbs differences in rate, but it does not add capacity: if consumers stay slower than producers, the backlog grows without bound until the queue’s limits are reached.

The asynchronous contract. The API answers 202 Accepted with an identifier and a way to learn the outcome: a status resource to poll, a webhook, a push notification. The result is eventually available, so the UI must show a pending state. Delivery is typically at-least-once, so consumers must be idempotent. Failures happen after the response and land in retries and a dead letter queue, so they need monitoring and a way back to the user.

When to use it

  • Synchronous when the caller needs the result to continue: price and stock checks, payment authorisation, reads that render the page. Keep the chain short and give every call a timeout.
  • Asynchronous for side effects that can finish after the response: notifications, shipping labels, search indexing, analytics, thumbnails, webhooks to third parties.
  • Asynchronous to absorb bursts and to isolate a flaky or slow third-party API from user traffic.

When not to

  • Don’t make a call asynchronous if the user must see its result immediately; you would only add polling and latency.
  • Don’t queue work to hide a capacity problem: a permanently growing backlog is an outage in slow motion.
  • Don’t introduce a broker for one simple call between two services you own when a timeout and a retry would do.

Trade-offs

Synchronous Asynchronous
Caller latency Includes all downstream work Only up to the enqueue
Failure of a dependency Caller fails or times out Backlog grows; caller unaffected
Availability Product of the chain Producer and broker only
Result Immediate, in the response Later: polling, callback, event
Consistency Done when the call returns Eventually consistent
Errors Returned to the caller Retries, DLQ, separate alerting
Operations Simple to trace Broker to run, backlog to watch, duplicates to handle

Alternatives

  • Timeouts, retries with backoff, circuit breakers and bulkheads keep synchronous calls but bound the damage of a slow dependency.
  • Publish/subscribe for one event with several independent consumers.
  • Event streaming (a log such as Kafka) when consumers need ordering per key or replay.
  • Transactional outbox to publish the message reliably in the same transaction as the database write.
  • Caching or precomputing the dependency’s answer so the synchronous call is avoided.

Failure modes

  • Cascading slowness: a slow synchronous dependency exhausts the workers of every caller above it.
  • Retry storms: callers retry timeouts and multiply the load on the struggling dependency.
  • Unbounded backlog: consumers fall behind permanently; messages age out or the queue fills and producers get errors.
  • Lost work: the service commits its database write but crashes before publishing the message (use an outbox).
  • Duplicates: at-least-once delivery processes the same message twice; non-idempotent consumers send two emails or two labels.
  • Invisible failures: asynchronous errors never reach the user unless the status model and alerts surface them.

In production

Draw the synchronous path of each user request and keep it as short as possible; give every call a timeout below the caller’s own. Move side effects behind a queue, published through an outbox. Alert on queue depth and oldest-message age, not just consumer errors, and autoscale consumers on backlog. Make consumers idempotent (dedupe by message or business id). Expose a status resource for anything the user cares about, and send failed messages to a dead letter queue someone actually watches.

Common misconceptions

  • "Async is faster." It makes the caller faster; the work itself takes as long, and often finishes later.
  • "A queue adds capacity." It moves the wait from the user to the backlog; consumers still need to keep up on average.
  • "If my service is healthy, a slow dependency is someone else’s problem." Your workers wait for it, so its slowness becomes yours.
  • "Messages are delivered exactly once." Most brokers deliver at least once; exactly-once effects need idempotent consumers.

Interview angle

For each step of a flow, ask "does the caller need this result now?" and say it out loud. Interviewers look for: latency budgets along the synchronous path, the availability product, timeouts and circuit breakers on remaining sync calls, a queue (with an outbox) for side effects, 202 Accepted with a status endpoint, idempotent consumers, and backlog monitoring. Mentioning that a queue absorbs bursts but not sustained overload is a strong signal.

Model assumptions

The simulation is a simplified model, not a benchmark. Shoppers arrive as a Poisson stream at 50 checkouts per second and give up after 1 second. The Checkout API (64 workers, 10 ms), Orders service (16 workers, 20 ms, 40 waiting) and Label service (16 workers, 60 ms, 30 waiting) are servers with lognormal service times; each holds a worker while it waits for the service it calls. "Slow" multiplies a node’s service time by eight. In the asynchronous version the Orders service answers as soon as the Label queue has stored the message; Label workers (8 slots, 60 ms) consume and acknowledge messages, with a 10-second visibility timeout. The 202 Accepted reply and the status polling flow are scripted illustrations; the engine does not model status stores or polling. Availability figures in the text are arithmetic, not simulated. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. Every hop waits for the next

    A shopper checks out. The Checkout API calls the Orders service, which calls the Label service, which asks a shipping carrier for a label. Each call is synchronous: the caller sends a request and holds a worker until the answer comes back.

    So the shopper waits for the sum of everything: about 10 + 20 + 60 ms of work plus six network hops. At 50 checkouts per second the median is around 150 ms, and every request succeeds.

    Scene: Shoppers call the Checkout API, which calls the Orders service, which calls the Label service. Each waits for the next. Median latency is about 150 ms and nothing fails.

    Key idea: In a synchronous chain, the caller’s latency is the sum of every service and hop below it.

  2. 2. One slow service, everyone waits

    One second in, the carrier gets slow: the Label service now takes eight times longer (about 480 ms). With 16 workers it can now serve only ~33 labels per second, fewer than the 50 checkouts arriving.

    Watch the Orders service: nothing is wrong with it, yet it becomes fully busy, because all its workers are stuck waiting for labels. Its own queue fills and it starts rejecting. Shoppers see latency climb to their 1-second timeout, and soon almost every checkout fails. A slow dependency deep in the chain has become everyone’s outage.

    Scene: The Label service becomes eight times slower and is fully busy; the Orders service’s workers are all held waiting for it, so it fills up and rejects too. Shopper latency reaches the 1 second timeout and many checkouts fail.

    Key idea: Synchronous calls propagate slowness upstream: callers’ workers are held while they wait, so a slow dependency exhausts healthy services.

  3. 3. Availability multiplies

    The carrier recovers, then one second in the Label service crashes. Every checkout now fails, even though orders could have been saved and the label printed later.

    In a synchronous chain the request needs every service to be up. If each is up 99.9% of the time and they fail independently, the chain is up about 0.999 × 0.999 × 0.999 ≈ 99.7%: three times the downtime of any one service. Each synchronous dependency you add multiplies in.

    Scene: The Label service goes offline after one second, and from then on every checkout fails because the chain cannot complete.

    Key idea: A synchronous request is only as available as the product of everything it calls.

  4. 4. Answer now, print the label later

    The shopper does not need the label to see "order confirmed". So the Orders service now saves the order, puts a message on a queue ("create label for order 123") and answers right away. Label workers consume the queue and call the carrier in their own time.

    The shopper’s path is now API → Orders → queue: the median drops well below the synchronous 150 ms and nothing fails. The label work still happens, just after the response.

    Scene: The Orders service puts a message on the Label queue and answers immediately; Label workers consume the messages and call the carrier. Shopper latency drops well below 150 ms and nothing fails.

    Key idea: Asynchronous messaging takes non-essential work off the request path: the caller’s latency no longer includes it.

  5. 5. The same carrier outage, decoupled

    Half a second in, the carrier is slow again: the Label workers become eight times slower and can process only ~17 labels per second while 50 arrive. Four seconds later the carrier recovers.

    The shopper notices nothing: latency stays where it was and no checkout fails. The trouble moves to where it belongs: the queue backlog grows while the workers are slow and drains once they recover. Labels are created late, not lost. Watch the queue depth, and the age of its oldest message, to know how late.

    Scene: The Label workers slow down for four seconds. Shopper latency and errors do not change; the Label queue’s backlog grows while the workers are slow and drains after they recover.

    Key idea: With a queue, a slow consumer shows up as backlog and delay, not as user-facing latency or errors.

  6. 6. Telling the client “not done yet”

    Asynchronous work changes the contract. The API no longer says "here is your label", it says "accepted": HTTP 202 Accepted with an order id and a status URL. The client finds out later, by polling the status or by being notified (webhook, push, email).

    The animation is a scripted illustration of that flow: checkout returns 202 at once, a Label worker records "label ready" in the order status store, and the client’s next status poll reads it. The live traffic behind it is still simulated.

    The price of async: the result is eventually there, so the UI must show "processing"; messages are delivered at least once, so the worker must be idempotent; and failures surface later, in a dead letter queue rather than an error page.

    Scene: An illustration of asynchronous request-reply: a checkout gets an immediate 202 Accepted reply while its message waits in the queue; a Label worker later writes the result into the Order status store, and the shopper’s next status request reads it.

    Key idea: Async APIs answer “accepted” and report the outcome later; design status, idempotency and failure handling for it.

  7. 7. Choosing per call

    Use synchronous calls when the caller needs the answer to continue (price, stock check, payment authorisation) and keep that chain short, with timeouts and fallbacks. Use asynchronous messaging for work that can finish after the response (labels, emails, indexing, analytics), and give it a status, idempotent consumers and backlog alerts.

    The question is never "sync or async?" for a whole system, but for each dependency: does the user need this result *now*?

    Scene: Shoppers call the Checkout API and the Orders service synchronously; label creation runs asynchronously through the Label queue and Label workers, which record results in the Order status store. Everything is healthy.

    Key idea: Keep only what the caller needs right now on the synchronous path; move the rest behind a queue.