# Communication: How System Parts Talk, and When to Wait

> Learn how clients, services, and databases exchange data over a network, and how waiting for a reply or handing off work changes speed and reliability.

**In short:** System parts communicate over a network, either by waiting for a reply or by handing off work, and that choice changes speed and reliability.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/communication

## What You Will Learn

The parts of a system must talk to each other. A phone app talks to a server, the server talks to a database, and one service may ask another service for help. This lesson explains how those parts send data over a network. It also shows how the style of talking changes speed, reliability, and how closely the parts depend on each other. By the end, you should be able to decide when a caller should wait for an answer and when it should hand off the work and move on.

- **Network call:** One part of a system asking another part for something over a network, such as cables or Wi-Fi between machines.
- **Function call:** A step where code calls a routine inside the same program. It runs on the same machine and usually fails in a clear way.
- **Synchronous:** A style where the caller waits for an answer before it continues.
- **Asynchronous:** A style where the caller hands off the work and continues. Any reply comes later.
- **Request/response:** An exchange where one side sends a request and the other side sends back a response.
- **Message:** A small package of data sent from one part to another, or to a middle program that stores it.
- **Message broker:** A middle program that holds messages and delivers them to the parts that need them.
- **Latency:** The delay between sending something and getting the answer back.
- **Timeout:** A limit on how long a caller waits before it gives up.
- **Coupling:** How much one part depends on another part being up, fast, and behaving a certain way.

## Why Are Network Calls Not Function Calls?

A function call inside one program is fast and predictable. The code jumps to a routine, the routine runs, and the answer comes back almost at once. A network call works very differently. The message must travel through cables, routers, and other machines before it reaches the receiver, and the reply must travel back the same way. Each part of that trip can go wrong.

- **Slower:** Even a short trip across a network takes longer than a step inside one program. The delay grows when the receiver is far away or busy.
- **Can fail or time out:** The message may be lost, the receiver may crash, or the reply may never arrive. The caller must choose how long to wait before it gives up.
- **Can arrive twice or out of order:** A sender that hears nothing may send again, so the receiver can get the same message twice. Two messages sent one after another can also arrive in the reverse order.

The hardest case is the unknown result. A caller may receive success, receive a clear failure, or receive nothing at all. When nothing arrives, the caller often cannot tell whether the work was done. Because of this, a good design assumes failure from the start instead of treating it as a rare surprise.

### What Are the Eight Fallacies?

Engineers at Sun Microsystems began a list of common wrong beliefs about distributed systems. A distributed system is one split across several machines that work together. Peter Deutsch wrote the first seven items in 1994, and James Gosling added an eighth around 1997. The list includes the beliefs that the network is reliable, that latency is zero, and that the network is secure. Each belief is risky to rely on, so the list works as a checklist of what not to assume.

A fallacy in this sense does not mean every network call fails. It means a builder cannot count on success for every call in every setting. Most calls work, but a design that ignores the rest will break at the worst moment.

## What Are the Two Ways to Talk, Waiting or Handing Off?

Most communication between parts falls into two styles. In a synchronous call, the caller sends a request and waits until the reply arrives before it does anything else. In an asynchronous call, the caller hands off the work and moves on, and the result comes back later, if it comes back at all.

### What Is a Synchronous Call Where the Caller Waits?

Synchronous request/response is easy to picture because it works like a question and an answer. A browser asks a server for a page and waits for the response. The flow is simple to follow, and the caller knows the result right away. The cost is that the caller is tied to the other side. If that side is slow or down, the caller waits too, and the user waits with it.

> **Tip:** Always set a timeout on a synchronous call. Without one, a slow receiver can hold the caller for a very long time.

### What Is an Asynchronous Message Where the Caller Hands Off?

In asynchronous messaging, the sender writes a message and gives it to a broker or queue. The sender then continues with its own work. A receiver picks up the message later, does the job, and may send a new message when it finishes. Because the sender does not wait, a slow receiver does not freeze it. The message can wait safely in the queue until the receiver is ready. The cost is that the result is not known at once. The user may see a 'received' message before the work is complete, so the team must track progress afterward.

## Trade-Offs: Speed, Coupling, and Failure

Choosing a style changes three things at once. Compare them using the cards below.

- **Speed:** A synchronous call adds the full round trip to the user's wait. An asynchronous hand-off returns quickly because the sender only has to store the message.
- **Coupling:** Synchronous calls tie the parts together in time, so both must be running when the call happens. Messaging loosens that tie, so a receiver can be offline for a while and still get the message later.
- **Failure handling:** A failed synchronous call fails the caller on the spot, and the caller must deal with it immediately. A queued message can be kept and tried again, but the team must then handle duplicates and jobs that never finish.

Chains make synchronous risk grow. Suppose a request waits on service A, which waits on service B, which waits on service C. Each link adds its own chance of failure. One source gives an example: five services, each up 99.9 percent of the time, called one after another in a blocking chain, give a combined ceiling near 99.5 percent. The number keeps falling with each extra step.

$$
0.999^3 \approx 0.997
$$

In words: zero point nine nine nine, multiplied by itself three times, is about zero point nine nine seven.

## Choosing a Style by Need

Do not pick one style for a whole system. Pick a style for each interaction. Ask four questions about the interaction.

1. **Who talks to whom?:** Does a person or browser need the answer on screen, or is one service simply reporting something to another?
2. **How much data moves?:** A small reply fits a simple call. Large batches of work may suit a queue, where the work is stored and processed in pieces.
3. **How fast is the answer needed?:** If the user must see the result now, use a synchronous call. If a result can arrive a little later, messaging keeps the user's screen quick.
4. **How tolerant is the flow of delay or loss?:** If the work must not be lost, a stored message that can be tried again is safer. If a late or missing answer would cause harm, a direct call with a clear timeout may be the better choice.

### Three Common Examples

- **A browser calls a server:** A user taps search and expects results right away. This is request/response, and the browser waits for the reply.
- **One service calls another:** A checkout service asks a stock service whether an item is available. Checkout needs that answer before it can go on, so it usually calls and waits.
- **A service publishes an event:** An order service announces that an order was placed. Email, analytics, and courier services can each react later. The publisher does not wait and does not need to know who listens.

## Worked Example: Placing a Food Delivery Order

Take a food delivery app. When you tap Place order, the screen must show a receipt quickly, yet many other tasks follow. The app can split the work so that only one step makes the screen wait.

1. **Send a synchronous request to confirm:** The app sends one request to the order service and waits. The service checks the order and saves it. The screen waits only for this step, then shows the receipt.
2. **Hand off the restaurant notice:** The order service places a message on a queue saying an order exists. The restaurant's system picks it up and shows the order when it is ready to read it.
3. **Hand off the courier search:** A courier service reads the same kind of message and starts looking for a driver. The app does not wait while this search runs.
4. **Hand off the email receipt:** An email service sends the receipt when it can. If the email service is slow, only the email arrives late. The screen has already moved on.

The sample below is a small stand-in for this idea. A list called outbox plays the part of the queue. The function saves the order, adds two jobs for later, and returns at once.

```python
from collections import deque
outbox = deque()
def place_order(order_id):
    outbox.append(('send_email', order_id))
    outbox.append(('notify_restaurant', order_id))
    return 'confirmed ' + order_id
print(place_order('A1'))
print(len(outbox))
for job in outbox:
    print(job)

```

Output:

```text
confirmed A1
2
('send_email', 'A1')
('notify_restaurant', 'A1')

```

In a real system, the queue is a separate program, and other services read the jobs. The main idea stays the same. The screen waits for the confirmation, but it does not wait for the restaurant, the courier, or the email.

## Check Your Understanding

Try these three short scenarios. Each one needs the ideas from this lesson.

**Quick check:** A checkout page freezes for 10 seconds whenever the email service is slow, because checkout calls the email service and waits for a reply. Which part of this communication would you change, and why?

A. Make checkout wait longer, so the email service always has time to reply
B. Send the confirmation email as an asynchronous message, so checkout does not wait for it
C. Remove the timeout from the email call, so the call never gives up
D. Move the email service onto the customer's phone

**Answer:** B. The purchase does not need the email to finish. Handing the email off to a queue lets checkout finish while the email service catches up.

**Quick check:** A shop page must show whether a size is in stock before the buyer can press Add to cart. Only the stock service knows the answer. Which style fits best?

A. Send a message and show 'in stock' by default while the answer is pending
B. Make a synchronous request, because the page needs the answer before its next step
C. Publish an event and ignore any reply
D. Send the question by message and let the buyer refresh the page later

**Answer:** B. The page cannot safely go on without the answer, so waiting for a direct reply is the right fit here.

**Quick check:** A ride app's trip service announces that a trip has ended. Billing, ratings, and a fraud check each need to react. None of them must reply to the trip service right away. Which style fits best?

A. The trip service calls each of the three in turn and waits for each reply
B. The trip service publishes one asynchronous event that the three services read when they are ready
C. The trip service makes one long call that waits until all three have finished
D. Each service checks a database every minute to see whether a trip has ended

**Answer:** B. One published event reaches several readers, and none of them holds up the trip service.

## Common Mistakes to Avoid

> **Tip:** Do not assume a call will succeed. For each call, plan the timeout, the action on failure, and what the user sees while waiting.

> **Tip:** Do not make everything asynchronous. If the user needs the answer on the screen to continue, waiting is the correct choice.

> **Tip:** Expect duplicate and late messages. Ask whether the receiver can handle the same message twice. Other lessons explain how to design for that.

## Summary

Parts of a system exchange data over a network, and a network call is far less predictable than a function call. It can be slow, fail, time out, or arrive twice. A synchronous call makes the caller wait, which is simple and gives an answer at once, but it ties the parts together and lets delays spread. An asynchronous message lets the caller hand off work and move on, which improves speed and loosens coupling, but the result arrives later and must be tracked. Choose the style for each interaction by asking who talks to whom, how much data moves, how fast the answer is needed, and how much delay or loss the flow can tolerate.

## Sources

- [Wikipedia: Fallacies of distributed computing](https://en.wikipedia.org/wiki/Fallacies_of_distributed_computing)
- [Fallacies of Distributed Computing Explained (University of Wisconsin PDF)](https://pages.cs.wisc.edu/~zuyu/files/fallacies.pdf)
- [Design Gurus: Synchronous vs Asynchronous Communication in System Design](https://www.designgurus.io/blog/sync-vs-async)
- [Levelop: Synchronous vs Asynchronous Communication Explained](https://levelop.dev/blog/synchronous-vs-asynchronous-communication)
- [AlgoMaster: Sync vs Async Communication (system design)](https://algomaster.io/learn/system-design/sync-vs-async-communication)
- [AlgoMaster: Synchronous vs Asynchronous Communication (microservices)](https://algomaster.io/learn/microservices/sync-vs-async-communication)
- [AlgoMaster: The Eight Fallacies of Distributed Computing](https://algomaster.io/learn/computer-networks/fallacies-of-distributed-computing)
- [Kindatechnical: The Eight Fallacies of Distributed Computing](https://kindatechnical.com/distributed-systems/eight-fallacies-of-distributed-computing.html)
- [Ably: Navigating the 8 fallacies of distributed computing](https://ably.com/blog/8-fallacies-of-distributed-computing)
- [Azion: How Microservices Communicate](https://www.azion.com/en/learning/microservices/how-microservices-communicate/)
- [Laws of Software Engineering: Fallacies of Distributed Computing](https://lawsofsoftwareengineering.com/laws/fallacies-of-distributed-computing/)
- [GeeksforGeeks: Communication Protocols in System Design](https://www.geeksforgeeks.org/system-design/communication-protocols-in-system-design/)
