2018-05-15 #laravel

Redis As Session Driver With Laravel

Laravel allows you to use a few different Session drivers.

  • File
  • Memcache
  • Redis
  • Database
  • Cookie
  • Array

You'll notice in this list is Redis and Memcache that we can also use as our Cache Driver. Redis and Memcache are the fastest options to use for your session driver so you'll find a lot of people using this option.

The problem comes when you're using Redis as both the cache driver and the session driver.


By default these are stored in the same Redis database, therefore if you ever clear the cache of your application you will also remove the session information.

Therefore if you have a blog that on publish of a post it will perform a php artisan cache:clear to reset the post data it will also clear all the session data in Redis, which results in all your users being logged out.


To solve this problem we need to tell Laravel there are different Redis connections, one for Cache and one for Session.

First navigate to config/database.php at the bottom of the file you'll see the settings for redis and there will be a default connection. We need to add a new connection, using the same host, password and host but change the database number.

'redis' => [

        'client' => 'predis',

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

        'session' => [
            'host' => env('REDIS_HOST', ''),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 1,


Now we need to tell redis to use this connection as the session. Open up the config/session.php file and find the connection property, then change this to 'session'.

    | Session Database Connection
    | When using the "database" or "redis" session drivers, you may specify a
    | connection that should be used to manage these sessions. This should
    | correspond to a connection in your database configuration options.

    'connection' => 'session',

Now when you clear the cache php artisan cache:clear your users will no longer lose their session data and be forcing them to logout.


Reliable uptime monitoring and instant alerts for any website downtime.

Get 50% off forever using the code 50OFFUP.