Sitemap
Arif Alili

Arif Alili

MACH enthusiast | Lead Software Engineer

SAGA Pattern for Composable Commerce transactions using AWS Step Functions

3 min readAug 1, 2024

--

Press enter or click to view image in full size

What Problems Are We Trying to Solve?

In a microservices architecture, using a database-per-microservice approach allows each service to select the most appropriate data store, enabling independent scaling and failure isolation. However, this complicates transactional operations across multiple services. Ensuring ACID (atomic, consistent, isolated, durable) properties becomes challenging, as distributed transactions require coordination across services, which not all data stores support.

What is the Saga Pattern?

The Saga Pattern offers a solution for managing distributed transactions across microservices by creating a sequence of local transactions that update microservices one after the other. If any transaction fails, the Saga Pattern initiates compensating transactions to reverse the changes, ensuring data consistency.

In essence, the Saga Pattern coordinates multiple local transactions to maintain consistency in a distributed system without relying on traditional transaction coordination mechanisms. A Saga is a collection of sub-transactions.

Example architecture walkthrough — Composable Commerce

With Composable Commerce becoming the new standard, the use of microservices in the e-commerce world has grown significantly. The order fulfilment process is a perfect example where transaction coordination is needed. Let’s go through this use case step-by-step.

Microservices involved

  • Orders: Responsible for creating orders.
  • Inventories: Responsible for updating inventory data.
  • Payments: Responsible for payment processing.
  • Notifications: Sends notifications to end customers about order status.

Order fulfillment happy path

  1. Customer initiates order: The customer places an order through frontend channels.
  2. Orders microservice: A new order is created.
  3. Inventory microservice: Confirms item availability and deducts inventory.
  4. Payments microservice: Processes the payment for the order.
  5. Notifications microservice: Sends notifications to the customer about the successful order status.
Press enter or click to view image in full size
Successful order fulfilment process

Handling failures

In scenarios where there isn’t enough stock, payment processing fails, or the payment microservice encounters a critical error (just to name some cases), it’s crucial to handle these failures gracefully. The Saga Pattern allows us to manage these cases effectively by reverting changes made by previous services to the datastore. This ensures data consistency and reliability across the microservices in the e-commerce application.

Press enter or click to view image in full size
Order fulfilment process using microservices

Implementing Saga pattern with AWS Step Functions

AWS Step Functions are ideal for managing long-lived transactions by enabling the creation of fully managed state machines. These state machines coordinate the components of distributed applications and microservices.

Get Arif Alili’s stories in your inbox

Join Medium for free to get updates from this writer.

In our e-commerce example, handling failure scenarios using the Saga Pattern can be implemented with AWS Step Functions. Each microservice mentioned above is deployed as a Lambda function. Additionally, we add specific functions to handle rollbacks:

  • Payment Refund Function
  • Inventory Rollback Function
  • Cancel Order Function

Order fulfilment happy path in AWS Step Functions

In case of happy path, no rollback is needed.

In case payment could not be processed, we would need to rollback the inventory for all the items and cancel the order (or change its status).

Blueprint for implementing the SAGA pattern in an e-commerce application using AWS Step Functions

Example project with Order Fulfilment workflow can be found in this GitHub repo. The project consists of 7 functions with example code written in Node.js and utelizes AWS SAM to deploy these functions to AWS Lambda and create Step Functions state machine.

If you find or ask anything you can directly open issue on repository.

--

--

Written by Arif Alili

0 followers

MACH enthusiast | Lead Software Engineer