This project contains an event-driven microservices design pattern that is influenced by the CQRS Pattern.
The command query responsibility segregation (CQRS) pattern separates the data mutation, or the command part of a system, from the query part. You can use the CQRS pattern to separate updates and queries if they have different requirements for throughput, latency, or consistency. The CQRS pattern splits the application into two parts: the command side and the query side. The command side handles create, update, and delete requests. The query side executes the get requests.
This pattern will write data into a DynamoDB table and then create a stream from it. Lambda is then used to process the change events in the database and replicate the data in Elasticache for faster reads.
Learn more about this pattern at Serverless Land Repos: https://serverlessland.com/repos
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Java11
- Maven installed
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- AWS CLI installed and configured
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
-
Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/mansiyd/cqrs-pattern.git
-
Change directory to the pattern directory:
cd cqrs-pattern
-
Build the project using the below Maven command in your terminal:
mvn clean install
-
From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
sam deploy --guided
-
During the prompts:
- Enter a stack name
- Enter the desired AWS Region
- Allow SAM CLI to create IAM roles with the required permissions.
Once you have run
sam deploy --guided
mode once and saved arguments to a configuration file (samconfig.toml), you can usesam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
The sam template creates the Amazon DynamoDB Table with streams enabled, Publisher and Subscriber AWS Lambda functions and Amazon Elasticache cluster.
- The OrderPublisher Lambda can be invoked from the console, using the sample request payload provided below.
- As soon as the Lambda receives the request, a new order record is created in the DynamoDB table.
- DynamoDB streams then captures this new record creation event and triggers the OrderSubscriber Lambda function.
- OrderSubscriber Lambda function processes this event and creates the record in Elasticache for Redis cluster for the Read queries.
- The data can be fetched from Amazon Elasticache by invoking OrderQueryService using the sample request payload provided below.
Invoke the OrderPublisher Lambda function with the following payload, which will create the new order in the DynamoDB table. Note the order id received as a response from the Lambda function, which can be used to query the order data.
{"cliendId":"123456789","productId":"abc-def-ghi"}
Invoke the OrderQueryService Lambda function with the following payload. Replace the <order-id>
with the Order Id returned by the previous Lambda function, to view the order details fetched from the Elasticache.
{"orderId":"<order-id>"}
- Delete the stack
sam delete --stack-name STACK_NAME
- Confirm the stack has been deleted
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0