Part 3 of 8 · 2 min read
Sync or async
Two choices that decide the shape of the rest.
BM calls this one of the most important decisions about how services work together.
In synchronous communication the caller sends a request and waits until the work is finished. In asynchronous communication the caller does not wait, and may not even care whether the work completes.
| Synchronous | Asynchronous | |
|---|---|---|
| Easier to reason about | Yes, you know at once if it worked | No, the result arrives later or never |
| Good for | Short calls with an answer needed now | Long jobs, low latency, flaky networks such as mobile |
| Cost | The caller is blocked | More tools and more ways to fail |
These modes lead to two styles of working together.
- Request and response. A client asks for something to be done and waits for the answer. It fits synchronous calls, and can also be asynchronous with a callback.
- Event-based. The client does not ask. It says "this thing happened" and others decide what to do. The sender never tells anyone what to do. Event-based work is always asynchronous.
BM notes that in event-based work, the logic is spread across the collaborators and not centred in one brain. The sender cannot know who listens, so new listeners can be added with no change to it. This is the broker topology of page 4, and the log-based tools of Level 6.
Event-based collaboration
In event-based collaboration, a service announces that something has happened, and other services react to it on their own. No service is told what to do.
Example: the customer service announces "customer created", and the email service reacts by sending a welcome message.
A worked example (made up). A mobile app sends a photo for processing, which takes 30 seconds. With a synchronous call, the connection stays open for 30 seconds on a patchy network, and a drop means starting again. With an asynchronous call, the app sends the photo, gets a job id in under one second, and is told or asks later when the job is done.
Check yourself: 1. Why does event-based collaboration always mean asynchronous? 2. Give one situation that suits synchronous and one that suits asynchronous. 3. In the photo example, what must the app keep so it can find the result later?