Redis
Before using Redis with Laravel, we encourage you to install and use the PHP extension via PECL. The extension is more complex to install compared to “user-land” PHP packages but may yield better performance for applications that make heavy use of Redis. If you are using Laravel Sail, this extension is already installed in your application’s Docker container.
If you are unable to install the phpredis extension, you may install the package via Composer. Predis is a Redis client written entirely in PHP and does not require any additional extensions:
You may configure your application’s Redis settings via the config/database.php
configuration file. Within this file, you will see a redis
array containing the Redis servers utilized by your application:
'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), ], ],
Each Redis server defined in your configuration file is required to have a name, host, and a port unless you define a single URL to represent the Redis connection:
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'default' => [ 'url' => 'tcp://127.0.0.1:6379?database=0', ], 'cache' => [ 'url' => 'tls://user:[email protected]:6380?database=1', ], ],
Configuring The Connection Scheme
By default, Redis clients will use the tcp
scheme when connecting to your Redis servers; however, you may use TLS / SSL encryption by specifying a scheme
configuration option in your Redis server’s configuration array:
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'default' => [ 'scheme' => 'tls', 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], ],
If your application is utilizing a cluster of Redis servers, you should define these clusters within a clusters
key of your Redis configuration. This configuration key does not exist by default so you will need to create it within your application’s config/database.php
configuration file:
By default, clusters will perform client-side sharding across your nodes, allowing you to pool nodes and create a large amount of available RAM. However, client-side sharding does not handle failover; therefore, it is primarily suited for transient cached data that is available from another primary data store.
If you would like to use native Redis clustering instead of client-side sharding, you may specify this by setting the options.cluster
configuration value to redis
within your application’s config/database.php
configuration file:
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'options' => [ 'cluster' => env('REDIS_CLUSTER', 'redis'), ], 'clusters' => [ // ... ], ],
If you would like your application to interact with Redis via the Predis package, you should ensure the REDIS_CLIENT
environment variable’s value is predis
:
'redis' => [ 'client' => env('REDIS_CLIENT', 'predis'), // ...],
In addition to the default host
, port
, database
, and password
server configuration options, Predis supports additional that may be defined for each of your Redis servers. To utilize these additional configuration options, add them to your Redis server configuration in your application’s config/database.php
configuration file:
The Redis Facade Alias
'aliases' => Facade::defaultAliases()->merge([ 'Redis' => Illuminate\Support\Facades\Redis::class,])->toArray(),
By default, Laravel will use the phpredis extension to communicate with Redis. The client that Laravel will use to communicate with Redis is dictated by the value of the redis.client
configuration option, which typically reflects the value of the environment variable:
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), // Rest of Redis configuration...],
In addition to the default scheme
, host
, port
, database
, and password
server configuration options, phpredis supports the following additional connection parameters: name
, persistent
, persistent_id
, prefix
, read_timeout
, retry_interval
, timeout
, and context
. You may add any of these options to your Redis server configuration in the config/database.php
configuration file:
'default' => [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD'), 'port' => env('REDIS_PORT', 6379), 'database' => 0, 'read_timeout' => 60, 'context' => [ // 'auth' => ['username', 'secret'], // 'stream' => ['verify_peer' => false], ],],
phpredis Serialization & Compression
The phpredis extension may also be configured to use a variety of serialization and compression algorithms. These algorithms can be configured via the options
array of your Redis configuration:
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'options' => [ 'serializer' => Redis::SERIALIZER_MSGPACK, 'compression' => Redis::COMPRESSION_LZ4, ], // Rest of Redis configuration...],
Currently supported serialization algorithms include: Redis::SERIALIZER_NONE
(default), Redis::SERIALIZER_PHP
, Redis::SERIALIZER_JSON
, Redis::SERIALIZER_IGBINARY
, and Redis::SERIALIZER_MSGPACK
.
Supported compression algorithms include: Redis::COMPRESSION_NONE
(default), Redis::COMPRESSION_LZF
, Redis::COMPRESSION_ZSTD
, and Redis::COMPRESSION_LZ4
.
You may interact with Redis by calling various methods on the Redis
facade. The Redis
facade supports dynamic methods, meaning you may call any on the facade and the command will be passed directly to Redis. In this example, we will call the Redis GET
command by calling the get
method on the facade:
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller;use Illuminate\Support\Facades\Redis;use Illuminate\View\View; class UserController extends Controller{ /** * Show the profile for the given user. */ public function show(string $id): View { return view('user.profile', [ 'user' => Redis::get('user:profile:'.$id) ]); }}
As mentioned above, you may call any of Redis’ commands on the Redis
facade. Laravel uses magic methods to pass the commands to the Redis server. If a Redis command expects arguments, you should pass those to the facade’s corresponding method:
use Illuminate\Support\Facades\Redis; Redis::set('name', 'Taylor'); $values = Redis::lrange('names', 5, 10);
Alternatively, you may pass commands to the server using the Redis
facade’s command
method, which accepts the name of the command as its first argument and an array of values as its second argument:
Using Multiple Redis Connections
Your application’s config/database.php
configuration file allows you to define multiple Redis connections / servers. You may obtain a connection to a specific Redis connection using the Redis
facade’s connection
method:
$redis = Redis::connection('connection-name');
To obtain an instance of the default Redis connection, you may call the connection
method without any additional arguments:
$redis = Redis::connection();
use Redis;use Illuminate\Support\Facades; Facades\Redis::transaction(function (Redis $redis) { $redis->incr('user_visits', 1); $redis->incr('total_visits', 1);});
Lua Scripts
The eval
method provides another method of executing multiple Redis commands in a single, atomic operation. However, the eval
method has the benefit of being able to interact with and inspect Redis key values during that operation. Redis scripts are written in the .
The eval
method can be a bit scary at first, but we’ll explore a basic example to break the ice. The eval
method expects several arguments. First, you should pass the Lua script (as a string) to the method. Secondly, you should pass the number of keys (as an integer) that the script interacts with. Thirdly, you should pass the names of those keys. Finally, you may pass any other additional arguments that you need to access within your script.
In this example, we will increment a counter, inspect its new value, and increment a second counter if the first counter’s value is greater than five. Finally, we will return the value of the first counter:
$value = Redis::eval(<<<'LUA' local counter = redis.call("incr", KEYS[1]) if counter > 5 then redis.call("incr", KEYS[2]) end return counterLUA, 2, 'first-counter', 'second-counter');
Warning
Please consult the Redis documentation for more information on Redis scripting.
Sometimes you may need to execute dozens of Redis commands. Instead of making a network trip to your Redis server for each command, you may use the pipeline
method. The pipeline
method accepts one argument: a closure that receives a Redis instance. You may issue all of your commands to this Redis instance and they will all be sent to the Redis server at the same time to reduce network trips to the server. The commands will still be executed in the order they were issued:
use Redis;use Illuminate\Support\Facades; Facades\Redis::pipeline(function (Redis $pipe) { for ($i = 0; $i < 1000; $i++) { $pipe->set("key:$i", $i); }});
Laravel provides a convenient interface to the Redis publish
and subscribe
commands. These Redis commands allow you to listen for messages on a given “channel”. You may publish messages to the channel from another application, or even using another programming language, allowing easy communication between applications and processes.
First, let’s setup a channel listener using the subscribe
method. We’ll place this method call within an since calling the subscribe
method begins a long-running process:
Now we may publish messages to the channel using the publish
method:
Wildcard Subscriptions
Using the method, you may subscribe to a wildcard channel, which may be useful for catching all messages on all channels. The channel name will be passed as the second argument to the provided closure:
Redis::psubscribe(['*'], function (string $message, string $channel) { echo $message;}); Redis::psubscribe(['users.*'], function (string $message, string $channel) { echo $message;});