1 00:00:00,000 --> 00:00:01,130 ‫So now let's talk about 2 00:00:01,130 --> 00:00:03,850 ‫how we can enable caching of our responses 3 00:00:03,850 --> 00:00:05,400 ‫in the API Gateway. 4 00:00:05,400 --> 00:00:06,233 ‫So what is caching? 5 00:00:06,233 --> 00:00:07,066 ‫What we know it. 6 00:00:07,066 --> 00:00:08,860 ‫Caching is to reduce the number of calls 7 00:00:08,860 --> 00:00:10,490 ‫made into the back-end. 8 00:00:10,490 --> 00:00:11,860 ‫So that means that our clients 9 00:00:11,860 --> 00:00:13,680 ‫when they talk to the API Gateway, 10 00:00:13,680 --> 00:00:16,180 ‫the API Gateway will first check the cache 11 00:00:16,180 --> 00:00:17,990 ‫and if a result is already in cache, 12 00:00:17,990 --> 00:00:19,140 ‫return that result. 13 00:00:19,140 --> 00:00:21,040 ‫Otherwise if there's a cache miss, 14 00:00:21,040 --> 00:00:23,420 ‫then it will go and talk to the back-end 15 00:00:23,420 --> 00:00:24,710 ‫to get the response. 16 00:00:24,710 --> 00:00:27,900 ‫So the idea is that we really leverage a lot of the cache 17 00:00:27,900 --> 00:00:30,950 ‫to reduce the amount of pressure on our back-end. 18 00:00:30,950 --> 00:00:31,940 ‫So the default TTL 19 00:00:31,940 --> 00:00:34,190 ‫which is how long the results live in the cache 20 00:00:34,190 --> 00:00:35,930 ‫is 300 seconds. 21 00:00:35,930 --> 00:00:38,190 ‫So five minutes, but the min is zero. 22 00:00:38,190 --> 00:00:41,540 ‫That means no caching and the max is one hour. 23 00:00:41,540 --> 00:00:43,860 ‫The caches are defined at the stage level. 24 00:00:43,860 --> 00:00:46,410 ‫So you define one cache per stage 25 00:00:46,410 --> 00:00:48,210 ‫and it is possible for you to overwrite 26 00:00:48,210 --> 00:00:50,130 ‫the cache settings per method. 27 00:00:50,130 --> 00:00:51,967 ‫So if you want a method to not use a cache, 28 00:00:51,967 --> 00:00:54,710 ‫you can specify it as well at the method level. 29 00:00:54,710 --> 00:00:56,090 ‫The cache can be encrypted 30 00:00:56,090 --> 00:00:59,570 ‫and the cache size is between 0.5 gigabytes 31 00:00:59,570 --> 00:01:02,840 ‫all the way to 237 gigabytes. 32 00:01:02,840 --> 00:01:04,430 ‫The cache is really expensive 33 00:01:04,430 --> 00:01:06,910 ‫so it makes sense only if you're in production. 34 00:01:06,910 --> 00:01:09,410 ‫They may not make sense if you're in development 35 00:01:09,410 --> 00:01:10,760 ‫or test environment, for example, 36 00:01:10,760 --> 00:01:12,960 ‫because there's not that many requests done 37 00:01:12,960 --> 00:01:14,600 ‫on your back-end anyway. 38 00:01:14,600 --> 00:01:15,920 ‫So caching is really something 39 00:01:15,920 --> 00:01:18,780 ‫you wanna use in production or in pre-production. 40 00:01:18,780 --> 00:01:20,770 ‫Now, how do we invalidate the cache? 41 00:01:20,770 --> 00:01:22,670 ‫So you are able to invalidate 42 00:01:22,670 --> 00:01:25,560 ‫the entire cache immediately from the UI. 43 00:01:25,560 --> 00:01:27,780 ‫Or clients can invalidate the cache 44 00:01:27,780 --> 00:01:31,060 ‫with the header placed into a query 45 00:01:31,060 --> 00:01:35,250 ‫into the API Gateway called Cache-Control max H equals zero. 46 00:01:35,250 --> 00:01:36,740 ‫And for the clients to do this, 47 00:01:36,740 --> 00:01:38,730 ‫they need proper IAM authorization. 48 00:01:38,730 --> 00:01:40,000 ‫So this is an IAM policy 49 00:01:40,000 --> 00:01:42,350 ‫that would allow a client to invalidate 50 00:01:42,350 --> 00:01:44,640 ‫the cache on a specific resource. 51 00:01:44,640 --> 00:01:47,860 ‫And if you don't impose an InvalidCache policy 52 00:01:47,860 --> 00:01:50,550 ‫or if you don't require authorization in the consult 53 00:01:50,550 --> 00:01:53,140 ‫then any clients can invalidate the API cache 54 00:01:53,140 --> 00:01:54,950 ‫and that could be a disaster. 55 00:01:54,950 --> 00:01:56,180 ‫So now you know everything about caching. 56 00:01:56,180 --> 00:01:58,850 ‫Let's go in the hands-off to see how that works. 57 00:01:58,850 --> 00:02:01,680 ‫So let's go into the prod stage and in here 58 00:02:01,680 --> 00:02:03,500 ‫I can enable the API cache. 59 00:02:03,500 --> 00:02:05,150 ‫Now, if you do enable caching 60 00:02:05,150 --> 00:02:06,730 ‫just know that it increases costs 61 00:02:06,730 --> 00:02:08,470 ‫and it's not covered by the free tier. 62 00:02:08,470 --> 00:02:10,110 ‫So this is not something you may wanna do 63 00:02:10,110 --> 00:02:13,750 ‫if you wanna remain with a very low cost API Gateway 64 00:02:13,750 --> 00:02:15,240 ‫and account billing. 65 00:02:15,240 --> 00:02:17,620 ‫So, cache capacity can be from anywhere 66 00:02:17,620 --> 00:02:20,780 ‫between 0.5 gigabyte all the way to 237. 67 00:02:20,780 --> 00:02:22,610 ‫I'm not sure why I don't see it in here 68 00:02:22,610 --> 00:02:25,640 ‫but it can go to all the way to 237 gigabytes. 69 00:02:25,640 --> 00:02:27,520 ‫You can encrypt the cache data. 70 00:02:27,520 --> 00:02:30,140 ‫And we can set up a default time-to-live in the cache. 71 00:02:30,140 --> 00:02:32,300 ‫For example 60 seconds. 72 00:02:32,300 --> 00:02:34,060 ‫Then we can do per cache invalidations. 73 00:02:34,060 --> 00:02:37,210 ‫So, do we allow the clients to invalidate the cache? 74 00:02:37,210 --> 00:02:39,820 ‫And if so, do we want to require authorization? 75 00:02:39,820 --> 00:02:41,670 ‫If not, then any client can invalidate 76 00:02:41,670 --> 00:02:43,500 ‫the cache which can be a big problem. 77 00:02:43,500 --> 00:02:45,700 ‫Or if you require authorization 78 00:02:45,700 --> 00:02:48,870 ‫then what to do if a request is unauthorized. 79 00:02:48,870 --> 00:02:50,280 ‫So I can say for now 80 00:02:50,280 --> 00:02:51,700 ‫you don't need to require authorization, 81 00:02:51,700 --> 00:02:52,640 ‫but it's up to you. 82 00:02:52,640 --> 00:02:53,750 ‫And so when this is done, 83 00:02:53,750 --> 00:02:55,820 ‫the cache is going to be created for you 84 00:02:55,820 --> 00:02:59,620 ‫and you would save the changes and that would be it. 85 00:02:59,620 --> 00:03:03,330 ‫So now the cache is enabled for my stage. 86 00:03:03,330 --> 00:03:04,570 ‫And so what I can do 87 00:03:04,570 --> 00:03:07,270 ‫is I can go and edit the cache setting 88 00:03:07,270 --> 00:03:09,450 ‫at each method level. 89 00:03:09,450 --> 00:03:11,320 ‫So for example, for the houses 90 00:03:11,320 --> 00:03:14,600 ‫if I didn't want to have caching for the houses method 91 00:03:14,600 --> 00:03:17,070 ‫then I could override this method 92 00:03:17,070 --> 00:03:18,890 ‫and in here I can scroll down 93 00:03:18,890 --> 00:03:21,190 ‫and for the cache setting I can, for example, 94 00:03:21,190 --> 00:03:22,970 ‫disable the cache method. 95 00:03:22,970 --> 00:03:26,150 ‫Or we could change the TTL or just choose to not encrypt 96 00:03:26,150 --> 00:03:28,300 ‫or choose to not require authorization. 97 00:03:28,300 --> 00:03:30,580 ‫So really it's up to you to wanna customize everything 98 00:03:30,580 --> 00:03:33,630 ‫but each method can be customized for the cache. 99 00:03:33,630 --> 00:03:35,010 ‫So that's very good. 100 00:03:35,010 --> 00:03:37,610 ‫Will say this and this is how we enable caching 101 00:03:37,610 --> 00:03:39,270 ‫on our API Gateway 102 00:03:39,270 --> 00:03:40,510 ‫and this is seamless for us 103 00:03:40,510 --> 00:03:45,350 ‫so if we go into the production and just open it. 104 00:03:45,350 --> 00:03:46,183 ‫As we can see right now, 105 00:03:46,183 --> 00:03:47,670 ‫we get Hello from Lambda v2. 106 00:03:47,670 --> 00:03:49,890 ‫If I refresh, it's going to get it from the cache. 107 00:03:49,890 --> 00:03:51,270 ‫And again, get it from the cache 108 00:03:51,270 --> 00:03:53,490 ‫and not invoke our Lambda function this time 109 00:03:53,490 --> 00:03:56,120 ‫because this result is cached thanks to the cache setting. 110 00:03:56,120 --> 00:03:57,410 ‫Now, I don't to incur any charges 111 00:03:57,410 --> 00:04:00,580 ‫so I'm going to disable the API cache and save my changes. 112 00:04:00,580 --> 00:04:01,413 ‫And here you go. 113 00:04:01,413 --> 00:04:03,380 ‫Very simple, but the API Gateway caching 114 00:04:03,380 --> 00:04:04,820 ‫is quite a powerful feature. 115 00:04:04,820 --> 00:04:05,653 ‫All right, that's it. 116 00:04:05,653 --> 00:04:07,320 ‫I will see you in the next lecture.