Amazon Simple Queue Service (SQS) is a service that is used whenever you want to decouple your application or for asynchronous execution. There is detailed documentation on the official AWS website. In this article, I am going to share my personal experience with SQS and a demo to get started.
In case you are completely new to AWS then look into this article about what AWS is and what solution it offers?
- What is SQS?
- Difference between synchronous and asynchronous execution
- How does SQS work?
- How did I use SQS?
What is SQS?
According to documentation “Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS eliminates the complexity and overhead associated with managing and operating message-oriented middleware and empowers developers to focus on differentiating work. Using SQS, you can send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available. Get started with SQS in minutes using the AWS console, Command Line Interface or SDK of your choice, and three simple commands.”
Difference between synchronous and asynchronous execution
To understand the use of SQS, first, we need to understand synchronous and asynchronous execution. In short words, our application waits until we get a response in synchronous execution. For example, when you check for your bank balance you get a response immediately. In this case, the application waits until it gets a response back. At the time, let’s say you buy a product online. In that case, the application doesn’t wait until your product reaches home.
How does SQS work?
As I said, SQS works for asynchronous execution. So the producer application puts the data into the SQS queue and does not wait for the processing of data. Also, there will be a consumer machine that will keep polling for messages from the queue and process the data. There is something called visibility timeout in SQS configuration which makes sure that the same message is not being polled before the consumer machine processes the data. So make sure you configure the visibility timeout higher than the time the consumer machine would take to process.
How did I use SQS?
When proceeding with any event-driven architecture, the first thing that comes to my mind is Amazon SNS, Amazon SQS, and AWS Lambda. Not to mention the Amazon SNS and SQS jell really well. If you are not aware of what SNS is then take a look here.
Also, there is a feature called the dead letter queue which makes sure that our data is not lost in case of any failure. Do keep in mind that there is a size restriction of only 256KB per message.
Before beginning with SQS, create an SNS topic. In case you are not sure how to do that, go to the demo part of this article.
Now that you have created a SNS topic it’s time to create a SQS queue. Search for SQS in the search bar and you will arrive at the SQS console.
Step 1: Click “Create queue”.
Step 2: Enter the queue name as shown above and click create queue.
Step 3: Go to SNS subscription tab and click Subscribe to Amazon SNS topic.
Step 4: Select the SNS topic and click save.
Now it is time to test things!! Go to the SNS topic and click publish message.
Enter a message and click publish message. Now to your SQS queue and click poll for messages.
You will find a record. Open it. You can see your message under the “Message” object.
The above demo which is shown using the console can also be done using an API and CLI. If you feel comfortable with the console then understanding the API wouldn’t be much of a problem. Happy programming!!