Separate Redis Store in Laravel

Credit to https://www.techalyst.com/

Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, and sorted sets.

In this article, I will show how to separate the Redis configuration for cache, session and queue. Why separate? For me, its :-

  • Easier in terms of application debugging
  • Less data losses
  • More application control in-memory storage
  • Multiple connections
  • Best production oriented configuration.

Laravel have out-of-the-box Redis configuration which give you freedom to assign your connection and storage for your application.

Here is the default connection configuration in

The problem is the default session setting all goes to default redis connection which is database 0. The moment when you run in the redis for DB 0, all the data will be gone.

Great about Redis is there is a way to partition data logically. The default database is 0 but you can change that to any number from 0–15 or more depend on your setting. Each database provides a distinct keyspace, independent from the others.

Lets reconfiguring to separate the Redis connection

For example, I want to separate session, queue, cache and horizon.

So, every config is the same except the database number. You can give any number from 0 to hundreds. Your choice. If you want each of it to have different Redis server, all you need is to change the host directly to the file or using file

Reconfigure Cache Config

If you look into the , there is default stores array defined for the use of Laravel cache services. By default, Laravel set the cache driver to file which its bad for multiple server because file only stored in current specific project directory and unable to be shared with other server.

Change the in .env to . If you see at the stores array, Laravel already point the Redis connection to cache database (DB 1). So, we don’t need to change the connection.

For me, I want to use different store for different cache for my logic. So i add another store named and the connection point to

So, i simply can access using or just create an helper method for easier access

Reconfigure Session Config

Look at the ,

Change the in .env to . By default, Laravel set the session driver to file which its bad for multiple server under load balancer lead to session loss or intermittent.

Ok now, there is 2 way either change the or change the value

More easier just change the connection value to redis connection (DB 2)

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

Reconfigure Queue Config

Look at the

Change the in .env to and it will automatically refer to redis setting in arrays belows

But, the connection is point to default redis connection. Change the value to the .

Now you can run your queue without loosing data if you accidentally flush default redis (DB 0)

Reconfigure Horizon Config

If you are using Laravel Horizon,

Just simply change the “use” to “horizon” connection that we assign earlier

Ok now, you can verify it by accessing the redis server

Its will show the list of your key value have been stored!

Thats it. Thank for your time guys. Just my 2 cent. Just ping me if there is a way for best to do it. See ya~~

Software Engineer at Teratotech.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store