Laravel caching with Redis: tags, TTLs and stampedes
Cache the expensive read, invalidate it deliberately, and never cache models — the pitfalls are predictable once you name them.
Caching is easy to add and easy to get subtly wrong. The three failures we see most are stale data nobody invalidates, a cache stampede when a key expires, and a cached object that cannot be unserialised.
Cache data, not models
Laravel can serialise Eloquent models, but the database cache store may be configured to refuse class unserialisation, and a cached model can be stale in ways that are hard to trace. Cache arrays of scalars and rehydrate, or cache the rendered fragment.
Choose the invalidation strategy first
Either a short TTL with tolerable staleness, or explicit invalidation on write. Mixing the two without a plan produces "sometimes wrong" bugs. For content that editors change, tag the cache and flush the tag in the model event.
Avoid the stampede
When a popular key expires, every concurrent request recomputes it at once. Use the lock helper or cache::flexible-style early expiration so one request refreshes while others serve the stale value. On a busy page this is the difference between a blip and an outage.
Scope keys properly
Include the tenant, locale and any filter in the key. A key like "posts" will serve one user's filtered page to another. Prefix by environment too, so staging and production can share a Redis instance without crossing streams.
Frequently asked questions
Redis or database cache?
Redis for anything with real traffic; the database store is convenient for a small site and adds load to the database you are trying to protect.
How long should a TTL be?
As long as the data can be stale without hurting someone. A homepage section can be an hour; a price should be minutes or explicitly invalidated.
More on this topic: Performance & SEO articles.
Admin
Related articles
Internal linking: the cheapest SEO work you are not doing
Every important page should be linked from somewhere with descriptive anchor text. It costs nothing and it compounds.
Getting a new site indexed by Google: a checklist that works
Verify Search Console, submit a sitemap, make sure nothing is blocking crawl, and then be patient. Most indexing problems are self...
How to speed up a slow WordPress site without rebuilding it
Hosting, page cache, images and plugins — in that order. Most slow WordPress sites improve dramatically before any theme work.