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! 🧠

LIVE MENTORSHIP ONLY 5 SPOTS

Laravel Mastery
Coaching Class Program

KritiMyantra

Transform from beginner to Laravel expert with our personalized Coaching Class starting June 21, 2025. Limited enrollment ensures focused attention.

Daily Sessions

1-hour personalized coaching

Real Projects

Build portfolio applications

Best Practices

Industry-standard techniques

Career Support

Interview prep & job guidance

Total Investment
$200
Duration
30 hours
1h/day

Enrollment Closes In

Days
Hours
Minutes
Seconds
Spots Available 5 of 10 remaining
Next cohort starts:
June 21, 2025

Join the Program

Complete your application to secure your spot

Application Submitted!

Thank you for your interest in our Laravel mentorship program. We'll contact you within 24 hours with next steps.

What happens next?

  • Confirmation email with program details
  • WhatsApp message from our team
  • Onboarding call to discuss your goals

Tags

Comments

Eric Winter

Eric Winter

Apr 30, 2025 08:20 PM

Excellent write-up and exactly what I was looking for. I can already see the performance improvement. Thank you!
K

Kritim Yantra

May 30, 2025 09:19 AM

Thank you so much for your kind words! I’m really glad it helped in your learning. Keep exploring and all the best! 🚀

Please log in to post a comment:

Sign in with Google

Related Posts