Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NewToLaravel's avatar

Should I cache large datasets in Redis?

I'm not sure what category to put this under, but I'm using Laravel and Redis and just wanted an opinion.

I created a blog in Laravel, and I was just wondering - should I store my paginated results in Redis?

The server is not a problem, I have plenty of memory to use...but I'm not great when it comes to Redis and think maybe I'm using it wrong.

Lets say all my posts had 50,000 characters and I was storing 25 posts per page, so I stored all the results with "blog_posts_page_".

That's a pretty big dataset, no? It makes me feel like I'd be abusing Redis and causing problems that I don't really see.

I'm just looking for opinions really, I have 500 "dummy" posts with 10,000 - 50,000 characters in each.

This is purely educational. I'm trying to learn!

Thanks :)

0 likes
5 replies
lostdreamer_nl's avatar

why would you?

Running the same query would probably not be that much slower than accessing your cache.

You should only cache stuff that takes too long (or too much CPU) when you want get the data.

A simple 'select * from table LIMIT 0, 100' should never be cached.

zanderwar's avatar

@lostdreamer_nl that is very bad advice... elasticache usage is MOONS cheaper than RDS usage, all DB calls should be cached for a reasonable amount of time and even indefinitely as long as you handle the cache busting appropriately, no need to be lazy.

NewToLaravel's avatar

@lostdreamer_nl I was reading tutorials and articles about performance and they cache a lot of queries and datasets that don't look too resource intensive. I was also looking at speed comparisons between hitting the database and hitting the cache, and there's a big difference. I'd rather learn to do it properly now than have to change my methods down the line.

D9705996's avatar

I would only reach for caching when you find you actually need it I.e. your application feels slow. Laravel makes it really easy to cache eloquent queries with minimal code changes so don't worry about it. Also remember caching has down sides like cache invalidation

1 like
click's avatar

Thinking about caching is a good idea. But do not start with it would be my suggestion. Especially not when you start with programming. Simple queries won't take that much time. MySQL is fast if you do it right.

Build whatever you are building with good queries. If you are done and you see that some queries take a long time or your page load in general is slow. See if there is a way you can optimize or limit your queries (indexing, eager loading, etc.) If that still does not work: start caching results where possible. Not the other way around.

2 likes

Please or to participate in this conversation.