Relay

For much faster cache performance the Relay PHP extension can be used to store the cache data inside PHP’s shared memory pool. Using it with Object Cache Pro is as easy as changing the client configuration option.

There are however a few important things to know:

  1. Understand the multitenancy limitations
  2. Using compression and igbinary helps to reduce Relay memory usage
  3. Experiment with prefetching and alloptions splitting

Multitenancy limitations

With Relay enabled, running multiple WordPress sites using a single Redis instance has some limitations, which will cause unnecessary flushing of Relay’s memory.

To bypass most of them it is vital to:

  1. Use a dedicated database for each WordPress site
  2. As well as using a unique prefix for each site

Unfortunately, due to Redis’ design, Relay isn’t aware of the database index when FLUSHDB is called and thus will flush it’s entire memory.

If you’re curious, read the technical details.

Relay configuration

define('WP_REDIS_CONFIG', [
    'token' => '...',
    'host' => '127.0.0.1',
    'port' => 6379,

    // use `relay` extension
    'client' => 'relay',
    
    // avoid unnecessary flushing
    // change `3` and `db3` for each site
    'database' => 3,
    'prefix' => 'db3',

    // whether multiple WordPress sites use the Redis instance
    'shared' => true,

    // reduce memory usage
    'compression' => 'zstd',
    'serializer' => 'igbinary',

    // experiment with these, some sites may benefits some won't
    'prefetch' => false,
    'split_alloptions' => false,

    // reduces memory footprint in production
    'debug' => false,
    'save_commands' => false,
]);

Client-only

Relay can be used in client-only mode without using the in-memory caching.

define('WP_REDIS_CONFIG', [
    'relay' = [
        'cache' => false,
    ],
]);

Adaptive caching

Relay supports adaptive caching that prevents caching of write-heavy keys by requiring a read-write ratio for keys to be cached. This can improve performance significantly for sites that suffer from cache thrashing, like many WordPress sites do.

define('WP_REDIS_CONFIG', [
    'relay' = [
        'adaptive' => [
            // (integer) Number of horizontal cells. Ideally this should scale
            // with the number of unique keys in the database. Supported values: 512 - 2^31.
            'width' => 100_000,

            // (integer) Number of vertical cells. Supported values: 1 - 8.
            'depth' => 6,

            // Minimum number of events (reads + writes) before Relay
            // will use the ratio to determine if a key should remain cached.
            // Using a negative number will invert this and Relay won't cache
            // a key until its seen at least that many events for the key.
            'events' => 10,

            // (float) Minimum ratio of reads to writes of a key to remain
            // cached (positive events) or be cached (negative events).
            'ratio' => 5.0,
        ],
    ],
]);