1 00:00:00,000 --> 00:00:01,110 ‫So let's take some time 2 00:00:01,110 --> 00:00:03,901 ‫to understand how caching works in CloudFront. 3 00:00:03,901 --> 00:00:05,135 ‫So the cache lives 4 00:00:05,135 --> 00:00:07,387 ‫at each CloudFront edge location. 5 00:00:07,387 --> 00:00:08,876 ‫So you'll have as many caches 6 00:00:08,876 --> 00:00:10,374 ‫as edge locations. 7 00:00:10,374 --> 00:00:12,504 ‫And each object in the cache 8 00:00:12,504 --> 00:00:13,891 ‫is going to be identified 9 00:00:13,891 --> 00:00:15,115 ‫by a Cache Key. 10 00:00:15,115 --> 00:00:16,740 ‫We'll see what the Cache Key is 11 00:00:16,740 --> 00:00:17,573 ‫in the next slide. 12 00:00:17,573 --> 00:00:19,651 ‫But the idea is that a request made 13 00:00:19,651 --> 00:00:22,150 ‫through CloudFront edge location. 14 00:00:22,150 --> 00:00:23,501 ‫The edge location is first going 15 00:00:23,501 --> 00:00:25,393 ‫to check whether or not the object 16 00:00:25,393 --> 00:00:26,505 ‫has been cached. 17 00:00:26,505 --> 00:00:27,930 ‫And if it's in the cache, 18 00:00:27,930 --> 00:00:28,888 ‫has it expired or not based 19 00:00:28,888 --> 00:00:30,513 ‫on the time to live. 20 00:00:30,513 --> 00:00:31,737 ‫And then if not, 21 00:00:31,737 --> 00:00:33,127 ‫if it's not in the cache, 22 00:00:33,127 --> 00:00:34,493 ‫then the request is forwarded 23 00:00:34,493 --> 00:00:36,524 ‫and we'll see how it's forwarded 24 00:00:36,524 --> 00:00:37,866 ‫to your origin. 25 00:00:37,866 --> 00:00:40,016 ‫And then the response from the origin 26 00:00:40,016 --> 00:00:41,730 ‫is cached into your edge location. 27 00:00:41,730 --> 00:00:45,259 ‫So that future request return a cache result. 28 00:00:45,259 --> 00:00:47,874 ‫So you want to maximize the Cache Hit ratio 29 00:00:47,874 --> 00:00:49,500 ‫by minimizing request to the origin. 30 00:00:49,500 --> 00:00:51,142 ‫That means that you want to cache 31 00:00:51,142 --> 00:00:52,244 ‫as much stuff as possible 32 00:00:52,244 --> 00:00:54,250 ‫in your edge locations. 33 00:00:54,250 --> 00:00:55,391 ‫We'll see as well that it's possible 34 00:00:55,391 --> 00:00:57,002 ‫for you to not wait 35 00:00:57,002 --> 00:00:59,749 ‫until the item expires based on a TTL. 36 00:00:59,749 --> 00:01:00,745 ‫If you wanna remove it from the cache, 37 00:01:00,745 --> 00:01:03,255 ‫you can create an invalidation. 38 00:01:03,255 --> 00:01:04,761 ‫So what is a CloudFront Cache Key 39 00:01:04,761 --> 00:01:06,235 ‫and what do we cache on? 40 00:01:06,235 --> 00:01:07,872 ‫Well, a Cache Key is a unique identifier 41 00:01:07,872 --> 00:01:09,783 ‫for each object in the cache. 42 00:01:09,783 --> 00:01:11,986 ‫And by default, if you don't do anything, 43 00:01:11,986 --> 00:01:13,394 ‫it's made up of the host name 44 00:01:13,394 --> 00:01:17,130 ‫and the resource portion of URL. 45 00:01:17,130 --> 00:01:17,963 ‫So in this example, 46 00:01:17,963 --> 00:01:20,010 ‫there is mywebsite.com. 47 00:01:20,010 --> 00:01:20,873 ‫So that's the host name. 48 00:01:20,873 --> 00:01:21,706 ‫And then there is the 49 00:01:21,706 --> 00:01:26,670 ‫GET/content/stories/example-story.html. 50 00:01:26,670 --> 00:01:29,011 ‫That is the resource portion of the URL. 51 00:01:29,011 --> 00:01:29,844 ‫That means that anyone 52 00:01:29,844 --> 00:01:31,739 ‫that makes a similar request 53 00:01:31,739 --> 00:01:33,500 ‫in case there is a cache miss, 54 00:01:33,500 --> 00:01:34,400 ‫well, first, we're going 55 00:01:34,400 --> 00:01:36,631 ‫to get the object from the origin 56 00:01:36,631 --> 00:01:37,464 ‫but then the object 57 00:01:37,464 --> 00:01:38,617 ‫is going to be cached 58 00:01:38,617 --> 00:01:40,503 ‫based on these two keys right here, 59 00:01:40,503 --> 00:01:42,255 ‫the host name 60 00:01:42,255 --> 00:01:44,745 ‫and the resource portion of the URL. 61 00:01:44,745 --> 00:01:47,280 ‫And then a request that looks similar 62 00:01:47,280 --> 00:01:48,113 ‫with the same host name 63 00:01:48,113 --> 00:01:49,742 ‫and the same resource portion 64 00:01:49,742 --> 00:01:50,874 ‫will hit the cache 65 00:01:50,874 --> 00:01:52,640 ‫and will get a cache hit. 66 00:01:52,640 --> 00:01:54,001 ‫But sometimes you want your Cache Key 67 00:01:54,001 --> 00:01:55,133 ‫to be a bit more complicated 68 00:01:55,133 --> 00:01:56,648 ‫because sometimes, well, 69 00:01:56,648 --> 00:01:59,280 ‫you have content that will vary 70 00:01:59,280 --> 00:02:00,113 ‫based on the user 71 00:02:00,113 --> 00:02:02,940 ‫or the device or the language 72 00:02:02,940 --> 00:02:03,875 ‫or the location the user 73 00:02:03,875 --> 00:02:05,407 ‫is from and so on. 74 00:02:05,407 --> 00:02:06,240 ‫And so we want to do 75 00:02:06,240 --> 00:02:08,264 ‫is to enhance the Cache Key 76 00:02:08,264 --> 00:02:10,004 ‫and add more information. 77 00:02:10,004 --> 00:02:12,492 ‫So we wanna add maybe HTTP headers 78 00:02:12,492 --> 00:02:14,997 ‫or cookies or query strings in it. 79 00:02:14,997 --> 00:02:16,382 ‫And to define how to create 80 00:02:16,382 --> 00:02:18,240 ‫that Cache Key, we have to define 81 00:02:18,240 --> 00:02:22,020 ‫what's called a CloudFront cache policy. 82 00:02:22,020 --> 00:02:23,752 ‫So this cache policy can be caching 83 00:02:23,752 --> 00:02:26,160 ‫based on the HTTP headers. 84 00:02:26,160 --> 00:02:27,245 ‫So you can select none of them 85 00:02:27,245 --> 00:02:28,486 ‫or a whitelist to say 86 00:02:28,486 --> 00:02:30,125 ‫which one you want to include. 87 00:02:30,125 --> 00:02:31,650 ‫You have the cookies. 88 00:02:31,650 --> 00:02:32,989 ‫So you can select none of them, 89 00:02:32,989 --> 00:02:34,480 ‫a whitelist or list you wanna include 90 00:02:34,480 --> 00:02:37,007 ‫or all or all accept. 91 00:02:37,007 --> 00:02:38,249 ‫And we have the query strings. 92 00:02:38,249 --> 00:02:39,880 ‫Again, similarly, do you want 93 00:02:39,880 --> 00:02:41,923 ‫to include none of them, 94 00:02:41,923 --> 00:02:44,393 ‫a whitelist, all except, or all? 95 00:02:44,393 --> 00:02:45,626 ‫And this is basically going 96 00:02:45,626 --> 00:02:47,236 ‫to configure how your Cache Key 97 00:02:47,236 --> 00:02:48,856 ‫is going to be created. 98 00:02:48,856 --> 00:02:49,689 ‫In the cache policy, 99 00:02:49,689 --> 00:02:51,128 ‫you can also control the TTL. 100 00:02:51,128 --> 00:02:52,755 ‫So from zero seconds up 101 00:02:52,755 --> 00:02:54,501 ‫to one year in the cache. 102 00:02:54,501 --> 00:02:55,875 ‫And then you can also control 103 00:02:55,875 --> 00:02:57,498 ‫that setting using specific headers 104 00:02:57,498 --> 00:02:59,520 ‫called the cache control header 105 00:02:59,520 --> 00:03:00,738 ‫or the expires header. 106 00:03:00,738 --> 00:03:03,398 ‫So you can create your own cache policies 107 00:03:03,398 --> 00:03:04,624 ‫or you can use predefined 108 00:03:04,624 --> 00:03:06,887 ‫managed policies by AWS. 109 00:03:06,887 --> 00:03:08,010 ‫And the very important thing 110 00:03:08,010 --> 00:03:09,510 ‫I want you to remember 111 00:03:09,510 --> 00:03:11,249 ‫is that all the HTTP headers, 112 00:03:11,249 --> 00:03:12,493 ‫cookies and query strings 113 00:03:12,493 --> 00:03:14,631 ‫that you include in the Cache Key 114 00:03:14,631 --> 00:03:16,864 ‫will be automatically included 115 00:03:16,864 --> 00:03:20,430 ‫and forwarded to your origin request. 116 00:03:20,430 --> 00:03:21,263 ‫So in the case of HTTP header 117 00:03:21,263 --> 00:03:22,638 ‫to give you an example, 118 00:03:22,638 --> 00:03:23,988 ‫let's say we have this request 119 00:03:23,988 --> 00:03:25,988 ‫and we have a language fr-fr, 120 00:03:25,988 --> 00:03:27,992 ‫meaning that we request the blog 121 00:03:27,992 --> 00:03:28,975 ‫in the French language. 122 00:03:28,975 --> 00:03:29,881 ‫How does that work? 123 00:03:29,881 --> 00:03:31,235 ‫Well, if we define a none 124 00:03:31,235 --> 00:03:32,504 ‫HTTP header cache policy, 125 00:03:32,504 --> 00:03:34,251 ‫then none of the headers 126 00:03:34,251 --> 00:03:36,146 ‫are going to be cached 127 00:03:36,146 --> 00:03:38,142 ‫and the headers will not be forwarded 128 00:03:38,142 --> 00:03:40,133 ‫unless and I will show you what happens 129 00:03:40,133 --> 00:03:41,749 ‫in the rest of this lecture. 130 00:03:41,749 --> 00:03:42,997 ‫So by default, headers 131 00:03:42,997 --> 00:03:44,639 ‫will not be forwarded. 132 00:03:44,639 --> 00:03:45,514 ‫But this gives you 133 00:03:45,514 --> 00:03:46,494 ‫the best caching performance 134 00:03:46,494 --> 00:03:48,749 ‫because you don't have any headers in it. 135 00:03:48,749 --> 00:03:50,484 ‫If you want to whitelist specific headers 136 00:03:50,484 --> 00:03:51,912 ‫and that may mean necessary 137 00:03:51,912 --> 00:03:52,745 ‫because while you want 138 00:03:52,745 --> 00:03:55,495 ‫to have the language as a Cache Key, 139 00:03:55,495 --> 00:03:57,248 ‫then you specify which headers you want 140 00:03:57,248 --> 00:03:58,252 ‫to include in the Cache Key, 141 00:03:58,252 --> 00:04:00,113 ‫for example, the language header. 142 00:04:00,113 --> 00:04:02,122 ‫And then this header, the language 143 00:04:02,122 --> 00:04:04,883 ‫will also be forwarded to your origin 144 00:04:04,883 --> 00:04:06,090 ‫so that the origin 145 00:04:06,090 --> 00:04:07,388 ‫can actually respond to the request 146 00:04:07,388 --> 00:04:08,388 ‫and give you the blog 147 00:04:08,388 --> 00:04:10,765 ‫in the correct language. 148 00:04:10,765 --> 00:04:12,244 ‫So this is a very similar mechanism 149 00:04:12,244 --> 00:04:13,680 ‫for query strings. 150 00:04:13,680 --> 00:04:15,240 ‫So query strings are what happens 151 00:04:15,240 --> 00:04:16,985 ‫in the URL after a question mark. 152 00:04:16,985 --> 00:04:18,999 ‫So for example, border equals red 153 00:04:18,999 --> 00:04:20,259 ‫and size equals large. 154 00:04:20,259 --> 00:04:21,994 ‫So here, we want a cat image. 155 00:04:21,994 --> 00:04:23,504 ‫But apparently, it's going to be customized 156 00:04:23,504 --> 00:04:25,250 ‫a little bit by the origin. 157 00:04:25,250 --> 00:04:26,642 ‫So again, if you have none, 158 00:04:26,642 --> 00:04:28,008 ‫then none of the query strings 159 00:04:28,008 --> 00:04:29,543 ‫are going to be used for the Cache Key 160 00:04:29,543 --> 00:04:30,376 ‫and they're not going 161 00:04:30,376 --> 00:04:32,751 ‫to be forwarded to your origin. 162 00:04:32,751 --> 00:04:33,750 ‫Whitelist, you specify 163 00:04:33,750 --> 00:04:36,739 ‫which query strings you want included. 164 00:04:36,739 --> 00:04:38,255 ‫Include all-except, you specify 165 00:04:38,255 --> 00:04:39,754 ‫which ones you don't want 166 00:04:39,754 --> 00:04:41,499 ‫but the rest passes 167 00:04:41,499 --> 00:04:44,125 ‫and all is including all the query strings 168 00:04:44,125 --> 00:04:45,383 ‫in the Cache Keys 169 00:04:45,383 --> 00:04:46,680 ‫and all the query strings 170 00:04:46,680 --> 00:04:47,513 ‫are going to be forwarded. 171 00:04:47,513 --> 00:04:48,747 ‫But of course, if you have many, 172 00:04:48,747 --> 00:04:51,540 ‫this gives you the worst caching performance. 173 00:04:51,540 --> 00:04:52,373 ‫So we've seen that 174 00:04:52,373 --> 00:04:54,392 ‫when we define a cache policy, 175 00:04:54,392 --> 00:04:56,249 ‫we can have query strings, 176 00:04:56,249 --> 00:04:58,644 ‫cookies and headers as whitelist. 177 00:04:58,644 --> 00:05:00,495 ‫For example, to choose (indistinct) want 178 00:05:00,495 --> 00:05:03,240 ‫and they will be forwarded to the origin. 179 00:05:03,240 --> 00:05:04,073 ‫But what if you want 180 00:05:04,073 --> 00:05:05,643 ‫to include some stuff 181 00:05:05,643 --> 00:05:07,886 ‫in the origin request, okay? 182 00:05:07,886 --> 00:05:09,391 ‫But you don't want to include them 183 00:05:09,391 --> 00:05:10,761 ‫in the Cache Key. 184 00:05:10,761 --> 00:05:11,747 ‫In that case, you can define 185 00:05:11,747 --> 00:05:14,512 ‫what's called an origin request policy. 186 00:05:14,512 --> 00:05:15,753 ‫So the idea is that 187 00:05:15,753 --> 00:05:18,140 ‫you can include extra HTTP headers 188 00:05:18,140 --> 00:05:20,398 ‫or cookies or query strings 189 00:05:20,398 --> 00:05:22,495 ‫but they will be forwarded to the origin 190 00:05:22,495 --> 00:05:23,621 ‫but they're not going to be used 191 00:05:23,621 --> 00:05:25,015 ‫in the Cache Key. 192 00:05:25,015 --> 00:05:26,372 ‫So you can also, 193 00:05:26,372 --> 00:05:28,492 ‫as part of the origin request policy, 194 00:05:28,492 --> 00:05:30,359 ‫add custom HTTP headers 195 00:05:30,359 --> 00:05:32,749 ‫or CloudFront HTTP headers to the origin 196 00:05:32,749 --> 00:05:34,385 ‫even though they were not present 197 00:05:34,385 --> 00:05:35,894 ‫in the viewer request. 198 00:05:35,894 --> 00:05:36,727 ‫For example, if you wanted 199 00:05:36,727 --> 00:05:39,659 ‫to pass an API key or a secret header. 200 00:05:39,659 --> 00:05:40,879 ‫So you can create your own policy 201 00:05:40,879 --> 00:05:43,860 ‫or you can use predefined managed policies. 202 00:05:43,860 --> 00:05:44,730 ‫And so at this point, 203 00:05:44,730 --> 00:05:45,563 ‫you may be confused like, 204 00:05:45,563 --> 00:05:46,396 ‫what is really the difference 205 00:05:46,396 --> 00:05:47,652 ‫between Cache Keys 206 00:05:47,652 --> 00:05:49,510 ‫and origin request policy? 207 00:05:49,510 --> 00:05:50,501 ‫Well, let me try to summarize 208 00:05:50,501 --> 00:05:52,009 ‫as best as I can. 209 00:05:52,009 --> 00:05:54,381 ‫So the request will come 210 00:05:54,381 --> 00:05:56,006 ‫with some query strings, 211 00:05:56,006 --> 00:05:58,267 ‫some cookies, some headers, 212 00:05:58,267 --> 00:06:00,245 ‫and then we will cache based 213 00:06:00,245 --> 00:06:02,006 ‫on the cache policy. 214 00:06:02,006 --> 00:06:03,371 ‫For example, we want to cache here 215 00:06:03,371 --> 00:06:05,644 ‫on the host name, the resource, 216 00:06:05,644 --> 00:06:08,392 ‫and a header called authorization. 217 00:06:08,392 --> 00:06:10,732 ‫But then, your origin may need more 218 00:06:10,732 --> 00:06:12,863 ‫than these three things to actually work 219 00:06:12,863 --> 00:06:15,491 ‫and to actually serve properly the request. 220 00:06:15,491 --> 00:06:18,510 ‫So you may want to add the user agents, 221 00:06:18,510 --> 00:06:21,993 ‫the session ID and the ref query string 222 00:06:21,993 --> 00:06:24,662 ‫as part of your request to the origin. 223 00:06:24,662 --> 00:06:25,738 ‫So in this case, 224 00:06:25,738 --> 00:06:26,641 ‫the request to the origin 225 00:06:26,641 --> 00:06:28,395 ‫is going to be enhanced 226 00:06:28,395 --> 00:06:30,650 ‫but then the caching will not happen 227 00:06:30,650 --> 00:06:32,247 ‫based on what we forwarded 228 00:06:32,247 --> 00:06:34,050 ‫to the origin request policy. 229 00:06:34,050 --> 00:06:34,889 ‫It's only going to be based 230 00:06:34,889 --> 00:06:36,244 ‫on the cache policy. 231 00:06:36,244 --> 00:06:37,258 ‫So I hope you see the synergy 232 00:06:37,258 --> 00:06:38,491 ‫between these two 233 00:06:38,491 --> 00:06:39,751 ‫and that should be enough 234 00:06:39,751 --> 00:06:41,758 ‫to answer questions at the exam. 235 00:06:41,758 --> 00:06:43,020 ‫All right, that's it. 236 00:06:43,020 --> 00:06:44,160 ‫I hope you liked it 237 00:06:44,160 --> 00:06:46,203 ‫and I will see you in the next lecture.