Saga Pattern for Dynamics365Ana Demeny
Over the course of time, I had seen many projects struggle with long running workflows. Last month I had the pleasure of presenting one possible solution for eliminating long processes in Dynamics365.
I remember it was a very sunny Saturday when London was buzzing with events like Pride, Wimbledon and last but not least, England was playing Switzerland in the quarter finals of the World Championship. Needless to say, the odds were not in favour of the 365 Summer Bootcamp.
Even so, at 9:00 am, the Microsoft office in Paddington was busy hosting 100% of the people registered for the event!
The 3 tracks were very interesting and up to date, making it difficult for attendees to stick to their designated presentations. My name fell under the Azure track as I presented a way of applying the Saga pattern in Dynamics, using NServiceBus queues.
I decided to present this, as it is something my team have successfully used, in order to avoid creating a workflow that would timeout for many weeks. With such a dramatic scenario, no wonder we tried looking at other options. After applying a Saga, we completely stopped building workflow activities, that would trigger thousands of processes and condemn them to sit there, quietly, until your system broke down in tears.
What’s a Saga?
The Saga Pattern was first presented in 1987 by Hector Garcia-Molina & Kenneth Salem, at Princeton University. It describes a journey where each step is defined through Transactions that either have an expected result, or get corrected by Compensating Transactions.
A really simple example is trying to book a package holiday. You can find this all over the internet and it’s very good as you can see how transactions and compensating transactions can be used in real life.
- Successful Saga - all services have been booked succesfully
2.Unsuccessful Saga - at least one booking failed, causing the Saga to start triggering Compensating Transactions, to cancel the successful bookings as well.
As you can see, this pattern makes sure that a journey is either complete or completely rolled back.
Saga for Dynamics365 using NServiceBus
For my Dynamics365 scenario I made a number of assumptions:
- Dynamics365 is online and you use an Azure subscription with NServiceBus
- The overall system is made out of microservices communicating via API endpoints or messages on a queue
- Your team has .Net skills – you’ll need to build a custom workflow activity and to code the saga
Similar to the holiday booking scenario, you’ll need a few things clear in your strategy:
1. You need to start a Saga Let’s say you start booking a holiday, this creates an opportunity record. There we go! Here’s our trigger. At this point you’d create a custom workflow activity that can capture a start event.
NServiceBus is pretty smart. It gives you the entire code where you can define how to start your saga:
2. You need to end a Saga
Otherwise it will hang around for ever! Make you have a stop event, such as the booking is complete, or the booking has failed. Img stop saga
3. You can expire a saga
Very useful for scenarios when you have to wait for something to happen, you can start a saga and then let it expire after a while. Don’t worry if the expected event has happened and the Saga was closed already. NServiceBus know how to handle these things.
If you feel you didn’t really get the gist after this article, don’t worry, you were not supposed to. I just wanted to rise awareness about this way of transforming your system. I’m also adding my slide deck, a way to get started on NServiceBus and NserviceBus Saga.