Redis cache with Laravel

Welcome dear reader, today you will learn how to speed up your Laravel application with Redis caching. If your website feels slow because of expensive database queries, caching is often the quickest win available. In this guide, we will install Redis, connect it to a Laravel project, and cache our first piece of data — all in a few small steps.

At re:Software S.L., we create innovative solutions for businesses that aim to digitize their processes and routines, and this topic today is part of one of our stories about digitization.

Table of contents

What is Redis and Why Use It for Caching?

Redis is an open-source, in-memory key-value store. Because the data lives in RAM instead of on disk, reading and writing is extremely fast — often in the range of microseconds. This makes Redis a popular companion for web frameworks such as Laravel.

Caching means storing the result of an expensive operation — for example a database query — and reusing it for the next requests. Instead of hitting MySQL on every page load, your application asks Redis first and only falls back to the database when the cached copy is missing or expired.

If you want to make those underlying queries faster as well, have a read about MySQL indexes and query optimization — caching and indexing work hand in hand.

How to Install and Start Redis

On most Linux distributions, Redis is available from the package manager. On Ubuntu or Debian, run:

# Install the Redis server
sudo apt install redis-server

# Verify that Redis is up and running
redis-cli ping

If everything is installed correctly, the last command answers with PONG. That is your first cache hit — well, sort of.

How to Configure Laravel to Use Redis

Laravel talks to Redis using the PHP Redis extension (phpredis) or the predis Composer package. The simplest setup uses Composer:

# Install the predis package
composer require predis/predis

Next, open the .env file in the root of your Laravel project and update the cache and Redis settings:

# Tell Laravel to use Redis as the cache store
CACHE_STORE=redis

# Connection details for Redis
REDIS_CLIENT=predis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=null

That is all the configuration needed — Laravel resolves everything else out-of-the-box. In case you are starting a fresh project, the guide on how to build your own SaaS with Laravel covers the project creation.

Cache and Retrieve Data in Laravel

With Redis configured, the Cache facade becomes your best friend. The most useful method is Cache::remember(), which fetches the cached value or computes and stores it when missing:

use Illuminate\Support\Facades\Cache;

// Fetch from Redis, or query the database and cache for 60 minutes
$posts = Cache::remember('featured_posts', 60, function () {
    return Post::latest()->take(5)->get();
});

The first argument is the cache key, the second is the lifetime in minutes, and the third is a closure that runs only when the cache is empty. You can also manage entries manually:

// Store a value in Redis
Cache::put('featured_posts', $posts, 60);

// Retrieve a value (returns null if missing)
$posts = Cache::get('featured_posts');

// Remove a specific key
Cache::forget('featured_posts');

While developing, you can wipe the whole cache from the terminal with php artisan cache:clear.

Conclusion

That’s already it to get started with Redis caching in Laravel. Install Redis, point the CACHE_STORE at it, and wrap your expensive queries with Cache::remember(). Your database will thank you, and your visitors will enjoy the faster pages. Leave a comment below if you have any questions or feedback about the content of this article.

Editorial Team

By Editorial Team

A source code library for the open world! Find snippets and examples for popular programming languages including PHP, Javascript, Typescript, Node.js, Vue.js, and more.

One thought on “Redis Cache for Laravel Applications”

Leave a Reply