DevOps & Cloud

Redis Caching Strategies: Speed Up Your Application 10x

February 22, 2026 2 min read 7 views

Redis processes over 100,000 operations per second with sub-millisecond latency. Used by Twitter, GitHub, and Shopify, it is the go-to solution for application caching and session storage.

Cache-Aside Pattern (Lazy Loading)

$users = Cache::remember('active_users', 3600, function () {
    return User::where('active', true)->with('profile')->get();
});

// With tags for granular invalidation
$post = Cache::tags(['posts', "user:{$userId}"])
    ->remember("post:{$id}", 1800, function () use ($id) {
        return Post::with('comments', 'author')->findOrFail($id);
    });

Cache Invalidation

class PostObserver
{
    public function updated(Post $post): void
    {
        Cache::tags(['posts'])->flush();
        Cache::forget("post:{$post->id}");
    }
}

Redis Data Structures Beyond Key-Value

// Sorted sets for leaderboards
Redis::zadd('leaderboard', $score, $userId);
$topTen = Redis::zrevrange('leaderboard', 0, 9, 'WITHSCORES');

// Lists for recent activity feeds
Redis::lpush("user:{$id}:activity", json_encode($event));
Redis::ltrim("user:{$id}:activity", 0, 49);

// Sets for unique tracking
Redis::sadd("post:{$id}:viewers", $userId);
$uniqueViews = Redis::scard("post:{$id}:viewers");

// Hashes for object storage
Redis::hset("user:{$id}", 'name', 'Mahmoud', 'visits', 42);

Cache Warming

class WarmCache extends Command
{
    protected $signature = 'cache:warm';

    public function handle(): void
    {
        Post::published()->chunk(100, function ($posts) {
            foreach ($posts as $post) {
                Cache::put("post:{$post->id}", $post, 3600);
            }
        });
        $this->info('Cache warmed successfully.');
    }
}

A Redis caching layer typically reduces database load by 80-90% and cuts page response times from 200ms to under 20ms.

Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!