12.2 About the Sample Saga Application

Use the sample Saga application to book a trip, which consists of booking a hotel room and a flight.

The following figure shows a sample Saga application, which contains several microservices, to demonstrate how you can use MicroTx to manage Saga transactions. Each microservice in the sample application performs a different task. One microservice books a trip, another books a flight, and a third microservice books a hotel. MicroTx coordinates the transactions between these microservices.

Image of Sample Saga application

The sample Saga application consists of the following polyglot microservices:

  • MicroTx (Saga Coordinator) coordinates the transaction between the sample microservices.
  • Trip Manager service is the transaction initiator service, where the Saga transaction starts. While booking a trip, this service calls the flight and hotel services for booking a flight and hotel respectively. The Trip Manager exposes the APIs to book both the hotel and flight and to cancel the booking. Either both hotel and flight are booked successfully or both bookings are canceled if there is a failure.
  • Hotel Booking service exposes APIs to book a hotel room and also to cancel the booking in case of any failure. It is called by the Trip Manager service to reserve a room. As it is called within the context of an existing Saga, it enlists itself and provides callback URIs that the Saga coordinator uses to complete or compensate the room reservation.
  • Flight Booking service exposes APIs to book a flight ticket and also to cancel the booking in case of any failure. It is called by the Trip Manager service to book a flight ticket. As it is called within the context of an existing Saga, it enlists itself and provides callback URIs that the Saga coordinator uses to complete or compensate the flight reservation.
  • Trip client is the user interface which you can use to confirm or cancel the booking. It does not participate in the Saga transaction. It is provided as sample client service which calls microservices to perform a distributed transaction that uses the Saga protocol.

The source code for the Saga sample application is available in the lra folder in the microtx-samples GitHub repository. MicroTx libraries are included in the code of the sample application microservices. The services communicate with each other through the exposed REST endpoints while using the MicroTx libraries.

When you run the application, it makes a provisional booking by reserving a hotel room and flight ticket. Only when you provide approval to confirm the booking, the booking of the hotel room and flight ticket is confirmed. If you cancel the provisional booking, the hotel room and flight ticket that was blocked is released and the booking is canceled. By default, the flight service permits only two confirmed bookings. To enable you to test the failure scenario, the flight service sample application rejects any additional booking requests that are made after two confirmed bookings. This leads to the cancellation (compensation) of a provisionally booked hotel within the trip and the trip is not booked.