PHPackages                             colinmollenhour/cache-backend-redis - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. colinmollenhour/cache-backend-redis

ActiveMagento-module[Caching](/categories/caching)

colinmollenhour/cache-backend-redis
===================================

Zend\_Cache backend using Redis with full support for tags.

1.18.0(3mo ago)38522.9M↑16.9%142[17 issues](https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/issues)[1 PRs](https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/pulls)9BSD-3-Clause-ModificationPHPCI passing

Since Feb 5Pushed 3mo ago43 watchersCompare

[ Source](https://github.com/colinmollenhour/Cm_Cache_Backend_Redis)[ Packagist](https://packagist.org/packages/colinmollenhour/cache-backend-redis)[ Docs](https://github.com/colinmollenhour/Cm_Cache_Backend_Redis)[ RSS](/packages/colinmollenhour-cache-backend-redis/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (39)Used By (9)

This `Zend_Cache` backend allows you to use a Redis server as a central cache storage. Tags are fully supported without the use of `TwoLevels` cache so this backend is great for use on a single machine or in a cluster. Works with any Zend Framework project including all versions of Magento!

FEATURES
========

[](#features)

- Can take advantage of the [phpredis PECL extension](https://github.com/phpredis/phpredis) for best performance if it is installed.
- Falls back to standalone PHP if phpredis isn't available using the [Credis](https://github.com/colinmollenhour/credis) library.
- Tagging is fully supported, implemented using the Redis "set" and "hash" data types for efficient tag management.
- Key expiration is handled automatically by Redis.
- Supports unix socket connection for even better performance on a single machine.
- Supports configurable compression for memory savings. Can choose between gzip, lzf, l4z, snappy and zstd and can change configuration without flushing cache.
- Uses transactions to prevent race conditions between saves, cleans or removes causing unexpected results.
- Supports a configurable "auto expiry lifetime" which, if set, will be used as the TTL when the key otherwise wouldn't expire. In combination with "auto expiry refresh on load" offers a more sane cache management strategy for Magento's `Enterprise_PageCache` module.
- Supports reading from slaves, can be overridden at run time by setting the 'safe\_load' directive.
- Can read connection info from Redis Sentinel for automatic failovers.
- **Unit tested!**

REQUIREMENTS
============

[](#requirements)

This backend uses the [Credis](https://github.com/colinmollenhour/credis) library. Optionally, but for improved performance, you can install [phpredis](https://github.com/phpredis/phpredis) which is a compiled extension and available via pecl and many official packages.

INSTALLATION
============

[](#installation)

Add the package as a dependency to your project with Composer.

```
composer require colinmollenhour/cache-backend-redis
```

### modman

[](#modman)

It is not the recommended method, but you may install via [modman](https://github.com/colinmollenhour/modman):

```
modman clone https://github.com/colinmollenhour/Cm_Cache_Backend_Redis
```

CONFIGURATION
=============

[](#configuration)

These examples assume you are using Magento, but the configuration can just be passed to the constructor as a PHP array with the same key names as seen in the examples.

Edit `app/etc/local.xml` to configure:

```

      Cm_Cache_Backend_Redis

        127.0.0.1
        6379

        0

        0
        1
        10
        0
        1
        1
        20480
        gzip
        0
        500
        300000
        100
        tcp://redis-slave:6379

      Cm_Cache_Backend_Redis

        127.0.0.1
        6379

        1

        0
        1
        57600
        0

```

High Availability and Load Balancing Support
--------------------------------------------

[](#high-availability-and-load-balancing-support)

### Redis Sentinel

[](#redis-sentinel)

You may achieve high availability and load balancing using [Redis Sentinel](http://redis.io/topics/sentinel). To enable use of Redis Sentinel the `server`specified should be a comma-separated list of Sentinel servers and the `sentinel_master` option should be specified to indicate the name of the sentinel master set (e.g. 'mymaster'). If using `sentinel_master` you may also specify `load_from_slaves` in which case a random slave will be chosen for performing reads in order to load balance across multiple Redis instances. Using the value '1' indicates to only load from slaves and '2' to include the master in the random read slave selection.

Example configuration:

```

      Cm_Cache_Backend_Redis

        tcp://10.0.0.1:26379,tcp://10.0.0.2:26379,tcp://10.0.0.3:26379
        0.5
        mymaster
        1
        1

```

### Load Balancer or Service Discovery

[](#load-balancer-or-service-discovery)

It is also possible to achieve high availability by using other methods where you can specify separate connection addresses for the master and slave(s). The `load_from_slave` option has been added for this purpose and this option does *not*connect to a Sentinel server as the example above, although you probably would benefit from still having a Sentinel setup purely for the easier replication and failover.

Examples would be to use a TCP load balancer (e.g. HAProxy) with separate ports for master and slaves, or a DNS-based system that uses service discovery health checks to expose master and slaves via different DNS names.

Example configuration:

```

      Cm_Cache_Backend_Redis

        tcp://redis-master:6379
        tcp://redis-slaves:6379
        0
        0.5

```

### Static Configuration

[](#static-configuration)

You may also statically specify the master and slave servers by passing either an array to `load_from_slave` or a string with multiple addresses separated by a comma.

```

      Cm_Cache_Backend_Redis

        tcp://redis-master:6379
        tcp://redis-slave1:6379,tcp://redis-slave2:6379
        0
        0.5

```

### ElastiCache

[](#elasticache)

The following example configuration lets you use ElastiCache Redis (cluster mode disabled) where the writes are sent to the Primary node and reads are sent to the replicas. This lets you distribute the read traffic between the different nodes.

The instructions to find the primary and read replica endpoints are [here](http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Endpoints.html#Endpoints.Find.Redis).

```

      primary-endpoint.0001.euw1.cache.amazonaws.com
      6379
      0
      1

          replica-endpoint-1.jwbaun.0001.euw1.cache.amazonaws.com
          6379

          replica-endpoint-2.jwbaun.0001.euw1.cache.amazonaws.com
          6379

```

#### DEPRECATION NOTICE

[](#deprecation-notice)

Previously the ElastiCache config instructions suggested setting up a `` node but this functionality was flawed and is no longer supported. The config is still parsed and loaded for backwards-compatibility but chooses a random slave to read from rather than using md5 hash of the keys.

### Dragonfly

[](#dragonfly)

[Dragonfly](https://www.dragonflydb.io/) is a modern Redis-compatible in-memory data store. This backend includes support for Dragonfly with the following considerations:

**Lua Scripts Compatibility**

Dragonfly requires explicit declaration of keys used in Lua scripts. This backend includes the necessary `--!df flags=allow-undeclared-keys` annotations in all Lua scripts to enable compatibility.

**Performance Optimization**

For improved performance, you can run Dragonfly with disabled atomicity for Lua scripts:

```
dragonfly --default_lua_flags="disable-atomicity"
```

> **Warning:** When using `disable-atomicity`, Lua scripts will not execute atomically. Since the Lua scripts in this backend use global keys, there is a possibility of race conditions during concurrent operations. However, the final state should remain consistent. Additional testing in your specific environment is recommended before using this option in production.

TUNING
======

[](#tuning)

- The recommended "maxmemory-policy" is "volatile-lru". All tag metadata is non-volatile, so it is recommended to use key expirations unless non-volatile keys are absolutely necessary so that tag data cannot get evicted. So, be sure that the "maxmemory" is high enough to accommodate all the tag data and non-volatile data with enough room left for the volatile key data as well.
- Automatic cleaning is optional and not recommended since it is slow and uses lots of memory.
- Occasional (e.g. once a day) garbage collection is recommended if the entire cache is infrequently cleared and automatic cleaning is not enabled. The best solution is to run a cron job which does the garbage collection. (See "Example Garbage Collection Script" below.)
- When `use_lua` is enabled, garbage collection uses an iterative SSCAN-based approach to avoid blocking Redis for extended periods. You can tune the performance with:
    - `gc_scan_count` (default: 500) - Number of tag members to scan per iteration. Higher values are faster but block longer.
    - `gc_exists_cache_size` (default: 300000) - Size of local cache to reduce redundant EXISTS checks across tags.
    - `gc_remove_chunk_size` (default: 100) - Number of expired IDs to remove in a batch. Smaller values yield control more frequently.
- Compression will have additional CPU overhead but may be worth it for memory savings and reduced traffic. For high-latency networks it may even improve performance. Use the [Magento Cache Benchmark](https://github.com/colinmollenhour/magento-cache-benchmark) to analyze your real-world compression performance and test your system's performance with different compression libraries.
    - gzip — Slowest but highest compression. Most likely you will not want to use above level 1 compression.
    - lzf — Fastest compress, fast decompress. Install: `sudo pecl install lzf`
    - snappy — Fastest decompress, fast compress. Download and install: [snappy](http://code.google.com/p/snappy/) and [php-snappy](http://code.google.com/p/php-snappy/)
- Monitor your redis cache statistics with my modified [munin plugin](https://gist.github.com/1177716).
- Enable persistent connections. Make sure that if you have multiple configurations connecting the persistent string is unique for each configuration so that "select" commands don't cause conflicts.
- Increase your server's `lua-time-limit` if you are getting "BUSY" errors. This setting can also cause Redis Sentinel to invoke fail-overs when you would probably prefer to let the Lua script finish and have clients wait a little longer.
- Use the `stats.php` script to inspect your cache to find over-sized or wasteful cache tags.

### Example Garbage Collection Script (Magento)

[](#example-garbage-collection-script-magento)

```
