Skip to content

June 23, 2015

Configuring Magento 2 To Use Redis Cache Backend

Using the file-system for a cache backend on a production Magento site is just asking for your site to run slowly. But what about development? Should you use a file-system based cache there or not? Well, that is ultimately up to you, but you’ll be able to work faster if you develop on a stack with a fast cache backend. My personal favorite is currently Redis. Magento supports this out of the box in later versions of Magento 1 as well as in the developer betas of Magento 2. There are added benefits such as not constantly having a slew of cache files updating and triggering backups.

I’ll assume you already have redis installed on your system, but if not, go ahead and install that on Mac OS X with a simple brew install redis or use the appropriate package manager of your preferred OS. You may want to comment out all “save” directives in the redis.conf file. Being a dev environment, I don’t care about writing the cache data to disk, and if I reboot…let it start with a clean cache database.

So how do you configure Magento 2 to use Redis? When I started digging into Magento 2 development, I couldn’t really find much on this. It’s even changed a few times since I first spent the time to figure it out. By now though, this shouldn’t change much by the time the GA hits later this year.

In your app/etc/env.php file you should find a large PHP array with settings similar to the well-known app/etc/config.xml from Magento 1. It doesn’t matter where you put the settings in here, just as long as they only appear once, and result in a syntactically correct PHP array declaration. I like to add them directly above the cache_types index.

Here is the configuration I tend to use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  'cache' => 
  array (
    'frontend' => 
    array (
      'default' => 
      array (
        'backend' => 'Cm_Cache_Backend_Redis',
        'backend_options' => 
        array (
          'server' => '127.0.0.1',
          'port' => '6379',
          'persistent' => '',
          'database' => '0',
          'force_standalone' => '0',
          'connect_retries' => '1',
          'read_timeout' => '10',
          'automatic_cleaning_factor' => '0',
          'compress_data' => '1',
          'compress_tags' => '1',
          'compress_threshold' => '20480',
          'compression_lib' => 'gzip',
        ),
      ),
      'page_cache' => 
      array (
        'backend' => 'Cm_Cache_Backend_Redis',
        'backend_options' => 
        array (
          'server' => '127.0.0.1',
          'port' => '6379',
          'persistent' => '',
          'database' => '1',
          'force_standalone' => '0',
          'connect_retries' => '1',
          'read_timeout' => '10',
          'automatic_cleaning_factor' => '0',
          'compress_data' => '0',
          'compress_tags' => '1',
          'compress_threshold' => '20480',
          'compression_lib' => 'gzip',
        ),
      ),
    ),
  ),

If you have this setup on all your dev sites, a simple call to redis-cli flushall will clear all your caches. Handy tools like n98-magerun or bin/magento cache:flush --all in Magento 2 will also do the trick.

Read more from Magento

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments