Saga design pattern is used to preserve data integrity in distributed transactions that span multiple services.
A saga consists of a sequence of local transactions. Each local transaction in a saga updates the database and initiates the next local transaction. If a transaction fails, the saga runs compensating transactions to revert the database changes made by the previous transactions.
Saga design pattern has two variants - Choreography and Orchestration. The saga choreography pattern is Event Driven. The saga participants subscribe to the events and act based on the event details. They coordinate among themselves over a messaging system and there is no central-hub or an orchestrator to coordinate the flow of transactions.
The source code in this repo provides sample code for implementation of the saga choreography pattern on AWS. Below diagram depicts the architecture created by this source code.
- It has three microservices, namely - Order, Inventory and Payment services which are implemented as Lambdas.
- Each service is exposed through an Amazon API Gateway REST endpoint.
- Each service has a data store - implemented as DynamoDB tables.
- Each service publishes messages and consumes them on Amazon EventBridge.
For this walkthrough, you need:
- An AWS account
- An AWS user with AdministratorAccess (see the instructions on the AWS Identity and Access Management (IAM) console)
- Access to the following AWS services: Amazon API Gateway, AWS Lambda, Amazon EventBridge and Amazon DynamoDB.
- Node.js installed
- AWS CLI
- AWS CDK Toolkit
- Docker or Docker Desktop for Windows
- Postman to make the API call
The CDK code in this repository creates the target architecture as shown in the above diagram. These include IAM roles, REST API on Amazon API Gateway, DynamoDB tables, Amazon EventBridge event buses and Lambda functions.
- You need an AWS access key ID and secret access key for configuring the AWS Command Line Interface (AWS CLI).
- Clone the repo:
git clone https://github.com/aws-samples/saga-choreography-pattern.git
- Start Docker or Docker Desktop.
- Build the source code using npm command and bootstrap CDK in your chosen AWS region.
- The cdk synth command causes the resources defined in this repository to be translated into an AWS CloudFormation template. The cdk deploy command deploys the stacks into your AWS account. Run:
cdk synth
cdk deploy
-
CDK deploys the physical environment to AWS (including the microservices). You can monitor the progress using the CloudFormation console. The stack name shall be SagaChoreographyStack.
-
Once the physical deployment of environment is completed, familiarize yourself with the REST endpoints and microservices in AWS Console. Use the Postman collection given in the repository to connect to the REST Endpoints and invoke APIs to intitate the transactions.
-
You may use the DELETE API to invoke compensating transactions.
This library is licensed under the MIT-0 License. See the LICENSE file.
See CONTRIBUTING for more information.
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation template