2.4 SQS: Message Queues
Message queues are not a new concept in software engineering, but having a simple and integrated solution in AWS helps developers a lot. In this lesson, we will create a message producer and consumer that connect to SQS (Simple Queue Service).
1.Introduction3 lessons, 13:15
2.Amazon Web Services10 lessons, 1:17:39
3.Conclusion1 lesson, 01:25
2.4 SQS: Message Queues
Hi, welcome back to Explore Amazon Web Services. In this lesson, we're going to take a look at simple message queue or sqs. Message queues are a convenient way to distribute workload over a number of workers. There are two actors in this scenario, producers put things into the queue while consumers take those things out of the queue. Each message is normally only processed by one consumer and those only take messages if they have finished the previous one. So let's first create a queue. In my example, I will be doing some processing of orders of an imaginary online shop, as we'll continue through the next several lessons. After creating the queue, you can see that it got assigned a URL that we can reference to access the queue. To showcase producers and consumers, I will create a new JS project that contains both. First I'm going to define the package dependencies. We need the aws-sdk and the handy module called sqs consumer, by the British BBC, that allows you to do long polling on a queue. Long polling means that whenever a request for more data times out because nothing happened during a certain amount of time, it issues another request and, again, waits for more data. After installing the node modules, we can start writing our consumer. We first need to require our consumer object and then initialize it with the queue URL. We also need to define a method to handle the message. Messages normally are string representations so we need to pause it to chase them and can output the order ID. It is not very exciting, I know. After we finish processing, we call the done callback to notify consumer that we've finished our work, and accept a new one. We can also define an error call back, finally, we start the consumer with app.start. To run it switch to your shell, and execute node consumer js, it is now waiting for data. Onto the producer, we first require the aws-sdk, and create a new SQS object specifying the region and the queue URL. I'm now creating an order object that gets a random ID between 100 and 1000, customer name, and email, some items, and the totals, and text. Then I'm going to send a message to the queue specifying the message body as adjacent string. A callback takes an error and response metadata I'll just lock that when we run the producer in the shell you can see what this data looks like. You receive a message ID and an MD5S of your message body to verify integrity. Switching to the consumer we can see it started processing that data and locks the order ID. Let's stop the consumer for a moment and issue a new run of the producer. When we switch to the console of SQS and refresh the page, we can see the message queue contains one message. We can view it right here, including all details like the sender account and content. When we start our consumer again, the message gets processed. Now the last thing I wanted to show was the single consumer processing. I created a few colored tabs to make it easier to distinct between consumers which are orange, and the producer which is green. I have two consumers running and will now issue some messages with the producer. As you can see, the first consumer received two of them, the second one only one. Now you have an insight how message queues work, especially with SQS, of course more than message queue servers like or. Also offer other types of delivery, such as topics. But on aws this is a separate service called sms at which we look at in the next lesson. See you there. [SOUND]