Laravel Tutorials

Boosting App Speed with Flexible Caching - Laravel In Practice EP8

Published

▶️ 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.

▶️ Watch Episode 8 now →

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article