[AWS] Tutorial SQS (Simple Queue Service)
What is Simple Queue Service in AWS?
Amazon Simple Queue Service is a message queue used by distributed applications to exchange messages through a polling model, and can be used to decouple sending and receiving components.
Purpose of SQS:
In modern world of frameworks and micro-service architecture the elimination of dependency is very important since if two services are dependent on each other. If one service fails then the other might lose the effective data. In order to eliminate these dependencies and to provide an assured service, SQS was brought into picture. There are plenty of other use cases too but this is the core purpose of SQS.
Types of Queues in SQS:
There are two different types of queues provided by SQS service and both have their own functionalities and different pricing.
- Standard Queue
- FIFO Queue
The major difference between them is that the Standard queue handles the messages( data that we send ) in a random way and allows duplicate messages to hold inside the queue, While the FIFO queue allows the message as a proper queue: first message that comes in gets served out first. Also FIFO will not allow duplicate messages inside the queue.
Pricing difference: For both the queues First 1 Million Requests/Month is Free. After the 1 Million Request the Standard queue costs $0.40 for the next 1 Million to 100 Billion Requests and the FIFO queue costs $0.50 for the next 1 Million to 100 Billion Requests. When the number of request is increases then the pricing range will also increase.
Courtesy to AWS
The reason why FIFO queue costs more is because of extra services provided by AWS like FIFO order and the de-duplication provided by the AWS.
How to setup SQS from the console:
Now lets see in depth about how we can create a simple queue through the AWS console and what are the available configurations in AWS.
- Visit the AWS console and search for the SQS service. Then click on the create queue button.
- In the create queue page you can choose the type of the queue. And then give your queue a name (note: A queue with type FIFO the name of the queue must include .fifo at the end).
- Visibility timeout: This is the time period of how long the message that is polled out of the queue is in the isolated mode where the other consumer cannot access the polled out message.
- Message retention period: This is the time period till which the message is allowed to stay inside the queue. After this period the message gets deleted automatically from the queue.
- Delivery delay: This is the delayed delivery time duration for the consumer and it can be maximum up to 15 mins.
- Maximum message size: Every message should be within only 256 KB.
- Receive message wait time: The receive message wait time is the maximum amount of time that polling will wait for messages to become available to receive. It can be up to 20 seconds
I have chosen all the default values and have not altered any configurations.
- In general AWS provides a basic security for all the messages that are stored in the queue. You can also configure a new service key for the queue and make it more secure.
- You can disable the encryption too but it it not advisable though.
- You can make the queue to be only accessed by a specific role group or a particular IAM user.
- You can choose Advanced and configure the policy as a JSON like you do in the IAM console provided by AWS. I am going to choose the default configurations.
The Re-drive allow policy will help you to redirect the data in the Dead Letter Queue to your actual queue. It can be enabled by enabling the Re-drive allow policy and choosing the dead letter queue ARN. Let’s deal with the dead letter queue in the next article. I have disabled Re-drive allow policy.
You can set up a Dead Letter Queue which helps in storing the messages that were crashed while processing / failed to process a proper outcome. Lets discuss about the Dead Letter Queue in detail in the next article, I have disabled the Dead Letter Queue.
To organize and identify your Amazon SQS queues for cost allocation, you can add metadata tags that identify a queue’s purpose, owner, or environment. This is especially useful when you have many queues. It is optional and is not mostly required for most of the cases.
Now click the Create queue button to create the queue and Let’s start sending and receiving messages.
As you can see the queue has been created. Now click the queue name and click the send and receive message button.
Here you can pass a payload / message through the console and you can delay the delivery of the messages to the queue by mentioning the duration in the delivery delay field.
Also you can add message attributes to the messages. Message attributes are structured metadata (such as timestamps, signatures, and identifiers) that are sent with the message.
After sending the message you will get a success pop up and you can review the message details and its id and other attributes of the message by clicking view details.
In receive message division you can see the available messages in the queue and you can poll the messages out of the queue and view the messages.
Click the ‘Poll for messages’ to receive the messages. You can also configure the number of messages to be polled out of the queue and the poll duration.
As you can see that the message that we sent is now viewable. ( Note : the receive count here states that the no of times the message has been polled out of the queue and was made available to the consumer). You can click the ID and check the message details.
And with that we have covered the basics of how SQS can be configured. I would advice you guys to play with these configurations to get an in-depth knowledge about these configurations. My next article would cover how we can setup this queue as a trigger for a lambda function.
About the Author
Sangar Naarayanan R is a Software Development Engineer in KBX digital. With a hands-on experience and good knowledge in various technologies and looking forward how to upgrade his own skills by researching every day and provide the best possible solutions.
About KBX Digital
At KBX Digital, we use server-less technology to auto-scale micro-services to serve millions of customers.