How To Send Messages Between Distributed Applications (AWS SQS)

How To Send Messages Between Distributed Applications (AWS SQS)

Amazon Simple Queue Service (SQS) is a fully managed message queuing service provided by AWS that enables decoupled and asynchronous communication between distributed systems. It helps applications exchange messages reliably and efficiently, even when components of the application operate at different rates or times.

SQS is commonly used in architectures that rely on microservices, serverless applications, and distributed systems.


Key Features of SQS

  • Fully Managed Service:

    • AWS takes care of provisioning, maintaining, and scaling the infrastructure.

    • Ensures high availability and durability.

  • Two Types of Queues:

    • Standard Queue:

      • Offers best-effort ordering and at-least-once delivery.

      • High throughput and scalability.

      • Ideal for use cases where message duplication or occasional out-of-order processing is acceptable.

    • FIFO Queue (First-In-First-Out):

      • Guarantees that messages are processed exactly once and in the exact order in which they are sent.

      • Throughput is lower compared to Standard Queues.

      • Suitable for financial transactions, task processing, and workflows where strict ordering is critical.

  • Message Durability:

    • Messages are stored redundantly across multiple AWS Availability Zones (AZs) for durability.
  • Message Visibility Timeout:

    • Once a message is retrieved by a consumer, it becomes "invisible" to other consumers for a specified duration. This prevents multiple consumers from processing the same message simultaneously.
  • Dead-Letter Queues (DLQs):

    • Messages that can't be processed successfully after a specified number of attempts can be moved to a DLQ for further investigation.
  • Scalability:

    • Automatically scales to handle virtually any number of messages per second.
  • Security:

    • Integration with AWS Identity and Access Management (IAM) for fine-grained access control.

    • Supports encryption at rest using AWS Key Management Service (KMS).

    • Can be configured to support encryption in transit using HTTPS.

  • Message Retention:

    • Messages can be retained in the queue for a configurable period, ranging from 1 minute to 14 days (default is 4 days).

How SQS Works

  1. Message Producers:

    • Applications or services that send messages to the queue.

    • Messages can contain up to 256 KB of text in any format (e.g., JSON, XML).

  2. Message Queue:

    • Stores the messages until they are retrieved and processed.

    • Decouples the producers and consumers.

  3. Message Consumers:

    • Applications or services that retrieve and process messages from the queue.

    • Can use AWS SDKs or APIs to poll the queue for messages.

  4. Visibility Timeout:

    • After a consumer retrieves a message, SQS makes it invisible for the visibility timeout period.

    • If the message is not deleted before the timeout expires, it becomes visible again for other consumers.


Use Cases

  1. Decoupling Microservices:

    • SQS allows microservices to communicate asynchronously without direct dependencies.
  2. Batch Processing:

    • Messages can be processed in batches for improved throughput and efficiency.
  3. Load Leveling:

    • SQS can act as a buffer between components operating at different speeds to smooth out traffic spikes.
  4. Event-Driven Architectures:

    • Used with services like AWS Lambda to trigger serverless functions upon receiving messages.
  5. Error Handling:

    • Dead-letter queues can store failed messages for debugging or reprocessing.
  6. Order Processing Systems:

    • FIFO queues ensure that orders are processed in the exact sequence in which they are placed.

Best Practices

  1. Optimize for Scale:

    • Use batching for sending and receiving messages to reduce costs and increase throughput.
  2. Handle Message Idempotency:

    • Ensure consumers can handle duplicate messages gracefully.
  3. Set Appropriate Visibility Timeout:

    • Configure the visibility timeout to match the expected processing time of your consumers.
  4. Use Dead-Letter Queues:

    • Configure DLQs to capture messages that fail processing after multiple attempts.
  5. Monitor and Log:

    • Use Amazon CloudWatch to track metrics like message volume, processing time, and errors.
  6. Secure Your Queues:

    • Use IAM policies and encryption to protect data and restrict access.

Integrations

  • AWS Lambda: Process messages in a serverless manner.

  • Amazon SNS (Simple Notification Service): Combine SQS with SNS for pub/sub messaging patterns.

  • Amazon EC2 or ECS: Use SQS to manage workloads for compute services.

  • Amazon EventBridge: Route events from multiple sources to SQS for processing.


Hands-On

Steps to Access the Amazon SQS Console

  1. Open the AWS Management Console

    • Click on the AWS Management Console link (or open it in your browser).

    • Log in with your AWS credentials (username and password).

  2. Search for SQS

    • Once logged in, type "queue" in the search bar at the top of the AWS Management Console.

    • From the search results, select Simple Queue Service (SQS) to open the SQS console.

  3. SQS Console Landing Page

    • If you are new to SQS and see the landing page with a "Get Started Now" button, click it to proceed.

    • If this page does not appear, it means you are already in the main SQS dashboard, so move to the next step to start creating or managing queues.

Creating an Amazon SQS Queue

In this step, we will create and configure an Amazon SQS queue. Queues act as a reliable buffer, enabling asynchronous communication between distributed applications or microservices. This is useful for tasks like decoupling systems, managing batches, or storing notifications.

For this tutorial, we’ll simulate an e-commerce application that stores incoming orders.

  1. Create a Queue:

    • Open the SQS console.

    • In the Queue Name field, enter Orders to name your queue.

  2. Select Queue Type:

    • Since strict ordering isn’t required for this use case, leave the default Standard Queue type selected.
  3. Queue Configuration:

    • SQS allows customization of settings like retention period, message size, and delivery delays.

    • For this tutorial, we’ll use the default settings. Click Quick-Create Queue to proceed.

  4. Queue Created:

    • Your new queue named Orders is now created and will appear in the queue list, ready for use.

Sending Messages to the Amazon SQS Queue

Now that the Orders queue is created, it is ready to receive messages that capture the details of new orders from the e-commerce application. Follow these steps to send a message to the queue:

  • Select the Queue:

    • Ensure your Orders queue is selected in the list of queues.

    • Choose Send and receive Messages. This opens the Send and receive Messages dialog box.

  • Enter the Message Body:

    • In the Message Body tab, type the following text to represent a sample order:

    •   1 x Widget @ $29.99 USD
        2 x Widget Cables @ $4.99
      
  • Add Message Attributes:

    • Go to the Message Attributes tab to include optional metadata for easier message processing.

    • Add the following:

      • Name: Order-Type

      • Type: String

      • Value: Online

    • Click Add Attribute to save the attribute.

  • Send the Message:

    • Click Send message to immediately send the order to the queue.

    • A confirmation will appear, indicating the message was successfully sent.

Retrieve and Delete a Message from the Amazon SQS Queue

Once a message has been sent to the queue, it can be retrieved and processed by an application or manually by you for demonstration purposes. In this example, we will retrieve a message from the Orders queue, view its content, and delete it.

  1. Select the Queue:

    • Make sure the Orders queue is selected in the list of queues.

    • Choose Send and receive Messages. This opens the Send and receive Messages dialog box.

  2. Retrieve Messages:

    • In the dialog box, click Polling for messages to retrieve messages from the queue.

    • SQS does not allow you to specify a particular message; instead, it retrieves up to 10 messages at a time based on the polling request.

  3. Delete a Message:

    • After viewing and processing the message, select the message you want to delete.

    • Select the message and Click on Delete to remove the message from the queue.

  4. Confirm Deletion:

    • In the Delete Messages dialog box, Click on Delete to permanently delete the message.

Deleting an Amazon SQS Queue

  • Select the Queue:

    • In the list of queues, select the Orders queue.

    • Choose Delete.

  • Confirm Deletion:

    • The Delete queue dialog box will appear.

    • Type confirm to agree.

    • Click Delete to confirm the deletion.

  • Completion:

    • The queue will be removed from the list and is no longer available.

Summary

Amazon SQS is a powerful and fully managed message queuing service that enables reliable communication between distributed systems. By creating a queue, sending and retrieving messages, and managing message attributes, we demonstrated how SQS can be used to decouple components, improve scalability, and handle asynchronous workflows in an e-commerce application.

In this tutorial, we covered key operations such as creating a queue, sending a sample order message, retrieving and deleting a message, and finally, deleting the queue to follow best practices. This step-by-step walkthrough highlights how simple and effective SQS is for building robust and scalable applications. By understanding and leveraging these core functionalities, you can implement SQS in your projects to achieve greater reliability and efficiency in your architecture.

Did you find this article valuable?

Support Bosonique ITEdTech by becoming a sponsor. Any amount is appreciated!