This section assumes that you have a fully-working Redis server and have also
installed and configured the phpredis extension.

First, define a Symfony service for the connection to the Redis server:

YAML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# config/services.yamlservices:# ...Redis:# you can also use \RedisArray, \RedisCluster or \Predis\Client classesclass:Rediscalls:-method:connectarguments:-'%env(REDIS_HOST)%'-'%env(int:REDIS_PORT)%'# uncomment the following if your Redis server requires a password# - method: auth# arguments:# - '%env(REDIS_PASSWORD)%'

XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

<?xml version="1.0" encoding="UTF-8" ?><containerxmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd"><services><!-- you can also use \RedisArray, \RedisCluster or \Predis\Client classes --><serviceid="Redis"class="Redis"><callmethod="connect"><argument>%env(REDIS_HOST)%</argument><argument>%env(int:REDIS_PORT)%</argument></call><!-- uncomment the following if your Redis server requires a password: <call method="auth"> <argument>%env(REDIS_PASSWORD)%</argument> </call> --></service></services></container>

PHP

1
2
3
4
5
6
7
8
9
10

useSymfony\Component\DependencyInjection\Reference;// ...$container// you can also use \RedisArray, \RedisCluster or \Predis\Client classes->register('Redis',\Redis::class)->addMethodCall('connect',['%env(REDIS_HOST)%','%env(int:REDIS_PORT)%'])// uncomment the following if your Redis server requires a password:// ->addMethodCall('auth', ['%env(REDIS_PASSWORD)%']);

Now pass this \Redis connection as an argument of the service associated to the
RedisSessionHandler.
This argument can also be a \RedisArray, \RedisCluster, \Predis\Client,
and RedisProxy:

YAML

1
2
3
4
5
6
7
8
9

# config/services.yamlservices:# ...Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:arguments:-'@Redis'# you can optionally pass an array of options. The only option is 'prefix',# which defines the prefix to use for the keys to avoid collision on the Redis server# - { prefix: 'my_prefix' }

XML

1
2
3
4
5
6
7
8
9
10
11

<!-- config/services.xml --><services><serviceid="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler"><argumenttype="service"id="Redis"/><!-- you can optionally pass an array of options. The only option is 'prefix', which defines the prefix to use for the keys to avoid collision on the Redis server: <argument type="collection"> <argument key="prefix">my_prefix</argument> </argument> --></service></services>

PHP

1
2
3
4
5
6
7
8
9
10
11

// config/services.phpuseSymfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;$container->register(RedisSessionHandler::class)->addArgument(newReference('Redis'),// you can optionally pass an array of options. The only option is 'prefix',// which defines the prefix to use for the keys to avoid collision on the Redis server:// ['prefix' => 'my_prefix'],);

Next, use the handler_id
configuration option to tell Symfony to use this service as the session handler:

That's all! Symfony will now use your Redis server to read and write the session
data. The main drawback of this solution is that Redis does not perform session
locking, so you can face race conditions when accessing sessions. For example,
you may see an "Invalid CSRF token" error because two requests were made in
parallel and only the first one stored the CSRF token in the session.

If you use Memcached instead of Redis, follow a similar approach but replace
RedisSessionHandler by MemcachedSessionHandler.

Before storing sessions in the database, you must create the table that stores
the information. The session handler provides a method called
createTable()
to set up this table for you according to the database engine used:

1
2
3
4
5

try{$sessionHandlerService->createTable();}catch(\PDOException$exception){// the table could not be created for some reason}

If you prefer to set up the table yourself, it's recommended to generate an
empty database migration with the following command:

1

$ php bin/console doctrine:migrations:generate

Then, find the appropriate SQL for your database below, add it to the migration
file and run the migration with the following command:

A BLOB column type (which is the one used by default by createTable())
stores up to 64 kb. If the user session data exceeds this, an exception may
be thrown or their session will be silently reset. Consider using a MEDIUMBLOB
if you need more space.

MongoDB ODM 1.x only works with the legacy driver, which is no longer
supported by the Symfony session class. Install the alcaeus/mongo-php-adapter
package to retrieve the underlying \MongoDB\Client object or upgrade to
MongoDB ODM 2.0.

That's all! Symfony will now use your MongoDB server to read and write the
session data. You do not need to do anything to initialize your session
collection. However, you may want to add an index to improve garbage collection
performance. Run this from the MongoDB shell: