Boosting App Speed with Flexible Caching - Laravel In Practice EP8
Last updated on by Harris Raftopoulos
▶️ Watch the video tutorial (10 minutes)
Boosting App Speed with Flexible Caching - Laravel In Practice EP8
Your cache is fast, but users still hit slow responses when it expires. What if you could serve instant responses even with stale data while Laravel refreshes the cache in the background? Enter Laravel 12's game-changing flexible caching.
In this episode of Laravel In Practice, I show you how to implement the stale-while-revalidate pattern using Laravel's new Cache::flexible() method. You'll learn why traditional caching creates periodic slow responses for unlucky users, how flexible caching serves stale data instantly while refreshing in the background, and the optimal TTL values for different types of dashboard data.
The result? Your dashboard maintains sub-10ms response times even during cache refreshes. No more users hitting the "cache rebuild penalty" so everyone gets fast responses while Laravel quietly updates the data behind the scenes. You'll see exactly how to balance data freshness with performance using the two-tier TTL system.
This continues our Performance Optimization journey, building on the SQL optimizations from Episode 7. We're wrapping our optimized service with flexible caching that provides both speed and freshness. The implementation is surprisingly simple, just replacing Cache::remember() with Cache::flexible() and choosing appropriate fresh and stale periods.
The flexible caching strategies we implement here achieve consistent sub-10ms responses regardless of cache state. Next episode, we'll add memoization to eliminate duplicate cache hits within the same request.