Using Redis for Cache, Sessions, and Queues in Laravel 13
Redis is a useful backend for caching, session storage, and asynchronous job processing in Laravel.
Laravel 13 supports Redis through the phpredis extension or the predis/predis Composer package.
1. Configure Redis in Docker
First, make sure your PHP container has a Redis client installed.
To check for the PHP Redis extension:
php -m | grep redis
Alternatively, install Predis:
composer require predis/predis
2. Update Environment Variables
Configure the following values in .env:
CACHE_STORE=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_CLIENT=phpredis
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_DB=0
REDIS_CACHE_DB=1
Use the Redis service name from Docker Compose as REDIS_HOST when both containers share the same Docker network.
If you installed Predis instead of the PHP extension, change REDIS_CLIENT to predis.
3. Separate Redis Databases
Laravel’s Redis configuration can use different logical databases for different purposes.
For example:
'redis' => [
'client' => env('REDIS_CLIENT', 'phpredis'),
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
This separation helps prevent cache operations from interfering with sessions or queued jobs.
Some managed Redis services do not support multiple logical databases. In that case, use the same database and configure appropriate key prefixes.
4. Use Redis Through Laravel
For ordinary application caching, use the Cache facade:
use Illuminate\Support\Facades\Cache;
Cache::put('verification_code', '123456', now()->addMinutes(5));
$code = Cache::get('verification_code');
Cache::forget('verification_code');
For verification codes, Cache::pull() is useful because it retrieves and removes a value in one operation:
$code = Cache::pull('verification_code');
When you need Redis-specific data structures, use the Redis facade:
use Illuminate\Support\Facades\Redis;
Redis::incr('api_calls');
Redis::hset('user:1001', 'device', 'iOS');
Redis::get('some_key');
5. Use Redis Locks
Laravel also supports distributed locks through the cache system:
$lock = Cache::lock('order_submit:1001', 10);
if ($lock->get()) {
try {
// Perform the operation.
} finally {
$lock->release();
}
}
This is useful for preventing concurrent requests from processing the same operation.
6. Start a Queue Worker
Setting QUEUE_CONNECTION=redis only configures where jobs are stored. A worker must still consume them:
php artisan queue:work redis --tries=3
In Docker, run the worker as a persistent service rather than relying on a temporary interactive command.
Conclusion
Laravel provides convenient Redis integration through Cache, Redis, and its queue system.
Configure the three drivers in .env, verify the Redis connection, and make sure a queue worker is running whenever your application dispatches asynchronous jobs.
