Amazon's Messaging Services: SQS and SNS
In the world of cloud computing, efficient communication between different components of an application is crucial. Amazon Web Services (AWS) offers two powerful messaging services to facilitate this: Amazon Simple Queue Service (SQS) and Amazon Simple Notification Service (SNS). Both services are designed to handle different messaging needs, and understanding their features and use cases can help you choose the right one for your application.
Amazon Simple Notification Service (SNS)
Amazon SNS is a fully managed messaging service that lets you send and receive messages between microservices, distributed systems, and serverless applications. It allows you to send messages to multiple subscribers, including distributed systems, and mobile devices.
Overview of Amazon SNS
Application-to-application (A2A)
Amazon SNS is a managed messaging service that lets you decouple publishers from subscribers. This is useful for application-to-application messaging for microservices, distributed systems, and serverless applications. Know More
Application-to-person (A2P)
Amazon SNS lets you send push notifications to mobile apps, text messages to mobile phone numbers, and plain-text emails to email addresses. You can fan out messages with a topic or publish to mobile endpoints directly. Known more
Key Features of SNS
Pub/Sub Model: SNS uses a publish/subscribe model, allowing you to send messages to multiple subscribers simultaneously.
Flexibility: It supports multiple protocols, including HTTP/S, email, SMS, and AWS Lambda, providing flexibility in how messages are delivered.
Scalability: SNS automatically scales to handle high throughput, ensuring that messages are delivered quickly and reliably.
Reliability: It guarantees message delivery to all subscribers, ensuring that no message is lost.
Security: SNS provides encryption for messages in transit and at rest, ensuring data security.
A real-world example of Amazon SNS in action is a Ride-Sharing Application.
Publisher (Topic): The ride-sharing application publishes a topic called "RideStatusUpdates." This topic includes messages about the status of rides, such as when a ride is requested, accepted, in progress, or completed.
Subscribers:
Driver Mobile App: Subscribes to the "RideStatusUpdates" topic to receive notifications about new ride requests, ride cancellations, and updates on ride status.
Rider Mobile App: Subscribes to the same topic to receive notifications about the status of their ride, such as when a driver is on the way or has arrived.
Backend System: Subscribes to the topic to update the database with the latest ride status and trigger other backend processes, such as billing and analytics.
In this scenario, the ride-sharing application (publisher) sends messages to the "RideStatusUpdates" topic, and all the subscribers (driver app, rider app, and backend system) receive the relevant notifications in real-time. This ensures that all parties are kept informed about the status of rides efficiently and reliably.
Creating Topic, Creation Subscription and Sending Message
Creating a Topic Amazon SNS:
Navigate to the Amazon SNS dashboard, click on "Create topic," and fill in the required details like the topic name and display name.
Creating a Subscription in Amazon SNS
Creating a subscription in Amazon SNS is a straightforward process that involves several key steps.
Navigate to the Amazon SNS Dashboard: The first step is to go to the Amazon SNS dashboard. Here, you will find the "Create subscription" button. Click on this button to start the process of creating a new subscription.
Fill in the Subscription Details: Once you click on "Create subscription," a form will appear. This form requires you to fill in several fields:
Topic ARN: Select the Amazon Resource Name (ARN) of the topic you want to subscribe to.
Protocol: Choose the protocol for the subscription, such as HTTP, HTTPS, email, or SMS.
Endpoint: Enter the endpoint that will receive the notifications, such as a URL, email address, or phone number.
Subscription ARN: After filling in the details and creating the subscription, a Subscription ARN will be automatically generated. This ARN uniquely identifies your subscription.
Confirm the Subscription: To finalize the subscription process, you need to click on the "Confirm subscription" button. This step is crucial to ensure that the subscription is active.
Subscription Confirmation: Once the subscription is confirmed, you will see a confirmation message indicating that the subscription has been successfully created.
View Subscriptions: You can view the list of subscriptions under the selected topic. This list includes details such as the subscription ARN, protocol, and endpoint.
Manage Subscriptions: This also provides options to manage your subscription. You can delete or edit the subscription as needed.
Subscription Details: Finally, you can view and manage the settings of the specific subscription on the "Subscription details" page.
Sending a Message in Amazon SNS
When working with Amazon SNS, sending a message involves a few straightforward steps. Here’s a detailed guide to help you through the process:
Publish a Message: On the topic details page, click on the "Publish message" button. This will open the "Publish message" page where you can enter the details of your message.
Subject: Enter the subject of the message.
Message: Enter the content of the message.
Message Attributes: Optionally, you can add message attributes to provide additional metadata.
Send the Message: After filling in all the necessary details, click on the "Publish message" button to send the message to all subscribers of the topic.
Message received: By following these steps, you can efficiently send messages using Amazon SNS, ensuring that your notifications reach all intended subscribers.
Use Cases for SNS
Event Notifications: SNS can be used to send notifications about events, such as changes in the state of an application or system.
Broadcasting Messages: It can be used to broadcast messages to multiple subscribers, such as sending alerts or updates to a large number of users.
Mobile Push Notifications: SNS can be used to send push notifications to mobile devices, enabling real-time communication with users.
Amazon Simple Queue Service (SQS)
Amazon SQS is a fully managed message queuing service that helps you separate and scale microservices, distributed systems, and serverless applications. It allows you to send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available.
It acts as a buffer between different parts of a distributed system, allowing them to work independently
Overview of Amazon SQS
Amazon SQS allows producers to send messages to a queue. Messages are then stored in an SQS Queue. When consumers are ready to process new messages, they poll them from the queue. Applications, microservices, and multiple AWS services can take the role of producers or consumers.
Key Features of SQS
Decoupling: SQS helps in decoupling the components of an application, allowing them to run independently and communicate asynchronously.
Scalability: It automatically scales to handle any amount of traffic, ensuring that your application can handle increased loads.
Reliability: SQS guarantees at least once delivery of messages, ensuring that no message is lost.
Security: It provides encryption for messages in transit and at rest, ensuring data security.
Cost-Effective: You only pay for what you use, making it a cost-effective solution for message queuing.
A real-world example of Amazon SQS in action is an E-commerce Order Processing System.
Order Placement (Producer): When a customer places an order on the e-commerce website, the order details are sent as a message to an SQS queue called "OrderQueue." This queue acts as a buffer to handle the incoming order requests.
Order Processing (Consumers):
Inventory Service: A consumer application retrieves messages from the "OrderQueue" to update the inventory database, ensuring that the stock levels are adjusted based on the order details.
Payment Service: Another consumer application processes the payment for the order. It retrieves the order details from the queue, processes the payment, and updates the order status.
Shipping Service: Once the payment is confirmed, a shipping service retrieves the order details from the queue to arrange for the shipment of the products. It updates the shipping status and provides tracking information.
In this scenario, the e-commerce website (producer) sends order messages to the "OrderQueue," and multiple consumer services (inventory, payment, and shipping) independently process these messages. This setup ensures that each service can operate asynchronously and scale independently, improving the overall efficiency and reliability of the order processing system.
Different Types of Queues (Creating Queue)
When working with Amazon SQS, you have access to two main types of queues, each designed to meet different use cases and requirements:
Standard Queues
Standard queues offer maximum throughput, best-effort ordering, and at-least-once delivery. They are ideal for applications that need a high degree of scalability and can tolerate occasional duplicate messages or out-of-order message delivery. Here are some key features:
Unlimited Throughput: Standard queues support a nearly unlimited number of transactions per second (TPS) per action.
At-Least-Once Delivery: Messages are delivered at least once, but occasionally more than one copy of a message might be delivered.
Best-Effort Ordering: Messages are generally delivered in the order they are sent, but sometimes they might be delivered out of order.
FIFO Queues
FIFO (First-In-First-Out) queues are designed to ensure that messages are processed exactly once, in the exact order that they are sent. They are perfect for applications where the order of operations and events is critical. Key features include:
Exactly-Once Processing: Messages are delivered exactly once and remain available until a consumer processes and deletes them.
Ordered Delivery: Messages are strictly ordered, meaning they are delivered and received in the exact sequence they are sent.
Limited Throughput: FIFO queues support up to 300 transactions per second (TPS), but this can be increased to 3,000 TPS with batching.
Queue Configuration
Visibility Timeout: This is the period during which a message is hidden from consumers after being retrieved from the queue, preventing simultaneous processing by multiple consumers. If not deleted within this time, the message becomes visible again for another consumer to process.
Retention Period: This is the time messages are kept in the queue after being sent. Once expired, the message is automatically deleted, even if unprocessed. SQS allows setting this period from 1 minute to 14 days, useful for messages that may become obsolete.
Delivery Delay: This feature postpones message delivery to a queue, introducing a delay between sending and processing. The delay is set when sending the message, ensuring it isn't delivered until the specified time has passed.
Message Receive Delay: Similar to delivery delay but applied when receiving messages. It delays message visibility after it becomes available, preventing retrieval and processing until the delay period ends.
Sending and Receiving Messages
Sending a Message:
Navigate to the SQS dashboard and select the queue to which you want to send a message.
Click on the "Send and receive messages" button.
Enter the message body and any optional message attributes.
Click "Send message" to add the message to the queue.
Receiving a Message:
On the same queue page, click on "Poll for messages" to retrieve messages from the queue.
The messages will be displayed, showing details such as message ID, body, and attributes.
After processing, you can delete the message from the queue to prevent it from being processed again.
Choosing the Right Queue
Selecting between standard and FIFO queues depends on your application's specific needs:
Use Standard Queues if your application requires high throughput and can handle occasional duplicates or out-of-order messages.
Use FIFO Queues if your application requires strict message ordering and exactly-once processing.
By understanding the differences between these queue types, you can better design your system to meet your application's performance, reliability, and cost requirements.
Use Cases for SQS
Microservices Communication: SQS can be used to enable communication between microservices, ensuring that each service can operate independently.
Task Queues: It can be used to manage tasks that need to be processed asynchronously, such as image processing or data transformation.
Load Buffering: SQS can act as a buffer to handle sudden spikes in traffic, ensuring that your application remains responsive.
Choosing Between SQS and SNS
When deciding between SQS and SNS, it's important to consider the specific needs of your application. If you need to separate components and manage asynchronous communication, SQS is the right choice. On the other hand, if you need to send messages to multiple subscribers or broadcast notifications, SNS is the better option.
In some cases, you may need to use both services together. For example, you can use SNS to send a message to multiple SQS queues, allowing you to combine the benefits of both services.
Conclusion
Amazon SQS and SNS are powerful messaging services that can help you build scalable, reliable, and decoupled applications. By understanding their features and use cases, you can choose the right service for your needs and ensure efficient communication between the components of your application. Whether you need to handle asynchronous tasks, broadcast messages, or send notifications, AWS has the right tool for the job.
Subscribe to my newsletter
Read articles from Jasai Hansda directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jasai Hansda
Jasai Hansda
Software Engineer (2 years) | In-transition to DevOps. Passionate about building and deploying software efficiently. Eager to leverage my development background in the DevOps and cloud computing world. Open to new opportunities!