1
00:00:00,290 --> 00:00:03,550
So let's discuss Amazon SQS long polling.

2
00:00:03,550 --> 00:00:05,260
So the idea is that when a consumer

3
00:00:05,260 --> 00:00:07,250
is requesting messages from a queue,

4
00:00:07,250 --> 00:00:08,840
it can wait for messages to arrive

5
00:00:08,840 --> 00:00:10,370
if there are none in the queue

6
00:00:10,370 --> 00:00:12,130
and this is called long polling.

7
00:00:12,130 --> 00:00:14,480
The reason you would do so is for twofolds.

8
00:00:14,480 --> 00:00:16,170
Number one, to decrease latency

9
00:00:16,170 --> 00:00:19,440
and number two, to reduce the number of API calls

10
00:00:19,440 --> 00:00:20,780
you are doing onto SQS.

11
00:00:20,780 --> 00:00:22,100
So let's see how this works.

12
00:00:22,100 --> 00:00:25,100
So we have SQS queue and it is empty.

13
00:00:25,100 --> 00:00:28,090
So our consumer is going to poll the queue

14
00:00:28,090 --> 00:00:30,950
and is going to poll it for up to 20 seconds.

15
00:00:30,950 --> 00:00:32,750
And the reason we do so is that

16
00:00:32,750 --> 00:00:35,180
if the queue is empty, we just wait.

17
00:00:35,180 --> 00:00:37,790
It's okay to just wait if the queue is empty

18
00:00:37,790 --> 00:00:39,880
so that when a message arises

19
00:00:39,880 --> 00:00:42,000
like it's just arriving in the message.

20
00:00:42,000 --> 00:00:44,420
If a consumer is still in the long poll

21
00:00:44,420 --> 00:00:46,510
then automatically that message will be sent

22
00:00:46,510 --> 00:00:49,280
to the consumer, which is with low latency

23
00:00:49,280 --> 00:00:52,030
and will automatically arrive as fast as possible.

24
00:00:52,030 --> 00:00:53,190
So this makes sense,

25
00:00:53,190 --> 00:00:55,870
long polling will decrease the number of API calls

26
00:00:55,870 --> 00:00:58,880
made to SQS while increasing the efficiency

27
00:00:58,880 --> 00:01:01,960
and decreasing the latency of your application.

28
00:01:01,960 --> 00:01:05,030
So you can configure it between one to 20 seconds

29
00:01:05,030 --> 00:01:08,220
with obviously the longer, the more preferable.

30
00:01:08,220 --> 00:01:11,230
And then you would obviously prefer, in my opinion,

31
00:01:11,230 --> 00:01:13,120
long polling to short polling.

32
00:01:13,120 --> 00:01:14,460
So make sure you configure it.

33
00:01:14,460 --> 00:01:16,830
And there are two ways to configure long polling.

34
00:01:16,830 --> 00:01:18,450
Either you configure it at the queue level

35
00:01:18,450 --> 00:01:21,130
such as any consumer doing a poll from the consumer

36
00:01:21,130 --> 00:01:22,800
will have long polling enabled

37
00:01:22,800 --> 00:01:26,800
or the consumer itself can choose to do long polling

38
00:01:26,800 --> 00:01:29,530
by specifying the wait timeout seconds.

39
00:01:29,530 --> 00:01:31,180
So anytime at the exam you see

40
00:01:31,180 --> 00:01:33,080
a way to optimize the number of API calls

41
00:01:33,080 --> 00:01:35,970
made to an SQS queue and decrease of latency,

42
00:01:35,970 --> 00:01:36,917
think about long polling.

43
00:01:36,917 --> 00:01:38,980
That's it for this lecture, I hope you liked it.

44
00:01:38,980 --> 00:01:40,930
And I will see you in the next lecture.

