Start free trial
Start free trial Log in

14-day free trial. No credit card.

How to Monitor Laravel Cache in Production

Hit rates, latency, stampedes, and the events you can listen to right now.

  • Guide
  • 3 min read

How do I monitor Laravel cache in production?

Hook into CacheHit and CacheMissed events in AppServiceProvider to count hits vs misses, then aggregate by key pattern, not per-key. Track per-pattern hit rate and p95 latency. An APM with a cache watcher (NightOwl, Laravel Nightwatch Cloud) records every hit and miss without listener code, and NightOwl groups the keys into patterns for you.

Updated

A broken cache rarely announces itself. The app keeps working. It just gets slower and your database works harder, because the layer that was supposed to absorb reads has quietly stopped. By the time someone notices, the cause (a changed key, a TTL set to zero, a deploy that flushes everything) is days old. Monitoring the cache means watching the hit rate and latency so a cache that stops caching shows up as a number instead of a mystery slowdown.

Listen to cache events

Laravel fires events on every cache operation. Register listeners in AppServiceProvider::boot.

app/Providers/AppServiceProvider.phpphp
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Cache\Events\KeyWritten;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Redis;

public function boot(): void
{
    Event::listen(CacheHit::class, function (CacheHit $e) {
        // Group by pattern, not raw key: 'user:742' → 'user:?'
        $pattern = preg_replace('/:\d+/', ':?', $e->key);
        Redis::hincrby('cache_stats:hit', $pattern, 1);
    });

    Event::listen(CacheMissed::class, function (CacheMissed $e) {
        $pattern = preg_replace('/:\d+/', ':?', $e->key);
        Redis::hincrby('cache_stats:miss', $pattern, 1);
    });
}

Don't store raw keys. At scale, per-key metrics are unmanageable (millions of unique keys). Normalize to patterns and aggregate.

Prevent cache stampedes

A popular cached value expires. 100 requests simultaneously try to recompute it, all hitting the database. Your DB melts.

Stampede-prone: don't do this at scale

php
$value = Cache::remember('dashboard:stats', 300, function () {
    return DB::table('orders')->selectRaw('...')->get(); // expensive
});

Stampede-safe with atomic lock

php
$value = Cache::get('dashboard:stats');

if ($value === null) {
    $lock = Cache::lock('dashboard:stats:lock', 10);

    if ($lock->get()) {
        try {
            $value = DB::table('orders')->selectRaw('...')->get();
            Cache::put('dashboard:stats', $value, 300);
            Cache::forever('dashboard:stats:stale', $value); // fallback copy for waiters
        } finally {
            $lock->release();
        }
    } else {
        // Another worker is recomputing: wait or fall back
        $value = Cache::get('dashboard:stats:stale') ?? [];
    }
}

Laravel also ships this pattern built in. Cache::flexible('dashboard:stats', [300, 600], fn () => ...) treats the value as fresh for 300 seconds. Between 300 and 600 seconds after it was computed, callers get the stale copy while one deferred refresh recomputes it after the response is sent.

Pick a shared cache store

Multi-server setups need a shared cache. The file driver is per-server and will cause hard-to-debug inconsistencies. The database driver (the default since Laravel 11) is shared, but it puts every cache read on your database; Redis is the usual choice once cache traffic grows.

config/cache.phpphp
'default' => env('CACHE_STORE', 'redis'),

'stores' => [
    'redis' => [
        'driver' => 'redis',
        'connection' => 'cache',
        'lock_connection' => 'default',
    ],
],

Key things to alert on

  • Hit rate drop: from 90% to 60% suggests TTL or key-generation bug
  • Latency spike: cache p95 well above its usual baseline usually means the Redis box is under pressure
  • Eviction rate: Redis evicted_keys growing means cache is too small
  • Key cardinality explosion: wildcard patterns ballooning (e.g. a cached-per-user value instead of per-plan)

The cache that wasn't caching

A common version of this: someone caches a per-user value with Cache::remember("dashboard:{$user->id}", ...). It works in testing, where there's one user. In production every user gets their own key, so the hit rate for that pattern sits near zero (each key is read once, then expires before the same user comes back), and you're paying the full cost of caching with none of the benefit. The database load looks like there's no cache at all, because for that pattern there effectively isn't.

You only catch this if you group cache metrics by pattern. A per-key view drowns in millions of unique keys; a per-pattern view shows dashboard:? at a 3% hit rate and the problem names itself. The fix is usually to cache at a coarser granularity (per plan, per team) or to accept that this particular value isn't worth caching.

See the cache patterns that keep missing

The dashboard:? story above only shows up when calls are grouped by key pattern. NightOwl does that grouping for you: keys carrying ids collapse into patterns like user:{int}:profile, each with its own hit rate, tied to the requests that made the calls.

No credit card needed.

Terminal
composer require nightowl/agent
php artisan nightowl:install
php artisan nightowl:agent

Frequently asked questions

Anything else, write to support@usenightowl.com

How do I monitor Laravel cache hit rates?

Hook into CacheMissed and CacheHit events in AppServiceProvider. Count hits vs misses per key pattern (not per key) and aggregate by minute or hour. Watch each pattern against its own history: a pattern that used to hit most of the time and now mostly misses usually means a cache isn't doing what you think it is.

What's a good cache hit rate for Laravel?

Depends on the use case. A value that is read far more often than it changes should hit most of the time, while write-heavy caches can sit much lower. The absolute number matters less than the trend. A drop from 90% to 60% over a week is a real signal even if 60% would be fine for another app.

Should I use Redis or Memcached for Laravel cache?

Redis for most production apps in 2026. It supports more data types, has better persistence options, and is required if you run Horizon. Queues can also run on the database, SQS or Beanstalkd drivers, so Redis isn't mandatory for them. Memcached is slightly faster for pure get/set but that rarely matters. If you already run Redis for Horizon, the same cluster can serve cache and sessions too if scale allows.

How do I debug a Laravel cache that isn't being hit?

Log cache operations with Event::listen(CacheHit::class, ...) and CacheMissed::class. Check for key-mismatch bugs (different prefix, hashing, serialization), unexpected TTLs, or Cache::flush() calls on deploy. If you use tagged caches, verify the driver supports them. Redis, Memcached, APC and array do; the file, database and DynamoDB stores don't.

Does Laravel cache work across multiple servers?

Only if the cache store is shared. The file store is per-server. The database store (Laravel's default since version 11) is shared, because every server reads the same table, and Redis, Memcached and DynamoDB are shared by design. If you're running Laravel behind a load balancer with file caching enabled, each server has its own cache, so expect weird inconsistencies.

How do I monitor cache latency in Laravel?

Wrap Cache::get and put calls to time them, or use an APM that records cache operations with duration. Compare p95 against your own baseline rather than a fixed number. Latency spikes usually mean network issues between your app and the cache box, or eviction pressure from a cache that's too small.

What's cache stampede and how do I prevent it?

A cache stampede happens when a popular key expires and 100 requests simultaneously try to recompute it, all hitting the database. Fix with Cache::lock() or atomic operations: the first request acquires the lock, recomputes and stores the value, everyone else waits or falls back to a stale value. Laravel's Cache::flexible() packages the stale-while-revalidate version of this for you.

Flat pricing, whatever your traffic does

Every plan starts with a 14-day free trial. No credit card needed.

Hobby
$5 a month

One app for a side project. Issues and alerts, with a 14-day dashboard window.

Start my 14-day trial
TeamRecommended
$15 a month

Up to 3 apps and 5 people per team. Full history, the MCP server, data clearing and agent health.

Start my 14-day trial
Agency
$69 a month

Unlimited client apps and people, one overview of every app, and apps you can hand over.

Start my 14-day trial

Related

Find the cache keys that never hit

Install the agent and your cache calls group into key patterns, each with its own hit rate. No credit card needed.