Messaging & Queuing in AWS

Messaging and queuing are AWS services and techniques that enable asynchronous communication and decoupling of components within a distributed system. AWS offers a variety of messaging and queueing services, including
Amazon Simple Queue Service (SQS)
Amazon Simple Notification Service (SNS).
Amazon Simple Queue Service (SQS):
A fully controlled message queuing service is offered by Amazon Web Services (AWS) under the name Amazon Simple Queue Service (SQS). It allows for the decoupling and asynchronous communication of dispersed applications or system components. It provides a reliable and scalable solution for decoupling components and building distributed systems. It allows for loose coupling, fault tolerance, and asynchronous communication, making it ideal for building robust and scalable applications.
Features of SQS:
Message-Queue: Create a message queue using SQS to act as a buffer for messages exchanged between various components. Messages are held in the queue until the receiving component consumes them.
Distributed Architecture: SQS is built to work in a distributed fashion, which greatly enhances its scalability and dependability. It automatically takes care of the supporting framework needed for message transport and storage.
Message Durability: SQS stores redundant copies of messages across various availability zones to provide message longevity and high availability. This assures dependable transmission and prevents message loss.
Delivery at least once: SQS offers delivery semantics that guarantees messages are sent to the customer at least once. As a result, there is no chance of message loss during transmission.
Visibility Timeout: When a message is consumed from a queue, it briefly loses its visibility to other consumers. The consuming component can process the message unhindered by other consumers thanks to this visibility timeout.
Auto-Scaling Integration: SQS and Amazon EC2 Auto Scaling are both smoothly integrated with other AWS services. This makes it possible for the consumer instances to automatically scale based on how many messages are in the queue, guaranteeing effective resource use.
Message Delay: SQS enables you to postpone the delivery of messages to customers. When scheduling or delayed processing is necessary, this function is helpful.
For example:
Consider an e-commerce website that sells products from multiple suppliers. When customers place orders, the website needs to notify each supplier about the orders they need to fulfill. However, the processing time required to notify all the suppliers may vary, and failures can occur. In this case, SQS can be used to ensure reliable and asynchronous communication between the e-commerce website and the suppliers.
Here's how SQS can be utilized in this scenario:
Order Placement: When a customer places an order on the e-commerce website, the website generates an order notification message containing details like the customer's name, shipping address, and the products ordered.
Sending Messages to SQS: The website sends these order notification messages to an SQS queue specifically created for order notifications, let's call it "OrderNotificationQueue". The website can use the AWS SDK or API to send messages to the queue.
Supplier Integration: Each supplier has a working system or application that continuously polls the "OrderNotificationQueue" to retrieve order notification messages.
Processing Supplier Notifications: When a supplier's worker system retrieves an order notification message from the queue, it processes the message to extract the necessary order details. The supplier's system can then proceed to fulfill the customer's order based on the information provided.
Message Deletion: After the successful processing of the order notification, the supplier's worker system deletes the message from the queue using the SQS API to ensure that it is not processed again.
By using SQS in this scenario, the e-commerce website can reliably notify suppliers about new orders while ensuring asynchronous processing and fault tolerance. Even if there are delays or failures in delivering the notifications to the suppliers, the messages are stored securely in the queue, allowing suppliers' worker systems to retrieve them whenever they are ready.
SQS helps decouple the ordering system from the supplier notification process, enabling scalability, resilience, and fault tolerance in the overall architecture of the e-commerce application.
Amazon Simple Notification Service (SNS):
Amazon Simple Notification Service (SNS) is a fully managed publish-subscribe messaging service provided by Amazon Web Services (AWS). It enables reliable and scalable message dissemination and pushes notifications to subscribers or endpoints. Amazon SNS is widely used for various purposes, including sending event notifications, distributing messages to multiple subscribers, triggering AWS Lambda functions, and delivering push notifications to mobile devices. It provides a scalable and reliable solution for building and managing communication between distributed components and services.
Features of SNS:
Publish-Subscribe Model: SNS follows a publish-subscribe model where messages are published to topics, and subscribers receive the messages. Publishers send messages to topics, and subscribers can subscribe to one or more topics to receive the messages.
Flexible Message Delivery: SNS supports multiple delivery protocols, including email, SMS, mobile push notifications, HTTP/HTTPS, AWS Lambda, and Amazon Simple Queue Service (SQS). This allows you to choose the most appropriate method to deliver messages to your subscribers.
Topic-Based Structure: Messages in SNS are organized into topics, which act as logical access points. Publishers send messages to specific topics, and subscribers can subscribe to topics of interest to receive relevant messages.
Subscriptions: Subscribers can subscribe to topics and specify their preferred delivery protocols. SNS supports a wide range of protocols, making it easy to deliver messages to subscribers via their preferred method.
Flexible Message Filtering: SNS allows you to filter messages based on message attributes so that subscribers only receive messages that match specific criteria. This feature helps you reduce unnecessary message processing and optimize the delivery of relevant messages.
Fanout and Scaling: SNS enables fanout, which means a single message published on a topic can be delivered to multiple subscribers simultaneously. This makes it easy to broadcast messages to a large number of recipients. SNS automatically scales to accommodate high message throughput.
Message Attributes: You can attach optional attributes to your messages, providing additional metadata or instructions for message processing. Subscribers can use these attributes to filter or handle messages based on their specific needs.
Message Retention and Retry: SNS stores messages redundantly across multiple availability zones to ensure high durability. It also provides built-in retry mechanisms to handle failed message deliveries, improving the reliability of message delivery.
Monitoring and Logging: SNS integrates with AWS CloudWatch, allowing you to monitor and collect metrics on message deliveries, subscriptions, and other SNS-related activities. You can also enable the logging of SNS API calls for auditing and troubleshooting purposes.
For example:
Let's consider a scenario of E-commerce Order Status Notifications
Suppose you run an e-commerce website that sells various products, and you want to notify customers about the status of their orders in real time. You can utilize Amazon SNS to send order status notifications to your customers through different channels, such as email and SMS.
Here's how you can implement this scenario using Amazon SNS:
Topic Creation: Create an SNS topic named "OrderStatusTopic" within the AWS Management Console or via the AWS SDK/API. This topic will serve as the central point for publishing order status notifications.
Subscriptions: Allow customers to opt-in for notifications during the checkout process. When a customer selects the option to receive order status updates, subscribe their contact information (email address and phone number) to the "OrderStatusTopic." This can be done programmatically using the AWS SDK or via an interface on your website.
Order Status Update: Whenever there is a change in the status of an order, such as order confirmation, shipment tracking number availability, or delivery status update, your order management system triggers a message publish action to the "OrderStatusTopic" with the relevant order details.
Message Filtering (Optional): If you want to provide more granular notifications, you can attach message attributes to the published messages, such as order ID, customer ID, or order status type. Subscribers can set up message filtering rules to receive only notifications that match specific criteria.
Message Delivery: Amazon SNS automatically delivers the published order status messages to all the subscribed endpoints. For example, customers who subscribed via email will receive the notifications in their email inbox, while those who subscribed via SMS will receive the messages as text messages on their mobile phones.
Mobile Push Notifications (Optional): If you have a mobile application for your e-commerce platform, you can integrate Amazon SNS with mobile push notification services, such as Apple Push Notification Service (APNS) for iOS and Google Cloud Messaging (GCM) for Android. This allows you to send push notifications directly to the mobile devices of customers who have installed your app.
Monitoring and Logging: Utilize AWS CloudWatch to monitor the metrics and health of your SNS topic, such as message delivery rates, subscription counts, and any potential failures. You can also enable SNS API logging to track API calls related to your notifications for auditing and troubleshooting purposes.
By implementing this scenario with Amazon SNS, you can keep your customers informed about the progress of their orders in real-time, providing a better shopping experience and reducing the need for them to proactively check for updates.
