‫Now let's talk about
‫some more concepts you need to know for SQS,
‫but more at the developer level.
‫So, the first one is called Long Polling.
‫So when a consumer requests a message from SQS,
‫it has the option to "wait" for messages to arrive
‫if somehow the queue is empty.
‫And this is called Long Polling.
‫So let's take an example.
‫We have an SQS queue and it is empty
‫and the consumer does a poll request into the SQS Queue.
‫Now we have the option to "wait"
‫and it's fine to wait, right?
‫Because there are no messages.
‫So that means that if during the waiting period
‫a message goes into the SQS Queue,
‫then the message will be received at the consumer then.
‫So why do we do Long Polling?
‫Well, we do Long Polling because we are doing
‫less API calls into the SQS Queue.
‫And on top of it, we know that
‫as soon as the message will arrive in the SQS Queue,
‫then the SQS Queue will send it back to the consumer.
‫So we are increasing the efficiency
‫because we do less API calls,
‫so less CPU cycles are used.
‫And we are also decreasing the latency
‫because as soon as a message will be received
‫by your SQS Queue,
‫it will be received by your consumers
‫so Long Polling is a no-brainer.
‫Long Polling you can set between one second to 20 seconds,
‫but 20 seconds is preferable, why not?
‫And overall it is a recommendation in your applications,
‫to use Long Polling to Short Polling.
‫So you will see in the exam some questions
‫maybe telling you that the consumer
‫is doing too many calls into the SQS Queue,
‫and that is costing you money and CPU cycles,
‫and maybe increasing the latency,
‫then Long Polling is going to be the option.
‫Now long Polling can be either enabled at the Queue level,
‫so Queue level setting, or at the API call level
‫so whenever your consumer does an API call
‫into SQS Queue for polling,
‫using the ReceiveMessageWaitTimeSeconds parameter.
‫The second thing we need to look at,
‫is the SQS Extended Client.
‫So, as we know the max message limit is 256 kilobytes,
‫so how do we go ahead by sending large messages
‫into your SQS Queue, for example one gigabytes messages?
‫For this, we can use a Java Library
‫called the SQS Extended Client
‫which does something very simple
‫that you could implement in any other language.
‫But the idea is that it will use an Amazon S3 bucket,
‫as a repository for the large data.
‫So lets take an example,
‫your producer wants to send a large message into SQS
‫but first what's going to happen is that
‫the actual large message, will end up in Amazon S3.
‫And what will be sent into your SQS Queue
‫will be small metadata message
‫that has a pointer to the larger message
‫in your Amazon S3 bucket.
‫So the SQS Queue will contain small messages
‫and your Amazon S3 bucket will contain large objects.
‫And your consumer, when he reads from the SQS Queue,
‫using this library, the SQS Extended Client,
‫then he will consume at this moment a data message,
‫which will say to the consumer,
‫" Hey, glory that bigger message out of Amazon is free."
‫And the consumer will be able to read
‫and retrieve large messages from S3.
‫So a typical use case for this
‫is if you're processing video files,
‫you don't send the entire video file into your SQS Queue No,
‫you upload that video file into your Amazon S3 bucket,
‫and you send a small message with a pointer
‫to that video file into your SQS Queue.
‫And that allows you to accommodate
‫any message size really through this pattern.
‫Finally, you will maybe see some API calls
‫are given through by the exam and so,
‫it's just normal API calls
‫that you should understand by now,
‫but let's go over them.
‫So CreateQueue is used to create a Queue
‫and you can use the argument MessageRetentionPeriod
‫to set how long a message should be kept in a queue
‫before being discarded.
‫And DeleteQueue is used to delete a queue
‫and delete all the messages in the queue at the same time.
‫PurgeQueue is an API call that is used to
‫delete all the messages in the queue as well.
‫Now, when we are sending messages,
‫we use the SendMessage API.
‫And if we want to send messages with delay,
‫we can use the DelaySeconds parameter.
‫ReceiveMessage is to do polling,
‫and DeleteMessage is to delete a message
‫once it has been processed by a consumer.
‫So when you receive a message by default,
‫the parameter MaxNumberOfMessages is set to one
‫that means that you receive one message at a time,
‫but you can receive up to 10 messages at a time in SQS
‫so you can set the MaxNumberOfMessages parameter,
‫for the ReceiveMessage API to 10
‫to receive a batch of messages from SQS.
‫The ReceiveMessageWaitTimeSeconds
‫is telling your consumer how long to wait
‫before getting a response from the queue,
‫and this is equivalent of enabling long Polling
‫and the ChangeMessageVisibility,
‫is used to change the message timeout
‫in case you need more time to process a message.
‫Now, if you want to use Batch API calls
‫you can do so for SendMessage, DeleteMessage
‫as well as ChangeMessageVisibility,
‫and this help decrease the number of API calls
‫you're doing into API,
‫and therefore help decrease your cost.
‫So that's it.
‫Now let's have a look at how Long Polling works in AWS.
‫So let's go into our DemoQueue
‫and we'll edit the settings of our DemoQueue.
‫So as we can see here,
‫the Receive message wait time is currently zero
‫which is called a Short Polling,
‫but we can set it anywhere between zero and 20 seconds.
‫So as soon as I set it to one you enable Long Polling,
‫but we'll set it to 20,
‫and this is saying that
‫you should wait up to 20 seconds to receive a message
‫if the queue is empty.
‫So I will just go and apply the settings.
‫I'll save, make your configuration,
‫and then I will go into my Send and receive messages,
‫and in here, I'm gonna go ahead and start a consumer.
‫Now this consumer is doing Long Polling
‫because he was set at the Queue level,
‫and so that means that only one API call is happening,
‫and it's waiting for a message coming from the SQS Queue,
‫because right now there is none
‫but if I say, "hello world," and just press sent,
‫as soon as I press sent,
‫the message was received by my consumer
‫this was extremely low latency
‫because my consumer was in Long Polling mode
‫and he was waiting for a message from SQS.
‫Thanks to the wait message time setting
‫that we set from before.
‫So that's, it's very simple demo
‫but hopefully that makes sense.
‫I will see you in the next lecture.