You post a photo on a social app. You have two million followers.
Should the app make you wait until all two million followers have been notified, and only then say “Posted!”? Or should it say “Posted!” straight away, and notify your followers in the background over the next few seconds?
Everyone picks the second one. And without noticing, you’ve just made a communication model decision: you chose asynchronous over synchronous.
In the last two articles we split one application into many services spread over a network. Now those services have to talk to each other. How they talk decides how fast your system feels, how far one failure spreads and how easily it grows. This guide explains the three communication models in plain English, with a simple rule for when to use each.
Why communication models matter
Services in a distributed system don’t share memory. When the Order service needs something from the Payment service, it has to send a message over the network. That raises three questions:
- Does the caller wait for the answer, or keep going?
- What happens to the caller’s thread while it waits?
- What happens if the other service is slow, or down?
The way you answer them shapes four things:
| It affects… | Because… |
|---|---|
| Speed | Waiting adds time to every request |
| Reliability | If you wait on a broken service, you break too |
| Coupling | The more services know about each other, the harder they are to change |
| Cost | Waiting threads use memory and server capacity while doing nothing |
The three communication models at a glance
People often mix these up, so here is the whole map first. There are three separate questions, and each one has two answers:
- Synchronous vs asynchronous is about the caller: does it wait for the result?
- Blocking vs non-blocking is about the thread: does it sit idle while it waits?
- Request/response vs events is about the relationship: does the sender know who receives the message?
Let’s take them one at a time.
Model 1: Synchronous vs asynchronous communication
Synchronous: call and wait
In synchronous communication, the caller sends a request and waits for the response before doing anything else.
Think of a phone call. You dial, you wait for an answer, you talk, you get your reply. If nobody picks up, you’re stuck holding the phone until you give up.
// Java · CheckoutController.java (synchronous)
@PostMapping("/checkout")
public CheckoutResponse checkout(@RequestBody CheckoutRequest request) {
if (!inventoryClient.isAvailable(request.productId())) { // wait for inventory
return CheckoutResponse.outOfStock();
}
PaymentResult payment = paymentClient.charge(request.payment()); // wait for the payment gateway
if (!payment.success()) {
return CheckoutResponse.paymentFailed();
}
return CheckoutResponse.success(payment.transactionId()); // the user gets a clear answer
}
Checkout is synchronous for good reasons: the user needs to know right now whether the payment worked, and the steps must happen in order.
Asynchronous: send and move on
In asynchronous communication, the caller hands off the work and doesn’t wait for it to finish. It gets back a quick “got it” and carries on.
Think of sending an email. You hit send and get on with your day. The reply comes later, whenever it comes.
// Java · VideoController.java (asynchronous)
@PostMapping("/videos")
public ResponseEntity<UploadResponse> upload(@RequestParam("file") MultipartFile file) {
String videoId = UUID.randomUUID().toString();
String url = storage.upload(videoId, file); // save the raw file
kafka.send("video-uploads", new VideoUploaded(videoId, url)); // hand off the slow part
return ResponseEntity.accepted().body(new UploadResponse(videoId, "Processing")); // 202, right away
}
// Java · a separate service, running in the background
@KafkaListener(topics = "video-uploads")
public void encode(VideoUploaded event) {
// converting the video can take minutes: nobody is waiting on it
videos.markReady(event.videoId(), encoder.encode(event.url()));
}
Encoding a video takes minutes. Making the user stare at a spinner for that long would be terrible, so the API replies with 202 Accepted at once, and the work happens in the background.
Synchronous vs asynchronous: trade-offs
| Synchronous | Asynchronous | |
|---|---|---|
| The caller… | waits for the result | gets “accepted” and moves on |
| Feedback | immediate and clear | later, by polling or a notification |
| If the other side is down | the caller fails too | work waits in a queue until it’s back |
| Coupling | tight: both must be up at once | loose: they don’t need to be up together |
| Consistency | easy: you know the result | eventual: the result shows up later |
| Debugging | easy to follow | harder: the work hops between services |
| Extra infrastructure | none | a queue or message broker |
Use synchronous when the user needs the answer to continue: login, payments, checking stock, validating an address.
Use asynchronous when the work is slow or the user doesn’t need the result right now: sending emails and push notifications, encoding video, generating reports, updating analytics.
“Asynchronous doesn’t make the work faster. It makes the waiting someone else’s problem.”
Model 2: Blocking vs non-blocking I/O
The first model was about the caller. This one is about what happens inside the server: what does the thread do while it waits for the network?
Blocking I/O: one thread per waiting request
With blocking I/O, the thread that sends a request sits idle until the answer comes back. It can’t do anything else in the meantime.
// Java · blocking
String user = restClient.get().uri("/users/123").retrieve().body(String.class);
// this thread does nothing until the reply arrives
System.out.println(user);
It’s simple to write and easy to debug: the code reads top to bottom. The catch is that every waiting request holds a thread. With 1,000 requests waiting on a slow service, you need 1,000 threads.
Non-blocking I/O: one thread, many requests
With non-blocking I/O, the thread sends the request, registers “call me when the answer arrives,” and goes off to handle other work. A small number of threads can look after thousands of open requests.
Think of a restaurant. A blocking waiter takes one order, stands at the kitchen door until the food is ready, then serves it. A non-blocking waiter takes your order, passes it to the kitchen, and serves other tables until the kitchen rings the bell.
A great use is calling several services in parallel. With Spring WebFlux’s WebClient:
// Java · ProfileController.java (non-blocking, three calls at once)
@GetMapping("/users/{id}/profile")
public Mono<UserProfile> profile(@PathVariable String id) {
Mono<User> user = web.get().uri("http://user-service/users/{id}", id)
.retrieve().bodyToMono(User.class);
Mono<List<Order>> orders = web.get().uri("http://order-service/orders?userId={id}", id)
.retrieve().bodyToFlux(Order.class).collectList();
Mono<Preferences> prefs = web.get().uri("http://preference-service/preferences/{id}", id)
.retrieve().bodyToMono(Preferences.class);
return Mono.zip(user, orders, prefs) // all three run at the same time
.map(t -> new UserProfile(t.getT1(), t.getT2(), t.getT3()));
}
If each call takes 100 ms, the blocking version done one after another takes about 300 ms. This one takes about 100 ms, and no thread sits idle while waiting.
Blocking vs non-blocking: trade-offs
| Blocking I/O | Non-blocking I/O | |
|---|---|---|
| Code style | simple, top to bottom | callbacks, futures or reactive streams |
| Debugging | easy, clear stack traces | harder, stack traces jump around |
| Threads needed | one per waiting request | a few for many requests |
| Under heavy load | threads run out, requests queue | handles many more open requests |
| Best for | ordinary apps, CPU-heavy work | gateways, chat, streaming, lots of slow I/O |
Don’t confuse model 1 and model 2
These two are often mixed up, but they are different questions:
- The profile endpoint above is synchronous for the user (they wait for their profile) but uses non-blocking I/O inside.
- A Kafka consumer is asynchronous for the user who triggered the work, but it can use plain blocking code to process each message.
Sync vs async is what the caller experiences. Blocking vs non-blocking is how your threads behave. You choose each one separately.
Model 3: Request/response vs events
The last question is about who knows about whom.
Request/response: ask one service directly
In request/response, the caller sends a message to a specific service and expects an answer from it. The caller knows exactly who it is talking to. REST and gRPC calls work this way.
Think of asking a waiter for the menu. You ask a specific person, and you expect that person to bring it.
Events: announce what happened, anyone can react
In the event-driven model, a service publishes an event, a fact about something that happened, such as OrderPlaced. It doesn’t send it to anyone. It doesn’t know or care who is listening. Other services subscribe to the events they care about and react in their own time.
Think of a fire alarm. It doesn’t call each person. It just rings, and everyone who hears it reacts in their own way: people leave, the fire brigade is called, the lifts go to the ground floor.
// Java · OrderService.java (publisher: knows nothing about who listens)
public Order placeOrder(OrderRequest request) {
Order order = orders.save(new Order(request));
kafka.send("order-events", new OrderPlaced(order.id(), order.userId(), order.items()));
return order;
}
// Java · three separate services, each reacting on its own
@KafkaListener(topics = "order-events", groupId = "email")
public void sendConfirmation(OrderPlaced e) { emails.sendOrderConfirmation(e); }
@KafkaListener(topics = "order-events", groupId = "inventory")
public void reduceStock(OrderPlaced e) { inventory.decrement(e.items()); }
@KafkaListener(topics = "order-events", groupId = "analytics")
public void record(OrderPlaced e) { analytics.recordOrder(e); }
Notice what the Order service doesn’t do: it never calls the email, inventory or analytics services. Tomorrow you can add a loyalty-points service that listens to the same event without changing a single line of the Order service. And if the email service is down, orders still go through; the emails go out when it’s back.
Request/response vs events: trade-offs
| Request/response | Events | |
|---|---|---|
| The sender knows the receiver? | yes | no |
| Answer back? | yes, right away | no direct answer |
| Adding a new consumer | change the caller | just subscribe |
| One receiver is down | the caller sees an error | the others carry on |
| Following the flow | easy: read the code | harder: you need tracing and good event names |
| Best for | queries and commands that need an answer | telling others “this happened” |
Use request/response when you need an answer: “Is this item in stock?”, “Charge this card.”
Use events when you’re announcing a fact that several parts of the system might care about: “An order was placed.”, “A user signed up.”, “A video is ready.”
Putting it together: a food delivery order
Real systems mix all of these. The trick is to choose per step, not per system. Here’s how a food delivery app might handle one order:
// Java · OrderController.java (mixing both)
@PostMapping("/orders")
public ResponseEntity<OrderResponse> placeOrder(@RequestBody OrderRequest request) {
PaymentResult payment = payments.charge(request.payment()); // SYNC: the user must know
if (!payment.success()) {
return ResponseEntity.badRequest().body(OrderResponse.paymentFailed());
}
Order order = orders.save(new Order(request, payment));
events.publish(new OrderPlaced(order)); // ASYNC: restaurant, rider, email...
return ResponseEntity.ok(OrderResponse.placed(order.id())); // reply now
}
The customer waits a second or two for the payment, which is fine because they need that answer. They don’t wait for the email, the analytics or the rider search.
What breaks, and how to fix it
Each model has its own way of failing. These four come up again and again:
| What breaks | What you see | The fix |
|---|---|---|
| A sync call with no timeout | A slow service freezes every caller, then theirs | Always set a connect and request timeout, plus a fallback |
| Too many blocking threads | Thread pool full, requests queue, then the server falls over | Bounded pools and queues, non-blocking I/O, or virtual threads |
| Callback hell in async code | Deeply nested callbacks that nobody can read or debug | CompletableFuture chains or reactive streams, with errors handled in one place |
| Lost events | The database saved the order, but the broker was down, so the event never went out | The transactional outbox: save the event in the same database transaction, and send it from there |
The first fix is the one that matters most. With Java’s built-in HTTP client:
// Java · never wait forever
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(2)) // how long to wait to connect
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create("http://inventory/stock/42"))
.timeout(Duration.ofSeconds(3)) // how long to wait for the answer
.build();
The last one, the transactional outbox, deserves a closer look. The problem: you save the order, then try to publish the event, and the broker is down. Now the order exists, but no email, no stock update, nothing. The fix: in the same database transaction as the order, write the event to an outbox table. A small background job reads that table and publishes each event, retrying until it succeeds. The order and its event either both exist or neither does.
Puzzle 1: “We made it async and it got slower”
Asynchronous doesn’t make the work faster. It makes the reply faster. The work still takes 5 seconds, plus a little extra for the queue. And now the user has to find out when it’s done: refresh the page, poll the API, or wait for a notification that arrives after 6 seconds.
If the user needs the result to continue, async just moves the waiting somewhere more confusing. Async is better when the user doesn’t need the result right now, not as a speed trick.
Puzzle 2: the synchronous chain
All four must work at the same moment, so you multiply: 0.999 × 0.999 × 0.999 × 0.999 ≈ 0.996. That’s 99.6%: about 2.9 hours of failures a month, instead of 43 minutes for one service.
Synchronous chains multiply failure. Keep them short. Make steps asynchronous where the user doesn’t need the answer, and use timeouts and circuit breakers on the calls that stay synchronous.
A quick way to choose
For each interaction, ask three questions:
- Does the user need this answer to continue? Yes: synchronous. No: asynchronous.
- Is this server mostly waiting on other services, under heavy load? Yes: consider non-blocking I/O or virtual threads. No: plain blocking code is fine.
- Am I asking for something, or announcing that something happened? Asking: request/response. Announcing: events.
Key takeaways
- There are three communication models, each answering a different question: does the caller wait, does the thread wait, does the sender know the receiver?
- Synchronous is simple and gives instant answers, but couples services and spreads failures. Use it when the user needs the result.
- Asynchronous frees the caller and isolates failures, but adds queues, delay and harder debugging. Use it for slow or background work.
- Non-blocking I/O (or Java’s virtual threads) lets a few threads handle thousands of waiting requests.
- Events let services react without knowing each other, so you can add new features without touching the publisher.
- Mix them. Choose per step, not per system: wait only for what the user must see.
Next in this thread: the protocols that carry these messages, REST, gRPC, GraphQL and WebSockets, and how to pick between them.
Comments