Amazon Simple Notification Service (SNS) is a fully managed messaging service for both system-to-system and app-to-person (A2P) communication. It enables you to communicate between systems through publish/subscribe (pub/sub) patterns that enable messaging between decoupled microservice applications or to communicate directly to users via SMS, mobile push and email.
In a monolithic application, tables are stored in a huge database. However, when you strangle out services from the monolithic application, the corresponding table is also strangled out. How to make the data in these tables consistent will be a big challenge. In the current Unishop, the basket item stored in DynamoDB has a field called unicorn_id depending on another field with the same name in the RDS. Now, consider a scenario, a customer puts a unicorn X into his cart, the administrator delete this unicorn from the inventory. In this case, how would you notify the customer that this unicorn has been deleted and make the basket database (DynamoDB) consistent with the inventory database (RDS)? A common solution to this is to ensure eventual consistency based on asynchronous messaging.
In this part, you will learn how to enable asynchronous communication using Amazon SNS to achieve eventual consistency between the basket service and the inventory service.
Ideally, you should minimize the communication between services because communication will result in coupling. But, in some cases, you have to enable communication between services. When you need to do that, the communication should be asynchronous. Asynchronous communication enforces microservices’ autonomy and makes your service more available. Example, if your client interacts with service A, while service A needs responses from service B and service B needs responses from service C. If you use a synchronous communication method, your client will definitely experience latency. Also, if one of the services crashes, the other services that are directly dependent on them may be affected and become unavilable as well.
To create the scenario, we provide you a button to delete a unicorn. When you click a unicorn tab, you will see a “Delete unicorn” button there. That button allows you to delete the unicorn. Although only the administrators should be able to delete unicorns, we provide the button for simplicity.
Firstly, in order to update the availablity status of the unicorns in that basket, you would be required to search the table based on unicorn_id. For this purpose, we ask you to create an Index on the unicorn_id field.
In the AWS Management Console, navigate to DynamoDB. On the side bar, click on “Tables” to view your current DynamoDB tables. Then, click on the table with name “UnicornBasket.”
Click on the “Indexes” tab and hit “Create Index”.
In the Create Index window, for Primary key enter unicorn_id, and for the Index name enter unicorn_id_index.
Hit “Create Index” and wait till your index status goes from “Creating” to “Active”.
Open the folder “SampleApp” on the desktop. Then open a solution file named “UnicornWorkshop.sln” in it.
Right click on the project “BasketLambda” select the option “Publish to AWS Lambda”.
Do the following changes and click “Next”. Then click “Upload” to publish the new lambda function.
“Function Name”: Enter BasketSNSLambda
“Method Name”: Enter SNSHandler
In the AWS Management Console, navigate to Lambda.
You will see “BasketSNSLambda” show up. Click that lambda function. If you don’t see that, please reload the page after several seconds.
Click the copy button in the top right corner to copy the ARN of this Lambda function. Paste it into your cheat sheet (You should paste it under “SNS Lambda ARN”).
Then, you need to set up a SNS topic.
In the AWS Management Console, navigate to Simple Notification Service. Then, click the “Topics” tab on the left side.
Click “Create topic” button.
Provide a name for the topic such as “ItemDeleted”. Leave other settings as default and click “Create topic”.
In the new page, copy the “ARN” (a unique identifier for AWS resources) of the topic to your cheat sheet (You should paste it under “SNS Topic ARN”). Keep the page OPEN since you need to create a subscription in the next step in the same page.
In the topic page you just opened, click “Create subscription.”
Do the following changes. Then click “Create subscription” button.
“Topic ARN”: Paste the value of “SNS Topic ARN” from your cheat sheet
“Protocol”: Choose “AWS Lambda”
“Endpoint”: Paste the value of “SNS Lambda ARN” from your cheat sheet
Expand the InventoryService project in the UnicornWorkshop.sln and open appsettings.json. In that file, update “SNSTopicArn” key value with the value of “SNS Topic ARN” in your cheat sheet.
Rebuild the project before you redeploy the inventory service.
Redeployment will be largely the same with the deployment process in the part 1 of Containerized Inventory Service. In the solution explorer, right-click the “InventoryService” and select “Publish Container to AWS…” In the wizard, simply click “Next” through the entire interaction since the settings have been saved.
After the redeployment, you may notice that the tasks you deployed in previous steps and created by auto-scaling are not instantly stopped. This is because the previous version will be retained for some time before being stopped in the process of updating.
Add a unicorn to your cart.
Click that unicorn, and click the “Delete unicorn” button.
Click your cart again or refresh the page. You will see a notification called “Some unicorns in your cart are out of stock!” and corresponding unicorns get deleted from your cart.