Reliable messaging
Promoting domain events to types in your system has more benefits than just improving the maintainability of the application. Consider the following scenario. At peak hours, the web services of the warehouses may time out. But at that point in time, both the receipt has been sent and the billing system has been notified. Although it's possible to roll back the database update, it's impossible to roll back the notification-the customer has already been mailed. Unfortunately, the problems with the billing system aren't the only ones. Recently, one of the web servers that runs the order-approval process crashed. The customer's confirmation mail got sent just before the crash, but neither the billing system nor the warehouses were notified. The customer never got the order. What should you do to mitigate these kinds of problems? Although there are multiple ways to handle this scenario, domain events can help: they can be serialized and put on a durable message queue like MSMQ, Azure Queue, or a database table. Doing so allows you to let your OrderService only execute the following operations: ■ Begin a transaction ■ Update the order in the database as part of the transaction ■ Publish the OrderAccepted event to a durable queue as part of the transaction ■Commit the transaction Only after the OrderAccepted event has been committed to the queue does it become available for further processing. At that point, you can pass it on to each of the available handlers for that particular event. Each handler can run in its own isolated transaction. If one of the handlers fails, you could retry that specific handler without influencing the other handlers. You might even execute multiple handlers in parallel. Processing messages using a durable queue is a form of reliable
Sayfa 179 - Manning
Yazılım