1 00:00:00,320 --> 00:00:02,640 So now let's talk about an important concept, 2 00:00:02,640 --> 00:00:05,560 which is called the message visibility timeout. 3 00:00:05,560 --> 00:00:08,480 So when a message is polled by a consumer, 4 00:00:08,480 --> 00:00:11,350 it becomes invisible to other consumers. 5 00:00:11,350 --> 00:00:12,410 So let's take an example. 6 00:00:12,410 --> 00:00:14,830 We have the time going from left to right, 7 00:00:14,830 --> 00:00:18,160 and we have a consumer doing a ReceiveMessage request, 8 00:00:18,160 --> 00:00:22,520 and therefore, a message will be returned from the queue. 9 00:00:22,520 --> 00:00:25,460 Now the visibility timeout begins. 10 00:00:25,460 --> 00:00:28,430 And by default, that message visibility timeout 11 00:00:28,430 --> 00:00:30,160 is 30 seconds. 12 00:00:30,160 --> 00:00:32,890 That means that during these 30 seconds, 13 00:00:32,890 --> 00:00:35,200 the message has to be processed, okay? 14 00:00:35,200 --> 00:00:36,400 And if you do so, 15 00:00:36,400 --> 00:00:39,650 that means that if the same or other consumers 16 00:00:39,650 --> 00:00:42,930 do a message request API call, 17 00:00:42,930 --> 00:00:45,260 then the message will not be returned. 18 00:00:45,260 --> 00:00:47,860 And again another one during the timeout window, 19 00:00:47,860 --> 00:00:49,020 the message will not be returned. 20 00:00:49,020 --> 00:00:51,800 So effectively, during the visibility timeout, 21 00:00:51,800 --> 00:00:55,030 that message is invisible to other consumers. 22 00:00:55,030 --> 00:00:58,440 But after the visibility timeout is elapsed 23 00:00:58,440 --> 00:01:01,140 and if the message has not been deleted, 24 00:01:01,140 --> 00:01:02,560 then the message will be 25 00:01:02,560 --> 00:01:05,970 quote unquote put back into the queue 26 00:01:05,970 --> 00:01:08,990 and therefore, another consumer or the same consumer 27 00:01:08,990 --> 00:01:11,570 doing a receive message API call 28 00:01:11,570 --> 00:01:15,760 will receive the message again, the same message as before. 29 00:01:15,760 --> 00:01:17,870 So, this is something that's really important to understand. 30 00:01:17,870 --> 00:01:20,490 As you can see, while we receive a message, 31 00:01:20,490 --> 00:01:24,250 it becomes invisible during the visibility timeout period. 32 00:01:24,250 --> 00:01:27,740 Now, if we take a look at the same diagram, 33 00:01:27,740 --> 00:01:28,640 what we notice is that 34 00:01:28,640 --> 00:01:31,280 if we don't process a message 35 00:01:31,280 --> 00:01:34,030 within the visibility timeout window, 36 00:01:34,030 --> 00:01:37,370 then it will be processed twice potentially, right? 37 00:01:37,370 --> 00:01:40,480 Because it will be received by two different consumers, 38 00:01:40,480 --> 00:01:42,660 or twice by the same consumer. 39 00:01:42,660 --> 00:01:47,160 And so, if a consumer is actively processing a message 40 00:01:47,160 --> 00:01:49,560 but knows that it needs a bit more time 41 00:01:49,560 --> 00:01:51,900 to process the message because otherwise, 42 00:01:51,900 --> 00:01:54,690 it will go out of the visibility timeout window, 43 00:01:54,690 --> 00:01:58,370 there is an API called ChangeMessageVisibility. 44 00:01:58,370 --> 00:01:59,870 And so if a consumer knows 45 00:01:59,870 --> 00:02:02,460 that a message needs a bit more time to be processed 46 00:02:02,460 --> 00:02:04,620 and you don't want to process that message twice, 47 00:02:04,620 --> 00:02:06,090 then the consumer should call 48 00:02:06,090 --> 00:02:08,930 the ChangeMessageVisibility API to tell SQS, 49 00:02:08,930 --> 00:02:12,340 hey, do not make that message visible for now, okay? 50 00:02:12,340 --> 00:02:14,840 I just need a bit more time to process this message. 51 00:02:14,840 --> 00:02:17,840 And so, how do you set this message visibility timeout? 52 00:02:17,840 --> 00:02:18,673 Well, if you set it to 53 00:02:18,673 --> 00:02:21,720 a really, really high value by default, say, hours, 54 00:02:21,720 --> 00:02:23,330 and the consumer crashes, 55 00:02:23,330 --> 00:02:26,930 then it will take hours until this message reappears, 56 00:02:26,930 --> 00:02:29,730 re-becomes visible in your SQS queue, 57 00:02:29,730 --> 00:02:31,780 and that will take a lot of time. 58 00:02:31,780 --> 00:02:33,470 If you set it to something really, really low, 59 00:02:33,470 --> 00:02:34,760 like a few seconds, 60 00:02:34,760 --> 00:02:36,100 what happens that if the consumer 61 00:02:36,100 --> 00:02:38,360 doesn't end up having enough time to process that message 62 00:02:38,360 --> 00:02:39,470 for whatever reason, 63 00:02:39,470 --> 00:02:41,650 then it will be read many times by different consumers 64 00:02:41,650 --> 00:02:43,810 and you may get duplicate processing. 65 00:02:43,810 --> 00:02:46,880 So the idea is that the visibility timeout 66 00:02:46,880 --> 00:02:49,620 should be set to something reasonable for your application 67 00:02:49,620 --> 00:02:51,150 and your consumer should be programmed 68 00:02:51,150 --> 00:02:53,500 that if they know they need a bit more time, 69 00:02:53,500 --> 00:02:56,350 then they should call the ChangeMessageVisibility API 70 00:02:56,350 --> 00:02:58,810 to get more time and increase the timeout 71 00:02:58,810 --> 00:03:00,250 of that visibility window. 72 00:03:00,250 --> 00:03:01,460 But understanding this concept 73 00:03:01,460 --> 00:03:03,670 is very important from an exam perspective 74 00:03:03,670 --> 00:03:05,990 because there will be scenarios on that. 75 00:03:05,990 --> 00:03:07,460 So, let's go in the console 76 00:03:07,460 --> 00:03:10,010 to see how that works in practice. 77 00:03:10,010 --> 00:03:11,950 So I'm going to open 78 00:03:11,950 --> 00:03:16,890 two windows of send and receive messages 79 00:03:16,890 --> 00:03:18,920 just to show you how that works, so. 80 00:03:18,920 --> 00:03:22,350 In the first window, I'm going to enter a hello world 81 00:03:22,350 --> 00:03:26,090 and it's going to be sent into the queue. 82 00:03:26,090 --> 00:03:27,170 And if you remember, 83 00:03:27,170 --> 00:03:31,840 the queue has a default timeout of 30 seconds. 84 00:03:31,840 --> 00:03:34,250 So what's going to happen is that I have two consumers, 85 00:03:34,250 --> 00:03:37,390 I have the first window and the second window consumer, 86 00:03:37,390 --> 00:03:39,970 and so I'm going to read the messages from the first window. 87 00:03:39,970 --> 00:03:41,740 So let's poll for messages. 88 00:03:41,740 --> 00:03:43,200 Now my message has appeared right here, 89 00:03:43,200 --> 00:03:44,730 it has been received. 90 00:03:44,730 --> 00:03:48,820 And if I go into my second consumer and poll for messages, 91 00:03:48,820 --> 00:03:51,800 as you can see, the message does not appear in here. 92 00:03:51,800 --> 00:03:54,400 And the reason it doesn't appear is that we are still within 93 00:03:54,400 --> 00:03:58,240 the visibility window timeout of that message 94 00:03:58,240 --> 00:04:00,330 and therefore, during these 30 seconds 95 00:04:00,330 --> 00:04:03,080 this message is trying to be processed by this consumer, 96 00:04:03,080 --> 00:04:06,170 this consumer right here will not see it. 97 00:04:06,170 --> 00:04:08,510 But let's assume that we stop polling, okay? 98 00:04:08,510 --> 00:04:11,030 So we don't delete the message 99 00:04:11,030 --> 00:04:14,440 and we know this message will timeout at some point. 100 00:04:14,440 --> 00:04:16,170 What will happen is that yes, I'm already here, 101 00:04:16,170 --> 00:04:18,100 in the second window, in the second consumer, 102 00:04:18,100 --> 00:04:19,560 this is the second consumer, 103 00:04:19,560 --> 00:04:20,820 then the message has been received 104 00:04:20,820 --> 00:04:24,060 because it was being put back into the queue. 105 00:04:24,060 --> 00:04:26,670 And now let's assume we do something correctly, 106 00:04:26,670 --> 00:04:28,780 we delete that message, 107 00:04:28,780 --> 00:04:30,790 then we have fully processed that message. 108 00:04:30,790 --> 00:04:33,230 But remember, that message was received twice. 109 00:04:33,230 --> 00:04:35,750 It said received count was two. 110 00:04:35,750 --> 00:04:37,330 So it's important for you to understand 111 00:04:37,330 --> 00:04:38,610 how this visibility window works, 112 00:04:38,610 --> 00:04:41,050 and this was a good demonstration of it. 113 00:04:41,050 --> 00:04:45,210 Now, if you wanted to change this setting by default, 114 00:04:45,210 --> 00:04:47,930 what you could be doing is go into Edit, 115 00:04:47,930 --> 00:04:49,750 and then for visibility timeout, 116 00:04:49,750 --> 00:04:51,660 you could set the default value 117 00:04:51,660 --> 00:04:54,210 between zero seconds, which is definitely not recommended, 118 00:04:54,210 --> 00:04:55,510 up to 12 hours. 119 00:04:55,510 --> 00:04:58,190 I think 30 seconds is fine, but again, remember, 120 00:04:58,190 --> 00:05:02,240 if a consumer needs more time to process a message, 121 00:05:02,240 --> 00:05:05,330 you should just call the ChangeMessageVisibility API 122 00:05:05,330 --> 00:05:07,720 to edit the visibility of that one message 123 00:05:07,720 --> 00:05:08,650 and increase the value 124 00:05:08,650 --> 00:05:11,670 so that another consumer will not see that message 125 00:05:11,670 --> 00:05:13,420 and the first consumer will get enough time 126 00:05:13,420 --> 00:05:15,330 to process that message accordingly. 127 00:05:15,330 --> 00:05:16,163 So, that's it. 128 00:05:16,163 --> 00:05:18,870 I hope you liked it, and I will see you in the next lecture.