Using Heroku Redis with Laravel 5

Note: This guide was written for Laravel 5 and may be outdated or require updates.

Recently, I moved a Laravel 5 application to Heroku. The application’s previous host allowed access to the file system but Heroku uses an “ephemeral” file system, making it unsuitable for storing sessions, cache data etc.

With no access to the file system, it means an alternative solution is needed to store this data on Heroku. In the end, I settled on Redis using Heroku’s own add-on.

Laravel almost supports Redis out of the box. It can be configured in the database configuration file, but you first need to install the Redis PHP client via Composer:

$ composer require predis/predis

If you open Laravel’s database configuration file at config/database.php and scroll to the bottom, you’ll see a section with the redis key. Depending on the version of Laravel you’re using, the values will either be hard-coded or fetched from environment variables. To use Redis with Laravel on Heroku, we need to use environment variables, so re-write the Redis block to look like this if it doesn’t already:

'redis' => [

    'cluster' => false,

    'default' => [
        'host' => env('REDIS_HOST'),
        'port' => env('REDIS_PORT'),
        'password' => env('REDIS_PASSWORD'),
        'database' => 0,


If you haven’t already, add the Heroku Redis add-on to your Heroku application. Choose the plan that is most appropriate for your application size.

Once installed, you’ll notice your Heroku application has a new environment variable: REDIS_URL. Unfortunately, this isn’t what Laravel is expecting.

Back in database.php, at the top of the file, before the config array is opened, we need to do a little bit of logic to parse the Redis configuration URL into the environment variables Laravel is expecting. Add the following after the opening <?php tag, and before the array is opened:

if (getenv('REDIS_URL')) {
    $url = parse_url(getenv('REDIS_URL'));


This checks if REDIS_URL is defined as an environment variable and if so, parses the URL and places the relevant components into new environment variables.

Once committed and deployed, your Laravel application will be able to connect to Redis, which you can use as your session and/or cache driver, or for any other ad hoc needs.