Part 5 of 7 · 4 min read
Asynchronous benefits, request and reply, and data loss
Async makes the user faster to answer. Some cases still need an answer back.
Think of a restaurant that gives you a buzzer. You do not wait at the counter for your meal. You are told "accepted", and the buzzer rings when it is ready.
FSA says the style relies on asynchronous communication throughout, for both fire and forget (no response) and request and reply (response needed). The gain is responsiveness. The book's example is posting a comment that passes three checks (bad words, grammar, context) taking 3,000 ms:
| Path | Network in | Work | Network out | User waits |
|---|---|---|---|---|
| Synchronous call | 50 ms | 3,000 ms | 50 ms | 3,100 ms |
| Asynchronous message | 25 ms | 3,000 ms (in the background) | none | 25 ms |
The work still takes about 3,025 ms. The user is told "accepted" in 25 ms. The book separates responsiveness (how fast the user is told the action was accepted) from performance (how fast the work is done). The cost: errors are harder to report. In the book's caveat, the synchronous path guarantees the comment was posted, while the asynchronous path only promises it will be. If a comment is later rejected, there is no waiting caller to tell. FSA calls error handling the main issue of asynchronous communication and offers the workflow event pattern. When a consumer hits an error, it hands the message and the error to a separate workflow processor and moves on to the next message at once, so the queue keeps flowing. The workflow processor tries to repair the data by itself and puts it back on the original queue, where it looks like a new message. If it cannot repair it, it sends the message to a dashboard where a person fixes it and resubmits it. The book's example is a trade order with the word SHARES stuck after the number. A repair step strips the word and resubmits the trade. One consequence: repaired messages are processed out of order, which matters when order within an account is important.
Request and reply. Sometimes an answer is needed at once, such as an order id or a flight confirmation number. FSA describes pseudosynchronous request-reply. Each channel has a request queue and a reply queue. The producer sends to the request queue and then waits on the reply queue. Two techniques:
- Correlation id (the common one). The request has a message id, for example 124. The consumer's reply sets its correlation id to 124. The producer waits on the reply queue with a filter "correlation id equals 124", so it ignores replies for other requests.
- Temporary queue. The producer creates a queue for this request and names it in a reply-to header. The consumer replies there, and the queue is deleted afterwards. It is simpler, but the broker must create and delete a queue per request, which slows it at large volumes. The book recommends the correlation id technique.
Preventing data loss. FSA names three places where a message can be lost when processor A sends to a queue and processor B stores the data in a database. Each has a basic fix:
| Where it is lost | Fix |
|---|---|
| A to the queue, or the broker crashes before B reads it | Persistent queues (guaranteed delivery) plus a synchronous send, where A waits until the broker confirms the message is stored |
| B takes the message and crashes before processing | Client acknowledge mode: the message stays in the queue, reserved for B, until B confirms it |
| B cannot save the data to the database | Use a database transaction, and remove the message from the queue only after the commit (last participant support) |
These are the delivery guarantee ideas of Level 6 page 2, applied to one flow.
Broadcast. A producer can publish without knowing who listens. The book's example is a stock ticker whose price may feed many things.
When to choose. The book recommends the request-based model for well-structured, data-driven requests such as fetching a customer profile, where certainty and control over the workflow matter. It recommends the event-based model for flexible, action-based events that need high responsiveness and scale and have complex, dynamic processing.
Read it as
Use a request when the caller needs the answer to continue. Use an event when the caller can move on and others may react.
Check yourself:
- In the comment example, what does the user wait for in each path?
- How does a correlation id let many requests share one reply queue?