Warning: You are browsing the documentation for Symfony 4.0, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

Redis Cache Adapter

4.0 version

Redis Cache Adapter

This adapter stores the values in-memory using one (or more) Redis server instances. Unlike the APCu adapter, and similarly to the Memcached adapter, it is not limited to the current server’s shared memory; you can store contents independent of your PHP environment. The ability to utilize a cluster of servers to provide redundancy and/or fail-over is also available.


Requirements: At least one Redis server must be installed and running to use this adapter. Additionally, this adapter requires a compatible extension or library that implements \Redis, \RedisArray, RedisCluster, or \Predis.

This adapter expects a Redis, RedisArray, RedisCluster, or Predis instance to be passed as the first parameter. A namespace and default cache lifetime can optionally be passed as the second and third parameters:

use Symfony\Component\Cache\Adapter\RedisAdapter;

$cache = new RedisAdapter(

    // the object that stores a valid connection to your Redis system
    \Redis $redisConnection,

    // the string prefixed to the keys of the items stored in this cache
    $namespace = '',

    // the default lifetime (in seconds) for cache items that do not define their
    // own lifetime, with a value 0 causing items to be stored indefinitely (i.e.
    // until RedisAdapter::clear() is invoked or the server(s) are purged)
    $defaultLifetime = 0

Configure the Connection

The createConnection() helper method allows creating and configuring the Redis client class instance using a Data Source Name (DSN):

use Symfony\Component\Cache\Adapter\RedisAdapter;

// pass a single DSN string to register a single server with the client
$client = RedisAdapter::createConnection(

The DSN can specify either an IP/host (and an optional port) or a socket path, as well as a user and password and a database index.


A Data Source Name (DSN) for this adapter must use the following format.

redis://[user:[email protected]][ip|host|socket[:port]][/db-index]

Below are common examples of valid DSNs showing a combination of available values:

use Symfony\Component\Cache\Adapter\RedisAdapter;

// host "my.server.com" and port "6379"

// host "my.server.com" and port "6379" and database index "20"

// host "localhost" and SASL use "rmf" and pass "abcdef"
RedisAdapter::createConnection('redis://rmf:[email protected]');

// socket "/var/run/redis.sock" and SASL user "user1" and pass "bad-pass"
RedisAdapter::createConnection('redis://user1:[email protected]/var/run/redis.sock');

Configure the Options

The createConnection() helper method also accepts an array of options as its second argument. The expected format is an associative array of key => value pairs representing option names and their respective values:

use Symfony\Component\Cache\Adapter\RedisAdapter;

$client = RedisAdapter::createConnection(

    // provide a string dsn

    // associative array of configuration options
        'lazy' => false,
        'persistent' => 0,
        'persistent_id' => null,
        'timeout' => 30,
        'read_timeout' => 0,
        'retry_interval' => 0,


Available Options

class (type: string)
Specifies the connection library to return, either \Redis or \Predis\Client. If none is specified, it will return \Redis if the redis extension is available, and \Predis\Client otherwise.
lazy (type: bool, default: false)
Enables or disables lazy connections to the backend. It’s false by default when using this as a stand-alone component and true by default when using it inside a Symfony application.
persistent (type: int, default: 0)
Enables or disables use of persistent connections. A value of 0 disables persistent connections, and a value of 1 enables them.
persistent_id (type: string|null, default: null)
Specifies the persistent id string to use for a persistent connection.
read_timeout (type: int, default: 0)
Specifies the time (in seconds) used when performing read operations on the underlying network resource before the operation times out.
retry_interval (type: int, default: 0)
Specifies the delay (in milliseconds) between reconnection attempts in case the client loses connection with the server.
timeout (type: int, default: 30)
Specifies the time (in seconds) used to connect to a Redis server before the connection attempt times out.


When using the Predis library some additional Predis-specific options are available. Reference the Predis Connection Parameters documentation for more information.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.