Laravel 12 with Redis: The Beginner's Ultimate Guide

Author

Kritim Yantra

Apr 29, 2025

Laravel 12 with Redis: The Beginner's Ultimate Guide

When your Laravel app starts growing, handling sessions, caching, or queues efficiently becomes critical for performance. That’s where Redis shines! 🌟

In this blog, we’ll walk you through how to use Redis with Laravel 12 in a very simple and beginner-friendly way — even if you’re hearing about Redis for the first time.

Let’s dive right in! 🔥


📚 What You'll Learn

  • What is Redis?
  • Why use Redis with Laravel 12?
  • Installing Redis on your system
  • Setting up Laravel 12 with Redis
  • Using Redis for caching
  • Using Redis for sessions
  • Using Redis for queues
  • Bonus Tips

❓ What is Redis?

Redis stands for Remote Dictionary Server.
It is a super-fast, in-memory database that stores data as key-value pairs.

✅ Think of it like a very fast "notebook" where you can quickly save and retrieve data.

Redis is often used for:

  • Caching (storing data temporarily)
  • Session management
  • Queues (background jobs)
  • Real-time analytics

🚀 Why Use Redis with Laravel 12?

Laravel 12 is optimized to work with Redis easily. Some big advantages:

  • Speed: Redis is faster than regular databases for small, frequent operations.
  • Efficiency: It reduces database load.
  • Scalability: It helps handle more users at once without slowing down.

Laravel provides built-in drivers to work with Redis for Cache, Session, and Queue systems.


🛠️ Installing Redis

Before using Redis with Laravel, you need Redis running on your machine or server.

1️⃣ Install Redis on Mac

brew install redis
brew services start redis

2️⃣ Install Redis on Ubuntu/Linux

sudo apt update
sudo apt install redis-server
sudo systemctl enable redis-server.service

3️⃣ Install Redis on Windows

Use Memurai or Redis Windows binaries because Redis officially doesn't support Windows.


✅ Check if Redis is Running

After installation, check:

redis-cli ping

It should respond:

PONG

🎉 That means Redis is running!


🎯 Setting Up Laravel 12 with Redis

First, install the PHP Redis extension in your Laravel project.

1️⃣ Install phpredis extension

If you don't have it, you can install via:

  • Mac:

    brew install php-redis
    
  • Ubuntu/Linux:

    sudo apt install php-redis
    

After installing, restart your server (like php artisan serve) or PHP.


2️⃣ Configure Redis in Laravel

Laravel already has Redis support out of the box. Open your .env file and update:

CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis

Then open config/database.php and check the Redis configuration:

'redis' => [

    'client' => env('REDIS_CLIENT', 'phpredis'),

    'default' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
    ],

],

In .env file, add or verify:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Now Laravel is ready to talk to Redis! 🎉


📦 Using Redis for Caching in Laravel

1️⃣ Simple Caching Example

In your controller or route:

use Illuminate\Support\Facades\Cache;

Route::get('/cache', function () {
    Cache::put('name', 'Laravel with Redis', 600); // 600 seconds
    return 'Data cached successfully!';
});

2️⃣ Retrieve Cached Data

Route::get('/get-cache', function () {
    return Cache::get('name', 'default');
});

Explanation:

  • put() → Store data.
  • get() → Retrieve data.
  • 600 → Expiration time in seconds.

🔒 Using Redis for Sessions

Laravel makes it super easy to store session data in Redis.

  1. In your .env, set:
SESSION_DRIVER=redis
  1. Now, Laravel will store all session data inside Redis instead of the database or files.

✅ This speeds up authentication, user tracking, and admin panels!


📬 Using Redis for Queues

Queues are useful when you want to handle tasks like sending emails, notifications, or processing videos in the background.

  1. In your .env, set:
QUEUE_CONNECTION=redis
  1. Create a job:
php artisan make:job SendEmailJob
  1. Inside SendEmailJob.php, you can handle your logic.

  2. Dispatch the job:

SendEmailJob::dispatch($data);
  1. Start the worker:
php artisan queue:work redis

✅ Now your jobs will be pushed into Redis and workers will process them.


🎁 Bonus: Using Laravel Artisan Commands to Monitor Redis

  • Monitor Redis connections
php artisan queue:listen
  • View all cached keys (using redis-cli):
redis-cli KEYS *
  • Flush all Redis data (be careful!):
redis-cli flushall

🎉 Conclusion

And that's it! You have successfully connected Laravel 12 with Redis for caching, sessions, and queues.
Redis + Laravel is a powerful combo that makes your apps faster, smarter, and scalable.

Here’s a quick recap:

Feature Why Use Redis?
Caching Faster page loads, fewer DB queries
Sessions Quick and secure session storage
Queues Handle background jobs efficiently

Laravel’s elegant integration with Redis allows even beginners to take advantage of one of the fastest databases in the world — without a headache! 🧠

Tags

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts