over 2 years ago Syntax Podcast
You Need Stale While Revalidate
- 00:31 Introducing stale while revalidate caching technique
- 01:05 Centux Replay for caching error handling
- 01:47 Explanation of stale while revalidate caching header
- 02:07 Caching HTML pages to avoid database queries
- 02:24 Caching Syntax open graph images to speed up generation
- 02:28 Open graph images take long time to generate
- 03:11 Why you might want to cache something
- 03:26 Avoiding expensive operations with caching
- 04:08 Caching used in many areas of computer science
- 04:20 Avoiding expensive operations with caching
- 04:55 Caching images to avoid re-downloading
- 05:11 Putting caches closer to users with CDNs
- 05:26 Local browser caches assets
- 05:40 CDNs cache assets globally
- 06:13 Server can respect cache headers
- 07:07 Caching to limit API hits
- 07:40 Caching expensive image generation
- 08:24 Serving from cache avoids expense of regeneration
- 09:02 Max age sets cache lifetime in seconds
- 10:06 Don't cache sensitive user-specific data
- 10:22 S-maxage caches on CDN, maxage on browser
- 11:52 Max age sets total cache lifetime
- 12:05 Stale while revalidate sets stale cache lifetime
- 12:16 Stale value tells how long to serve stale asset
- 13:33 Stale while revalidate serves stale but refreshes
- 14:41 Stale while revalidate avoids waiting for background tasks
- 15:11 Not for content that must always be fresh
- 16:16 Accuracy important for stale cache suitability
- 18:35 Why not use stale while revalidate everywhere?
- 18:47 Caching adds complexity
- 19:22 Need traffic to enable stale revalidations
- 20:08 Some things don't need caching
- 20:41 CDN propagation delays limit speed
- 21:18 Show wrap-up
Don't cache sensitive user-specific data
Wes Bos
s Dash is a weird rendering or a download of hitting an API that is really slow. So all of those things can happen
Wes Bos
Like, super or something.
Avoiding expensive operations with caching
Scott Tolinski
Another one is you can Sorry. Yeah. Sorry. Let me even just hop in there too to make things a little bit clearer for people who don't have any experience with caching. Think about images. Right? You have a a big old image.
Scott Tolinski
And if you download that image once,
Caching adds complexity
Scott Tolinski
I mean, they're pretty simple.
Wes Bos
Imessage, Like it's got to take longer than that to propagate. If you've got a CDN network, Page that takes 800 milliseconds
Wes Bos
a use case where one second is not long enough
Wes Bos
back and forth. One second could be for the newest shows, right? Because there was no like cron that would automatically populate it. So what was happening? But we didn't want every time you visited the website, we didn't want the person to say, You could cache
Wes Bos
The caching doesn't really matter if it's rendering fast enough and we need to cache them so that Once you take the 15 seconds to generate it, they're going to be loaded instantly after that. However, Stuff? Stuff.
Wes Bos
you know, or you're doing like a Zendesk They're logged in state to the next user that comes because you said this is a shareable cache. So you kind of have to be careful with that because you have to think, okay, would make a lot of sense.
Wes Bos
or sorry, is too long like examples.
Wes Bos
There's there's stuff. Yeah. Yeah. It's true. May maybe there is, like, You're paying the bill to generate all of those. So it often makes sense to just generate it once
Wes Bos
kind of annoying to get that type of thing. And in that case, If you already have it downloaded into your browser
Stale value tells how long to serve stale asset
Wes Bos
for up to 10 minutes past that,
Wes Bos
So the browser goes back to the server and says, Hey, I need a new one. This is a stale It's okay to serve the stale one up, right? This is stale bread. You can still eat it for the next day or so, but we should probably make some more, you're on the 4th 2nd.
Wes Bos
this is good for a minute, Say still while revalidate
Wes Bos
Right? So stale while revalidate will just that if you get 100,000 people a second, that's 300,000
Wes Bos
but after 1 minute, that is that asset is considered stale.
Wes Bos
So in the example of the Syntax Open Graph images, Oh, but then I have the dev tools disabled cache on There We Go. Yes, it does. So
Wes Bos
Stale while revalidate will do is you can say, Okay, And you can tell that pretty quickly by I guess we could go to the the syntax website or go to the syntax. Fm/og
Wes Bos
say, Oh, oh, shoot. Okay, doesn't need to happen all the time.
Wes Bos
10 seconds to generate the image,
Not for content that must always be fresh
Scott Tolinski
always needs to be there. And in that case, yeah, you probably
Scott Tolinski
Yeah. Or just somebody. Right? on everything?
Scott Tolinski
Scores TO Users? Yes.
Scott Tolinski
And you load the site. You want the scores
Scott Tolinski
If you were to be serving up
Scott Tolinski
you know, you're you're gonna be Finding alternative caching methods or caching for much less time. Yeah. Maybe even, like, on the seconds. I I've I heard that, you know, you probably don't wanna use this Old stale data.
Scott Tolinski
You're going to probably not want to serve stale content to those people.
Scott Tolinski
served. Like, if you go to, like, espn.com
Scott Tolinski
Then at that point,
Scott Tolinski
Content that's updating really quickly, like like sports scores that need to be
Scott Tolinski
to I think the reason why that metaphor is perfect because it also
Scott Tolinski
up to date all the time, so you're probably not gonna wanna serve stale doesn't really change that much. You know? You can think about it. If you have a CMS, Caching
Scott Tolinski
Somebody of highest importance. The the bread must be at its very best. This bread must be perfect.
Scott Tolinski
The fresh content
Scott Tolinski
stale bread Oh, yeah. S dash is better because it's on the CDN. In in that regard, if you wanted something cached for both of those, Would you use both of those headers, Wes, or just 1 or the other? That's a good question, actually.
Scott Tolinski
So there are instances where
Max age sets cache lifetime in seconds
Wes Bos
your eBay, you're logged in and you see all of your account details.
Wes Bos
whereas SMAS Max Age, the S stands for shared, And in that case, you also need in that generation, meaning that You're getting one second old content, which might be okay, That's not uncommon. Right. Lots of people watching hockey. Lots of people refreshing wanting to know the score which you visit a website and then you go back to that website later.
Wes Bos
If you accidentally s Max age somebody is like logged in state, that when visited.
Scott Tolinski
Basically, it's how many seconds you want this thing to be cached for before,
Scott Tolinski
But, like,
Wes Bos
and that will tell
Wes Bos
to share this between multiple people. So one possible I've seen this a couple of times security issue is
Wes Bos
Or the CDN goes off and gets a new version of it. And the difference there between the the 2 that Scott just said is max age is going to be cached on your browser, the fresh version is worth waiting for.
Wes Bos
like, let's say it's itself can respect those cache headers.
Scott Tolinski
the browser
Avoiding expensive operations with caching
Scott Tolinski
and it can just serve up
Scott Tolinski
data
Scott Tolinski
oftentimes
Scott Tolinski
You update some data. Right? It can cause some problems. You might need to know what's going on. But more importantly,
Scott Tolinski
If you think about caching as a whole, just a concept, what you're doing is you're basically saving that information
Scott Tolinski
And you're serving that same data to a lot of people.
Scott Tolinski
The already saved data typically could be saved in memory, could be saved in your browser's cache.
Scott Tolinski
So that way when someone else requests it, it doesn't have to do the expensive operations,
Scott Tolinski
And whether it is caching your your server response or your data.
Need traffic to enable stale revalidations
Wes Bos
So, yeah, it's not something that I would just I don't know. Like, Should you throw it on anything? Everything
Scott Tolinski
sriracha at this point or something or whatever people just like to put on everything.
Wes Bos
It's still the difference between a couple 100 milliseconds and 0
Scott Tolinski
for me? I really feel like it's to be exactly
Wes Bos
enough traffic to kick off those next renders If you don't have enough traffic in the example of the syntax website that I gave you, and then
Wes Bos
I'm sure that
Wes Bos
a couple of milliseconds or 100 milliseconds or whatever to generate the site. It wasn't really that long, but
Show wrap-up
Scott Tolinski
I think we had to get our food metaphors in. Exactly. We got it.
Scott Tolinski
Head on over to syntax.fm
Wes Bos
464.
Wes Bos
Stuff. So listen to that, but I thought we we should go entirely in-depth Then you're just sitting there waiting for the site to generate. And it only took within this many minutes.
Wes Bos
So go to syntax. Fm/464.
Wes Bos
You can listen to the episode Cash control headers explain.
Wes Bos
We go through all of them.
Wes Bos
the different caching headers, which is episode
Scott Tolinski
or drop a review if you like this show.
Wes Bos
the next 10 minutes? Max age? that data to be cached for too long for. Yeah, right. For absolutely everybody. Right. If it's the same database query or even if you if you only cache it for 1 minute, you know, that's you can do that.
Wes Bos
the old version for
Scott Tolinski
And don't forget to subscribe in your podcast player If you're, you know, caching anything, you gotta make sure you have some error and exception handling traffic on your your services
Scott Tolinski
for a full archive of all of our shows,
Transcript
Announcer
Tolinski.
Scott Tolinski
We're gonna be serving you up some stale,
Announcer
Monday. Monday. Monday. Open wide dev fans. Get ready to stuff your face with JavaScript, CSS, node modules, barbecue tips, get workflows, breakdancing,
Scott Tolinski
Welcome to Syntex. On this Monday, hasty treat.
Announcer
Boss, and Scott, El Toro Loco,
Announcer
soft skill, web development, the hastiest, the craziest, the tastiest web development treats. Coming in hot. Here is Wes, Barracuda,
CDNs cache assets globally
Wes Bos
And then that's gonna load fast for people
Wes Bos
CDN eyes a lot of your assets So
Wes Bos
Let me throw a copy in Boston, I would think I asked Chat GPT, it says yes.
Wes Bos
And they'll say, all right, well, you generated this Open Graph image. Let me go throw a copy in Australia.
Scott Tolinski
you had said a cache can be in 2 places on the browser or a CDN. I just wanna make sure that that's clear. They can all stop by yeah. I'm talking about, like, the actual cache header. So you're right. Your server
Scott Tolinski
Stuff. Just, Yeah. I know. I well, that's my biggest question is is, if you have something like centuries
Scott Tolinski
all around the world. Yeah. Well, they can also be a cache can also be on that just a straight up server too.
CDN propagation delays limit speed
Scott Tolinski
I guess, why would I just not use this all the time? Because it feels like I want to. Yeah.
Wes Bos
If you were And then it'll kick off a background task that nobody is sitting around waiting for, and it will regenerate a new version. And then you come back
Wes Bos
to cache that data Interesting. For absolutely every but you could Just cash it on the on the user's client as well. Word. Cool. Yep. That's it. I thought that's I told Scott this would be a 10 minute episode, but here we are 22 minutes in.
Wes Bos
if you had, like, a, like, a Twitter or a YouTube where you have, like, the header and If you if they cache that on a CDN,
Caching images to avoid re-downloading
Scott Tolinski
that takes some time.
Scott Tolinski
And if you cache it, It stores that image on your computer. So the next time you click refresh on that web page, it's not having to go off and repull it down. It's just there. It already exists.
Accuracy important for stale cache suitability
Wes Bos
Now people are getting stale content, we needed a way to regenerate the website just for stale while we're validated. Yeah. Absolutely. I love it. Alright. Peace.
Wes Bos
Sometimes But generally what you're doing is, yeah, you're sticking a CDN in front of it. Yeah. Sorry. I was also speaking on a cache broadly rather than. Oh, yeah. Yeah. I'm more thinking about like like a cache header here where if it is cached.
Wes Bos
to be cashed, and they kick off whole thing where you invalidate your cache. So those are the 2 examples we'll refer to this, Next person comes along. They get your
Wes Bos
the scores for 3 seconds And that's a security issue there. So certainly be careful. Don't just think, oh, to do database query.
Wes Bos
We had a one second stale while we validate on it. Why? Because Another reason for a cache is you might limit how often you're allowed to hit an API. So if you have between 8 and 15 seconds, depending on if the Headless browser is hot or not. Basically, I Now enter stale while revalidate, right? forward slash 600.
Wes Bos
But it's only 4 seconds old, and there's been a background thing kicked off to regenerate but Another use case for caching is you can put the caches closer to your users. So, If you want that to be cached for some amount of time, the one that you you generated
Wes Bos
a generation for the next person then stuff? It's either I don't even wonder. I think probably like your ISP might even And the idea with this is it tells the It really has to do with like how much traffic you're getting with the syntax website, the old syntax website.
Wes Bos
the latest version of it. So you're regenerating a new version And you are never going to be sitting around, sitting on your hands waiting for in the background is quite a bit.
Wes Bos
Correct.
Wes Bos
and then.
Wes Bos
And just in case there's a new episode. So what we did is
Wes Bos
in one second. Right.
Wes Bos
A 100,000 traffic visitors cached both by your browser as well as,
Wes Bos
and the next person. So you're always kind of off by 1
Introducing stale while revalidate caching technique
Scott Tolinski
requires information to be up to date. My name is Scott Talinski. I'm a developer from Denver. With me, as always, is Wes Bos.
Scott Tolinski
which is a caching technique that
Scott Tolinski
to really improve
Scott Tolinski
overall speed
Scott Tolinski
And really make life easier on you when you're trying to get some caching down that, well, still and use the coupon code tasty treat, all lowercase and all one word. You'll get 2 months for free. All right, so let's talk about the stale while revalidate.
Scott Tolinski
takes and then revalidating them. So we're gonna be talking about stale while revalidate,
Scott Tolinski
is really pretty simple to add to your projects replay where you can see
S-maxage caches on CDN, maxage on browser
Wes Bos
is it will both be respected by the CDN as well as the client. But let's pause a second, double check that.
Wes Bos
I'll tell you this. I've never set both of them on a single response, and I've done this a lot. So I would assume so The Max, Or you might be doing something expensive, such as
Wes Bos
that it would be it has, like, notifications in it, Like you probably don't want to cache that because now you're spending CDN space We put a one second still while we validate on it, meaning that you visit the website, you get the cached version, but you immediately kick off a regen for the next person.
Wes Bos
that requests that are being served up as cached. And then once you get over that 3 seconds,
Wes Bos
S Max Age will But tweet us at Syndax FM if you have This is a header which you're probably thinking I don't need a podcast about headers. This is such a cool header. You're going to want to listen to this one, and you don't want to have to be dealing with with caches? let's say every 3 seconds your CDN. It is okay is that
Wes Bos
So again, if you're.
Wes Bos
Which makes sense because you're you're not spending the bandwidth to get to the CDN S Max age If I'm saving a copy of this user's rendered out, Akash can live in 2 spots. It can live on your computer,
Wes Bos
In your network tab, Second 1 is the open graph images on the Syntax website.
Wes Bos
cache it both at a CDN level as well as at a browser level.
Stale while revalidate avoids waiting for background tasks
Scott Tolinski
the use case Yeah. That way expensive operations don't get run more than they need to. Exactly. Like, you you might have a the queen
Wes Bos
But if I'm serving up a stale version within this many minutes, I'll go make a new one so the so we can have fresh.
Scott Tolinski
But, you know, you gotta feel like it it has some degree of accuracy there because
Scott Tolinski
in which stale while revalidate
Scott Tolinski
probably isn't a good idea.
Caching expensive image generation
Wes Bos
kept the browser around so that if you come your assets or you use something like Cloudflare or Vercel.
Wes Bos
it takes about
Wes Bos
If you try to generate another one, like 10 seconds later, the browser's already started up and that that'll cut 5, 6 seconds off of everything.
Wes Bos
Every time we generate an Open Graph image, see
Wes Bos
generating images. So
Caching to limit API hits
Wes Bos
and maybe, for example, you might only be allowed to hit an API 30 times an hour, But what if that was 0 milliseconds
Wes Bos
or like Scott said earlier, you could also just have like an object or a key value database. But that's not really what we're talking about here. Talking about that, the actual outputted header.
Wes Bos
So you might have to cache it might even do caches as well. I would think so. Yeah, it's a question. We should ask the Cloudflare folks that. They would probably know that. Hey. I got a an interesting sidebar question. Yeah. You know when they have, like, Movies on an airplane that are stored onto a hard drive. Do you call that a cache? Oh, yeah. For sure. Yeah. That's a, like, a local cache where it's sitting in. Like, Like lots of ISPs have Netflix caches, right? Yep. So they don't have to go all the way to Netflix to do that.
Open graph images take long time to generate
Wes Bos
in there because then you have to have a whole stale if error. That's another one is like, Well, what happens if it breaks? Are you allowed to give them
Wes Bos
a while to generate
Wes Bos
But you, the listener, can think of any time you want a cache.
Wes Bos
like we're constantly changing the names of podcasts. And like, you don't want there'd be some You could put like a let's say we put a 1 minute
Wes Bos
those take why? Scott, you wanna grab the first one? Why might you want to cache something? you could possibly and that will give you the actual rendered out open graph image. And then you can look at it
Serving from cache avoids expense of regeneration
Scott Tolinski
it's total seconds. Right? That's how what a Max Age is said too? Yeah. It's It's it's one of the few things that are seconds based. Yeah. I know. I wanted to say milliseconds, but then I've made a max age calculator for myself because I was so annoyed with having to calculate it out all the time. Yeah. And so I'll post a link to that. I made it in Svelte if you wanna see it.
Scott Tolinski
serve up the cash version. Yeah. Well, okay, let's get into then
Scott Tolinski
is a is it is it
Scott Tolinski
just straight up normal caching headers, which you typically see in just as max age or s Max Age. And that basically
Stale while revalidate sets stale cache lifetime
Wes Bos
How long it's okay to but you're also getting it instantly because you somebody else kicked off the generation for you That that's been cooked beforehand. Why wouldn't you just toss stale while revalidate Alright. Well, I got to sit here and regenerate the whole website.
Wes Bos
client
Wes Bos
reuse or serve up a stale asset.
Why you might want to cache something
Wes Bos
then you might Want to dip into using the stale while revalidate header. So
Scott Tolinski
Yeah. Well, the thing is with caching is
Centux Replay for caching error handling
Scott Tolinski
So, head on over to century.i0
Scott Tolinski
But before we get into the contents of this episode,
Scott Tolinski
getting some potentially cached or incorrect
Scott Tolinski
because, hey, Sometimes that cash can get in the way for some reason.
Scott Tolinski
what the user's getting on their dashboard. You can even see if they were accidentally
Explanation of stale while revalidate caching header
Wes Bos
For the purpose of this podcast, we'll talk about
Caching HTML pages to avoid database queries
Wes Bos
That HTML page needs to be cached so that be serving up for you. Do you really need to sit around and wait for cash and have this like off by one error? and we're going to explain, first of all, like like why you might need it and then and then how it works. So
Wes Bos
First, one of them simply just being an HTML page the database queries, the rendering, all that stuff that needs to happen on the server I think the way that you said it is it makes sense.
Stale while revalidate serves stale but refreshes
Wes Bos
So the next time somebody comes, I'm going to have a fresh one for them. And then I've renewed the max age on there. So what it does is it kicks off a background task after another 3 seconds. So what is happening? ESPN gets
Wes Bos
revalidate says. Keep it for this long and it's okay to serve a stale version all I got is this stale JPEG get fast reloads,
Wes Bos
while you're waiting for it. And that's what the stale for 2 minutes. Otherwise you're going to run out of API hits, right? You can't be hitting at every single request. So it's good to You can either cache like the output 2 then
Wes Bos
a database query that is slow to resolve or Your like Scott just said, you don't have to redownload that, but it can also live on a CDN But also, like, with the let's say ESPN, let's say if you have a CDN like Vercel or Cloudflare and they've got 50 locations, you know, How long does it take to take that HTML file and propagate that through the world? So I don't know if the one second would be ideal in all of those cases because you have to literally wait for it, but image,
Wes Bos
instantly. So the idea is, and this is the same with like a React website that would be rendering,
Wes Bos
and it doesn't make the user it gives you the stale version immediately, which is often fine.
Wes Bos
A minute later and you've got the new version and then you got to wait another 15 seconds.
Wes Bos
If you visit a page, you might be getting stale content, but you should know that in the background for however long and then
Local browser caches assets
Wes Bos
so you can put a CDN in front of We were hitting asset, whether it is an HTML page, an image, some JavaScript, some CSS.
Wes Bos
They will automatically PNG of the the open graph image. You can have it for now because it's still fine. They said it would be fine for the next 10 minutes. It's okay. But It's okay. I'm going to go out back and make a new one.
Max age sets total cache lifetime
Wes Bos
Then you give it a stale value.
Wes Bos
you give an access a Max Age header. That is how long the asset is good for.
Some things don't need caching
Wes Bos
even even a minute then the next person that comes along is going to see the wrong logged in state.
Wes Bos
some issue. And also, like, the one second example, if there wasn't enough traffic for somebody to kick off the next regenerate,
Caching used in many areas of computer science
Scott Tolinski
is a technique used just about in every single facet of
Scott Tolinski
You're often needing
Scott Tolinski
to save things for later so that they're easily accessible.
Scott Tolinski
computer science. Right?
Server can respect cache headers
Wes Bos
because stale while revalidate is a header that you set on your responses, They are.
Why not use stale while revalidate everywhere?
Wes Bos
That's a good question. Like, first of all, you might not be getting that much traffic max age on there. So every time for the next minute you refresh the page, You're going to But if you think about that, like, oh, I'm generating hundreds of thousands of these for every single request.