# Enhancing Saga Pattern for Distributed Transactions within a Microservices Architecture
## 1 Introduction  
A microservice based-application [1, 2] is a distributed system where the functionalities of the application are provided by multiple smaller services that are working together. The microservice architecture has restructured the monolithic application into several individual services in order to provide loose coupling, high maintainability, high availability, and scalability for the application development. The microservices architecture enables selecting the technology stack per service. For instance, the relational database could be employed to provide one service while the NoSQL database could be utilized to implement the other one; thus, allowing the services to manage the domain data independently. Moreover, with the microservice architecture, scaling data stores on-demand is enabled. Each microservice has its own database that contains some business transactions; therefore, managing distributed transactions and maintaining data consistency when transactions span across multiple services are challenging.  
To manage distributed transactions in the microservice architecture, the Saga Pattern (Software Automation, Generation, and Administration) was proposed. Saga design pattern manages transactions and maintains data consistency across distributed microservices transactions. Saga is a set of local sequential transactions that is responsible for updating the microservices and publishing messages to trigger the next transactions. In the case of failure by one transaction, compensating transactions will be run to counteract the preceding one. However, the saga pattern does not have read isolation. It is ACD (atomicity, consistency,durability), not ACID (atomicity, consistency, isolation, durability) [3]. The missing isolation means that data reading and writing from an incomplete transaction are allowed which, in turn, introduces various isolation anomalies [4; 5]. Therefore, to address this problem, this research proposes an enhanced saga pattern that aims to achieve eventual consistency via the use of the quota cache and the commit-sync service. The idea is to integrate the standard saga pattern with an in-memory data caching layer by allocating the quota of the main database to the in-memory data caching server. Therefore, the CRUD (create, read, update, and delete) tasks will be handled via the quota cache instead of the main database which ensures that no wrong commit to the main database will occur. When a microservice fails to be completed, the other microservices will run compensation transactions to rollbacks the changes that only affect the cache layer instead of the main database layer. This will resolve the lack of read-isolation of the saga pattern and, additionally, it will enhance the performance. The database commit will be postponed and handled via the message queue middleware at the end of the workflow when all transactions are completed successfully to achieve eventual consistency. The clean architecture approach was utilized for the implementation where all the domain use cases have been defined in advance.  
For demonstration, a lightweight microservices-based e-commerce system was implemented to compare the standard baseline version of the saga pattern with the proposed enhanced version. Several experiments were conducted for validation and evaluation. Results demonstrate that the proposed approach has the capability to resolve the lack of isolation in the saga pattern. Results also indicate that the proposed approach achieves better performance than the standard baseline version not only in typical cases but also in the scenario that needs to handle exceptions as employing the cache operations instead of the database operation enhances the performance and reduces the latency time.