Kritim Yantra
Apr 29, 2025
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! 🔥
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:
Laravel 12 is optimized to work with Redis easily. Some big advantages:
Laravel provides built-in drivers to work with Redis for Cache, Session, and Queue systems.
Before using Redis with Laravel, you need Redis running on your machine or server.
brew install redis
brew services start redis
sudo apt update
sudo apt install redis-server
sudo systemctl enable redis-server.service
Use Memurai or Redis Windows binaries because Redis officially doesn't support Windows.
After installation, check:
redis-cli ping
It should respond:
PONG
🎉 That means Redis is running!
First, install the PHP Redis extension in your Laravel project.
phpredis
extensionIf 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.
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! 🎉
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!';
});
Route::get('/get-cache', function () {
return Cache::get('name', 'default');
});
Explanation:
put()
→ Store data.get()
→ Retrieve data.600
→ Expiration time in seconds.Laravel makes it super easy to store session data in Redis.
.env
, set:SESSION_DRIVER=redis
✅ This speeds up authentication, user tracking, and admin panels!
Queues are useful when you want to handle tasks like sending emails, notifications, or processing videos in the background.
.env
, set:QUEUE_CONNECTION=redis
php artisan make:job SendEmailJob
Inside SendEmailJob.php
, you can handle your logic.
Dispatch the job:
SendEmailJob::dispatch($data);
php artisan queue:work redis
✅ Now your jobs will be pushed into Redis and workers will process them.
php artisan queue:listen
redis-cli
):redis-cli KEYS *
redis-cli flushall
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! 🧠✨
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google