Cache
Use Redis as a cache driver
Queues
Use Redis as a queue driver
Broadcasting
Real-time messaging with Pub/Sub
Introduction
Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, and sorted sets. Laravel uses Redis across multiple features:Choosing a client
Laravel supports two Redis clients: PhpRedis (a PHP extension) and Predis (a PHP package).Laravel 13 defaults to PhpRedis. PhpRedis is recommended for production.
If you use Laravel Sail, PhpRedis is already installed in your Docker container.
Configuration
config/database.php
Redis settings live in theredis array in config/database.php:
Environment variables
Set the connection details in your.env file:
TLS / SSL connections
To use TLS encryption, add thescheme option to a connection:
Clusters
Define a Redis cluster using theclusters key in config/database.php:
options.cluster is set to redis, enabling native Redis clustering with automatic failover.
To use client-side sharding with Predis instead, remove the options.cluster key. Note that client-side sharding does not handle failover and is best suited for transient cached data.
Predis
Install the package and set the environment variable:Retry and backoff (Predis)
Predis 3.4.0 and later supports built-in retry and backoff configuration. Configure retries using themax_retries option and configure the backoff strategy using the retry option. The retry option should be an array keyed by one of the following strategy classes: NoBackoff, EqualBackoff, or ExponentialBackoff:
parameters option of your cluster configuration:
PhpRedis
PhpRedis is installed via PECL (pre-installed in Laravel Sail). It supports these additional options:name, persistent, persistent_id, prefix, read_timeout, retry_interval, max_retries, backoff_algorithm, backoff_base, backoff_cap, timeout, and context.
Retry and backoff (PhpRedis)
Configure reconnection behavior with these options. Supported backoff algorithms:default, decorrelated_jitter, equal_jitter, exponential, uniform, constant.
Unix socket connections
Use a Unix socket instead of TCP to eliminate TCP overhead when Redis runs on the same server:Serialization and compression
Configure a serializer and compression algorithm in theoptions array:
Redis::SERIALIZER_NONE (default), Redis::SERIALIZER_PHP, Redis::SERIALIZER_JSON, Redis::SERIALIZER_IGBINARY, Redis::SERIALIZER_MSGPACK.
Available compression algorithms: Redis::COMPRESSION_NONE (default), Redis::COMPRESSION_LZF, Redis::COMPRESSION_ZSTD, Redis::COMPRESSION_LZ4.
Interacting with Redis
The Redis facade
TheRedis facade supports dynamic methods — you can call any Redis command directly on it:
command method to pass the command name and arguments explicitly:
Multiple connections
Switch between named connections defined inconfig/database.php:
Transactions
transaction() wraps Redis’s MULTI/EXEC commands. All commands in the closure run as a single atomic operation:
Lua scripts
Theeval method executes a Lua script atomically and can read Redis values during execution:
KEYS[1], KEYS[2], etc., and extra arguments as ARGV[1], etc.
Pipelining
Send many commands at once to reduce network round trips. Commands are executed in order but not atomically:Pub / Sub
Laravel provides a convenient interface to Redis’spublish and subscribe commands.
Subscribing
Becausesubscribe starts a long-running process, call it from an Artisan command:
Publishing
Publish messages from any part of your application:Wildcard subscriptions
Usepsubscribe to subscribe to channels matching a pattern. The channel name is passed as the second argument:
Quick reference
Choosing a client
Choosing a client
- Production: PhpRedis (PECL extension, best performance)
- Development / restricted environments: Predis (
composer require predis/predis) - Laravel Sail: PhpRedis is pre-installed
Operation comparison
Operation comparison