Sessions in PHP 7.1 and Redis

In case you have missed it, PHP 7.1.0 has been released recently. Now you can’t wait to upgrade your servers to the latest and greatest PHP version ever. But hold that thought a second…
With PHP 7 lots of things have changed underneath the hood. But these changed features can also put unexpected challenges on your path.

Our challenge

One of these challenges that we faced was getting PHP 7.1 to play nice storing sessions in our Redis storage. In order to store sessions in Redis, we needed to install the Redis PHP extension that not only provides PHP functions for Redis, but also installs the PHP session handler for Redis.
Because we upgraded our servers to PHP 7.1, we were looking to use the latest provided version for this Redis extension: redis-3.1.0. Once installed, we bumped against a nasty problem.
Warning: session_start(): Failed to read session data: redis (path: tcp://
Searching the internet for this error, we didn’t got many hits that could point us into a direction of a solution. 
We came up with this very basic PHP code snippet that could reproduce the error in a clean, stand-alone way. Since we used a framework, we wanted to make sure the problem was in PHP and not related to the choice of our PHP framework.

ini_set('session.save_handler', 'redis');
    'session.save_path', 'tcp://devcache1:6379'

echo nl2br(
    '<pre>' . session_save_path() . '</pre>' . PHP_EOL

echo nl2br(
    'Running PHP version: ' . phpversion() . PHP_EOL

if (!array_key_exists('visit', $_SESSION)) {
    $_SESSION['visit'] = 0;

echo nl2br(
    'You have been here ' . $_SESSION['visit'] . ' times.'
To give us some room to try things out, we decided to use Docker to create containers for us to try things out while searching for a solution. Find our basic setup from my GitHub repository.
cd /path/to/workspace
git clone
cd docker-php-redis-example/
We’ve added a docker-compose.yml to get started quickly using Docker Compose.
docker-compose up
This command will launch the different containers for PHP 5.6, 7.0 and 7.1 including a single Redis instance.
In the following YouTube video you can see what we experienced on our side.
PHP7, Redis extension and Docker
The redis PHP session handler worked perfect for PHP 5.6, failed with Segmentation faults on PHP 7.0 and wasn’t able to initialise a new session on PHP 7.1. Remarkable was that once we started a session on PHP 5.6, the session handler was having no issues updating this existing session on PHP 7.1.

Our solution

So we figured out there must be something broken with the update in the redis extension. Looking at the changelog we couldn’t directly spot a problem on how sessions are instantiated. Searching the internet we came to a lot of mentioning of session_regenerate_id(), but this was not affected in our situation.
In one of the comments someone mentioned that a similar failure started appearing after upgrading from v3.0.0 to v3.1.0. That sparked our interest. We downgraded our redis extension to version 3.0.0 and tried again.
Hooray!!! Our session creation was now working on PHP 7.1, so mission accomplished. Now we needed to agree on taking the unfixed issues between redis-3.0.0 and redis-3.1.0 as compromise. Since the earlier mentioned changelog didn’t contain issues that directly could have an effect on our use cases for Redis it was easy to agree.
Now we’re enjoying running our web applications on PHP 7.1.0 while our sessions are handled through Redis. And we hope by sharing this experience, you too can enjoy running your PHP application on the latest version: PHP 7.1.

Update 2016-12-22!!!

Today I received a tweet that there's an update available in the form of redis-3.1.1RC1. Go check out my github branch to see how to implement it with Docker.

This fix also solves the issue I displayed for PHP 7.0. You should be good to go and implement it on your PHP 7 installations.


Post a Comment

Popular Posts