← Previous · All Episodes
Which layer of caching does your slow app actually need? Episode 159

Which layer of caching does your slow app actually need?

· 11:33

|

Welcome to No Compromises. A peek into the mind of two old web devs who have seen some things. This is Joel.

And this is Aaron.

So, in general, I know there is this concept on the internet of caching things, right? And basically, that means keep a copy of it,

I guess, and just serve that copy again instead of getting the real one. But I'm going to play a little bit of a dummy here,

a little bit of a devil's advocate, Joel. And I'm going to pretend like I don't actually know much about caching.

Okay.

Because I think I'm going to ask you these questions because it's going to lead us through an interesting cache arc, a story arc through all the tools that

we have evolved, involved, available inside of Laravel. So, in general, if I were to say to you, "I think I need caching. My website is slow."

Okay.

What was the type of questions you'd ask me about first to kind of figure out why I'm even bringing that up?

I mean, I would say like, "What's slow?" right?

Yeah.

"Is it a particular page? Is it the site as a whole? Is it like this once? You know, it's e-commerce site, it's like this one product takes forever to load,

but like all the other ones load fine." Like, I would definitely start like, "What's slow? And what's the pattern for when it's slow?"

Okay. So, let's just say then that we've had... I'm going to skip a lot of the part of the conversations just to make this a shorter podcast.

Okay.

We've went down a route. We're going to splinter off one direction and be like, we've determined that it's just every single page is really slow.

It's mainly like an HTML website, but it's loading stuff from the database. But every single page is just really slow,

and it's third-party resource that's loading that are slow. It's just so slow all in general.

And it's dynamic enough that we can't reasonably do, like Cloudflare or something in front of it and just try to solve it that way?

Well, that might be the first step. So, what does that mean?

Okay. So, yeah, I guess the technical term for that would be like edge caching. Where you put a proxy server, in this case I'm going to say Cloudflare,

because the free tier is pretty generous and it's easy to turn it on. But it sits in between you, you as the server, and your users. So Cloudflare,

let's say you have an image-heavy site, it would keep a copy of all those images on its edge servers all around the world. You know, they have them everywhere.

And then if a second person in that region requests it and doesn't even hit your server, it would just pull it from Cloudflare.

And it should theoretically be faster because it's closer to them and your server is not being involved. So that's my high-level explanation of edge caching.

Perfect. So I think I understand that.

Okay.

It's basically look at my whole site, and it makes a copy, and it serves it to me closer. Now, that's fine. But you mentioned, you know, if it's not too

dynamic. Well, everything on my site now, let's just splinter a different direction, mainly it can be cached but there are some dynamic things. But when I say

dynamic, I mean I hit a third-party API on every page load here to get some information, but I know that that information only changes once a day.

Okay.

But I have to wait four or five seconds for that API load each time. And obviously that's not okay, because does caching help me there?

Yes, Aaron, it does. So, this is like, I would say, the much more granular inside the code. Now we're talking about using the cache facade in Laravel or

something like that. Where you've identified a particular operation in your code that's slow. Like you mentioned, a third-party API call.

And you also have clarified a certain level of freshness of that data is okay. You know, let's take an extreme example.

I really only need to get this data once every 24 hours, and so instead of, like, all 10,000 people that hit my site, resulting in 10,000 calls,

I could call them one time, and then I could cache that result and store it and serve it to the other 9,999 visitors.

Using something like the cache facade or something inside of Laravel, right?

Right.

That's kind of what you're referring to. Is, it has this first-party facade that you can work cache. Okay, that makes sense.

But I've been told by senior developers in the past that people reach for caching too often. So, why did you talk about caching there if it's only once a day?

Why not just run a job, put that in a database, and retrieve that?

So, proactively decide I'm going to need this data at some point today, so I'm going to go get it?

Yeah, why make the first person wait five seconds?

Oh, I don't like that person. I mean, I guess you could, but to me it feels a little weird. Because with caching, you have a concept of cache invalidation

where you can push a button and clear it out, and then it will automatically refresh it. You also have concepts of cache warming, where you don't

have to make that person wait. I don't have a strong reason. Maybe you have one you want to talk about why not doing it in a job.

It just feels weird, it feels like using a knife instead of a screwdriver. It's like, yeah, it works, but it doesn't feel like the right tool.

Yeah. But if you talk if you talk about things like cache warming, you have to run a job anyway. You can't warm a cache before someone hits it.

So, that mechanic is out the door. I'm just talking about more so that caching doesn't necessarily mean using the cache facade.

Sure.

Because remember we talked about caching. First of all, caching was Cloudflare.

Yeah.

Then caching is the named cache facade. Caching could also mean capturing data and serving it a different way that is not necessarily inside of that cache

facade. It could be every midnight or every 2 a.m. we get new results that go in here, and then that SQL table is actually part of a query that we do somewhere else.

So, maybe it wouldn't be great if it was in the actual Redis cache or something because I can't join against it.

Yeah, okay. You're right. Yeah, caching is a more general term and strategy. It's not just that one specific thing that you'll find if you go to

Laravel docs and type caching.

Yeah. So, and then so that covers, like from the outside, it covers a third-party API, it covers the database queries and stuff. What about caching...

And I don't know how to describe this, but like something that's really complex to solve. Some really complex programming,

a function that takes a long time to run. And it just takes that long to run and maybe I need it a couple times. What do I do with that?

Like, I don't know if there's an output there. It just needs to do something over and over.

It would just be something... Like, when you were describing that? I'm thinking of like a really expensive report that aggregates sales data

across a bunch of locations and sales categories, and like it totals that up, right? But it's for Q1, and if somebody comes behind you

and generates it for Q1 30 minutes later, it should be the same data.

I'm saying you could go like that, but I'm saying more so like there's a complicated calculation that determines the workflow of this controller.

And maybe the results of that complicated calculation are used multiple times in the whole workflow.

Oh, okay. So, maybe this is like not between requests. But like within a single request,

this thing will have to be calculated or loaded into memory, and I only want to do that once?

Yeah.

Yeah, I would consider that caching. I mean, can I get out my computer science textbook and use the word memoization? But like in Laravel,

we have a helper, a global helper called Once, that does what you're describing. Like that, yes, I would consider that to be in the technique of caching as well.

And the reason you might reach for that instead of like... Because right away when I thought like calculates it and it has a store, or whatever,

I just thought, "We'll put it in a variable, whatever, bro. And use that later." But it could be that this is something that happens in the

middleware to determine access. Then it's something that happens in the controller, and then maybe it's something that happens inside of a repository.

So, it could happen multiple times, and trying to pass that variable result around might not be as easy as just calling that same function again.

But since that function is memoized, it will return it once, and you'll always get that same result.

Yeah. And maybe in that case, actually sticking it in Redis or that database would be overkill.

It's literally just going to live for this one user for the life of their one request.

Yeah, so I just wanted to cover like the points of caching. Because I think so many times people throw out, "Oh, just cache that," or "My stuff's slow.

I didn't cache it." Yeah, but what does that actually mean? Is it the database? Is it the front end? Is it all these different combinations?

Is it calculations? etc. So, I just wanted to walk through some layers of caching, and there's many more beyond this.

Sure.

There's, you know, individual pieces that we can cache too. But I just wanted to make sure that was clear that when someone says cache,

there's a lot more questions to be asked about it.

I'm probably a simple person. But every once in a while, somebody will say something that just sticks in my head, and it's like, "Whoa."

Like, I just keep thinking about it way longer than I should. So, the other day, we were talking about food, and my wife...

I was talking about like, sausage and pepperoni, and my wife just said, "Pepperoni is sausage." I'm like, "Whoa, what?"

And I thought about it, it's like, yeah, if you ever see, like a whole pepperoni, it's a sausage. It's just like cut up and sliced.

So, if you're thinking pizza toppings, right? Like, sausage is one shape, pepperoni is another shape, but like they're basically the same thing, correct?

I guess. I don't eat pizza, and I don't eat pork.

Oh, no. (Unclear) though.

Well, I always knew it was a... Well, do you think it grows on trees and just takes shape?

I don't know.

Of course, it's a sausage. I don't understand. This is what's wrong with you people that eat pizza. You just decide to change your mind.

But so despite my own simple-mindedness here, and to this day I'll just say to her, like, "Man, I'm still thinking about pepperoni as sausage."

But it has helped me in one regard. So, there's this weird negotiation we have. Again, you won't know this as a non-pizza eater,

but like, "Well, what are we going to get on the pizza?" And she always throws out this thing. "Well, we can't have two meats." Like, that's too much.

Like, you can have two veggies, but like two meats is like too decadent. But now I'm like, "Well, pepperoni is sausage? So, it's only one meat."

If I want both pepperoni and sausage on the pizza, I win.

Oh, okay.

You know when you said things that stick in people's head when you say something? Someone said something to me once that is not exactly

the same thing, but I think about it a lot. They said to me, "Nice face."

So, I don't know. You just want to give us some cash?

Then head over to masteringlaravel.io. And you can either give us some cash and buy a book or something, or learn about caching.

View episode details


Creators and Guests


Subscribe

Listen to No Compromises using one of many popular podcasting apps or directories.

Apple Podcasts Spotify Overcast Pocket Casts Amazon Music
← Previous · All Episodes