Redis - is an open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.

Redis has a very small memory footprint and CPU requirements. It can run in small devices like the Raspberry Pi Zero without impacting the overall performance, using a small amount of memory while delivering good performance for many use cases.

Based on information from Redis official website. The benchmarks were performed on Raspberry Pi 3:

  • Test 1: 5 millions writes with 1 million keys (even distribution among keys). No persistence, no pipelining. 28,000 ops/sec.
  • Test 2: Like test 1 but with pipelining using groups of 8 operations: 80,000 ops/sec.
  • Test 3: Like test 1 but with AOF enabled, fsync 1 sec: 23,000 ops/sec
  • Test 4: Like test 3, but with an AOF rewrite in progress: 21,000 ops/sec

The benchmark above is referring to simple SET/GET operations, which are used in cache operations.

I assume you have Redis installed on your system, if not, please follow this article to know how to install and configure Redis in a docker container.

PHP modules

Before using a Redis cache with Laravel, you will need to either install the PhpRedis PHP extension via PECL or install the predis/predis package (~1.0) via Composer. phpredis yield better performance for applications that make heavy use of Redis, so I highly recomment to user phpredis instead of predis.

Installing phpredis

Installing from pecl/pickle

If you use PHP >= 7.3, and I hope you do, you can use pickle.

$ pickle install redis

otherwise, use pecl

$ pecl install redis

Most distributions provides pre-build binary packages of this extension. You can follow this instructions for installing phpredis for your OS.

Laravel Configuration

First of all, we need to configure connection to our redis server, it can be done via config/database.php. There are two connections under redis key: default and cache. We are interested in second one.

'redis' => [ 
    'client' => env('REDIS_CLIENT', 'phpredis'),
 
    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD'),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_CACHE_DB', 1),
    ], 
],

Default settings for cache key work well, but you may need to change host and/or password if your redis server running remotely and requires authentification. To change redis host name or password, open .env file in the root folder of your application and add/change it as follow:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=redis_password

Now take a look at client key, there is a place to tell Laravel which library you use to connect to Redis.

database is another key you may need to change. This is a Redis "database name". Redis database is not the same as a database name in sql databases. It is a way to isolate keys, and only provides index based naming.

By default, Redis has 0 - 15 indexes for databases, you can change that number databases NUMBER in redis.conf

And then you use SELECT command to select the database you want to work with.

Now, we need to tell Laravel which cache driver we want to use for cache. Open config/cache.php

'default' => env('CACHE_DRIVER', 'file'),

and change CACHE_DRIVER to redis in your .env file.

At this point you are ready to use Redis as a cache engine, but for busy and high traffic load environment I highly recomment to read "Tuning Redis for best performance" article.