Asynchronous pattern using Amazon SQS

Published by Vignesh M on

HTTP (HyperText Transfer Protocol),the underlying protocol in the world of web is commonly used for synchronous communications. However there are some use cases which needs the activities to be done in asynchronous way. Message brokers can help to integrate systems using asynchronous pattern. In this post, let see how Amazon SQS services provided by AWS(Amazon Web Services) can be used to design the asynchronous pattern.

Synchronous pattern

Let’s see a simple example of synchronous communication using HTTP.

  • Client sends request to server.
  • Server persists the data from request into database.
  • Until the time server process the request and persist the data, client waits for the response from server.

As we can see, the synchronous communication is simple and straight forward. But when there are heavy burst of requests, both client and server will have tremendous load. When server goes down with maximum load, there is a potential of message loss and requests getting timed out.

Asynchronous pattern

Let’s introduce Amazon SQS into the above synchronous pattern.

  • Client sends request to server.
  • Server put the message into SQS queue.
  • The server returns the acknowledgement response(“Your request has been submitted successfully for processing”) to client immediately.
  • The consumer applications then pulls the message from SQS queue to do further computation.

Why Amazon SQS?

Alright! After taking a quick look into both synchronous and asynchronous patterns, let’s acknowledge that having message queues into our designs will help for distributed architectures. But why to choose Amazon SQS in particular?

  • Amazon SQS is a fully managed message queuing service.
  • Easy to scale and highly distributed without any administrative overhead.
  • Integrates seamlessly with Amazon Lambda. (If the entire architecture stack is decided to be hosted in AWS cloud offering)
  • SQS operations can be performed via AWS console, SDK of choice, Command line interface.

SQS – Visibility Timeout

One of the important concept to consider when choosing SQS is visibility timeout.

  • Visibility timeout is the period for which the messages will be hidden from consumers.
  • Once the message is consumed for first time, the visibility timeout starts. Default is 30 seconds. Maximum is 12 hours.
  • Consumers of message have to explicitly delete the message after receiving.
  • It’s good to have visibility timeout period to be configured with value more than the time consumer needed for processing the message.

Amazon SQS – Examples

Let’s see some examples to create queue, put messages to queue, consume messages from queue using AWS SDK for Java.

Send messages to Queue

Send Batch messages to Queue

SQS client provider

Util methods to build input messages

Consume messages from Queue

Consume multiple messages from Queue. Poll for messages in a loop as a standalone consumer.

Amazon Lambda as event listener for SQS Queues

  • SQS event can be made as a trigger for Amazon Lambda.
  • Based on the messages coming into SQS, Lambda can increase or decrease polling frequency.
  • Very helpful in reducing usage cost of SQS since we don’t have to keep on polling the queue all the time.

If Lambda is configured as event listener for SQS queues, then the event can be handled as below:

To illustrate sending error messages to Dead Letter Queue(DLQ), message with id=3 is moved to DLQ.

Dead Letter Queue(DLQ)

DLQ is yet another SQS queue. It can be configured to receive messages which are not consumed after certain attempts or to send any error messages explicitly.

When creating SQS queue, we can configure it’s DLQ as below:

Alternatives to SQS

Other message brokers as an alternative to SQS are:

  • Amazon MQ – Managed service provided by AWS based on Active MQ
  • Apache Active MQ – Open source alternative
  • Rabbit MQ – Open source alternative
  • IBM MQ


We have looked into the details of SQS service from Amazon offering. SQS service helps to build a scalable message queueing architectures. But that’s not the only option. If our architecture stack is not based on AWS, then open source alternatives like Active MQ and Rabbit MQ provides a reliable messaging solution.


The source code examples can be downloaded from below location:


  1. Amazon SQS documentation
  2. Amazon SQS developer guide


Vignesh M

Java developer , AWS Certified Solutions Architect Associate and Cloud technology enthusiast. Currently working for Clarivate. He believes that knowledge increases by sharing not by saving.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.