Card 01/ 09
Synchronous and Asynchronous Communication
Waiting for the answer ties you to the slowest service in the chain.
When one service asks another to do something, it can wait for the work to finish, or hand the work off and carry on. Waiting is synchronous communication: the caller gets the result, but is as slow as the work and fails when the work fails. Handing off is asynchronous communication: the caller replies at once, and the work happens later, somewhere else.
Synchronouswait for every step
The order service saves the order, calls the payment service and waits, then calls the email service and waits for the receipt to be sent, then replies to the customer. If the email service takes four seconds, the customer waits four seconds. If it is down, the order fails, although the card was charged and the order saved.
Asynchronoushand off what can wait
The order service saves the order and calls the payment service, because the customer must know whether the payment worked. It then puts a send receipt message on a queue and replies. The email service takes the message when it can. If it is down, the message waits, and the receipt goes out when it recovers.
What the caller gains. Its reply no longer includes the time of the work handed off. A slow or broken service downstream no longer slows or breaks the caller. And a burst of work can wait in line rather than overwhelming the service that does it.
What the caller gives up. When it replies, the handed-off work has not happened yet, and it does not know whether it will succeed. Something else must notice failures and retry them. The system now has a queue to run, and work that finishes at some later moment rather than before the reply.
Deciding what can wait. Ask whether the caller's answer depends on the result. The customer must know whether the payment went through, so that stays synchronous. The customer does not need the receipt in hand before seeing "order placed", so that can be handed off.
So keep synchronous only the work the reply depends on, and hand off the rest, accepting that it finishes later and can fail separately.
← → move through sections and cards