Python Web Apps that Fly with CDNs Transcripts
Chapter: Avoiding Stale Caches
Lecture: The caching problem

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's see this problem I described in action, at least conceptually. So the caching problem is we want our websites to be as fast as possible.
0:13 And the way we've achieved that is taking these hundreds of requests and telling the browser, you keep that, and if you've ever seen it before,
0:23 you just don't even go back to the CDN, you just show it to the user. And we've told the CDN, hey, you don't come back to us for this information.
0:31 You keep it for months. And that's awesome for speed. However, you can see down here in our network tools,
0:41 that this is all just coming out of memory cache. So when something changes on the server, it doesn't even go back and ask,
0:48 hey, should I get a new version of this or no? This is the fastest way to get the web app to run. However, it absolutely leads to that staleness,
0:59 that cache staleness problem that I talked about. And you're probably familiar with this if you've done a lot of web development.
1:07 This happens on regular web servers as well where you serve up static files and you tell the clients to cache them for the same reason.
1:14 But it's a little bit worse on CDNs 'cause there's a multi-level problem. So let's suppose we've got this web user and we've got our web server
1:24 and this yellow dot up there, those three yellow dots are three copies of the same file. This is, let's say, version one of our CSS file.
1:36 Now, when somebody comes to visit our website, you can see they make a request and then they save that file in their local browser cache
1:43 as we ask them to because that's the fastest. Now, we've got a grand new idea for updating our website.
1:51 We make some changes and you can see right there, And now we have a green version, that's the new version of this file that we want them to share.
2:00 Now, if this web user comes back and makes another request, what are they going to get?
2:06 They're going to get the stale file, the yellow one, because that's the one in the CDN.
2:10 Moreover, they're probably not even going to get that, right, as we saw.
2:14 But if for some reason they're like, "Oh, I think it might be a cache problem," and
2:17 do a complete refresh, they dump their cache or they do a hard reload and they completely
2:24 go back to the what they consider to be the source of truth, the CDN where we sent them
2:29 to, it's still going to give them the yellow file because we also told the CDN not to come back to us.
2:35 So we've got this multi tier problem, how do we get the web user and their browser to realize oh, stop caching that use something else?
2:43 as well as how do we make the CDN understand the same thing.S


Talk Python's Mastodon Michael Kennedy's Mastodon