So let's discuss Amazon SQS long polling.
So the idea is that when a consumer
is requesting messages from a queue,
it can wait for messages to arrive
if there are none in the queue
and this is called long polling.
The reason you would do so is for twofolds.
Number one, to decrease latency
and number two, to reduce the number of API calls
you are doing onto SQS.
So let's see how this works.
So we have SQS queue and it is empty.
So our consumer is going to poll the queue
and is going to poll it for up to 20 seconds.
And the reason we do so is that
if the queue is empty, we just wait.
It's okay to just wait if the queue is empty
so that when a message arises
like it's just arriving in the message.
If a consumer is still in the long poll
then automatically that message will be sent
to the consumer, which is with low latency
and will automatically arrive as fast as possible.
So this makes sense,
long polling will decrease the number of API calls
made to SQS while increasing the efficiency
and decreasing the latency of your application.
So you can configure it between one to 20 seconds
with obviously the longer, the more preferable.
And then you would obviously prefer, in my opinion,
long polling to short polling.
So make sure you configure it.
And there are two ways to configure long polling.
Either you configure it at the queue level
such as any consumer doing a poll from the consumer
will have long polling enabled
or the consumer itself can choose to do long polling
by specifying the wait timeout seconds.
So anytime at the exam you see
a way to optimize the number of API calls
made to an SQS queue and decrease of latency,
think about long polling.
That's it for this lecture, I hope you liked it.
And I will see you in the next lecture.