The saga design pattern is a style of software architecture that's intended to manage distributed transactions that execute as a sequence of steps in a failsafe manner.
We can use Saga Pattern. A saga is a sequence of local transactions. In this pattern, each transaction updates the database and triggers an event or publishes a message for next transaction in saga. In case, any local transaction fails, saga will trigger series of transactions to undo the changes done so far by the local transactions.
For example, imagine a travel agent that makes airline, hotel and car rental reservations for a customer's trip. The agent must abide by a governing rule: If any of the reservations fail, all the reservations made hitherto must be canceled as if the trip never happened. So, if the sequence of reservations is airline, hotel and car rental, a failure in the car rental reservation means the reservations for airline and hotel must be canceled too. If the hotel reservation fails, the airline reservation must be canceled and the car rental reservation must not happen at all.
Implementation
In the choreographed approach to a saga design pattern, each step acts independently with no central controller. One step knows its subsequent step in the process, so when it completes it calls the next step. If a call fails, the called step executes its compensation and then throws an error. The calling step catches the error and executes its compensation, and all the previous steps execute their own compensations in a reversed cascade.
In an orchestrated saga, a central controller manages the steps and compensations within a sequence. If the first step is successful, the controller proceeds to run the next step in the sequence. If there is a problem running the step, the controller executes the compensation for the failing step. If the failure occurs downstream after a few steps, the sequence terminates and the controller runs the compensation for the failing step plus the compensations for all the previous steps.