You’re viewing the documentation for Object Cache Pro 2.x, which is currently a release candidate. View the 1.x documentation.
Relay
Relay is a next-generation PHP extension that keeps a partial, in-memory replica of your Redis data inside PHP’s shared memory pool, delivering up to 100× faster cache reads with near-zero network traffic. Its lock-free, multi-threaded design avoids the single-threaded bottlenecks of Redis and lets every PHP worker read cached data concurrently without round-tripping over the network.
Because Relay is a drop-in replacement for PhpRedis with server-assisted client-side caching and active invalidation, switching Object Cache Pro over is as simple as changing the client configuration option — no other code changes required. It collapses the boundary between APCu and PhpRedis into a single layer, and its adaptive caching can automatically avoid thrashing keys that hurt WordPress performance.
There are, however, a few important things to know:
- Understand the multitenancy limitations
- Use compression and igbinary to reduce Relay memory usage
- Experiment with prefetching and
alloptionssplitting
When optimizing Relay for memory usage, prefer zstd compression with igbinary over lz4 for a better compression ratio.
Relay requires Redis 6.2.7 or newer, or Valkey. With older Redis versions, Relay’s in-memory cache may go stale.
Installation
Relay can be installed using PIE, Homebrew or its APT and YUM repositories. Refer to the Relay installation docs for adding the repositories, Docker and manual installations.
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:
- Use a dedicated
databasefor each WordPress site - Set
sharedtoredis-server
With relay.listeners enabled, also include the database number in each site’s prefix, such as db3.
Unfortunately, due to Redis’ design, Relay isn’t aware of the database index when FLUSHDB is called and thus will flush its entire memory.
If you’re curious, read the technical details.
Relay Configuration
define('OBJECTCACHE_CONFIG', [
'token' => '...',
'host' => '127.0.0.1',
'port' => 6379,
// use `relay` extension
'client' => 'relay',
// avoid unnecessary flushing
// change `database` and `prefix` for each site
'database' => 3,
'prefix' => 'ncc-1701-d',
// whether multiple WordPress sites use the Redis instance
'shared' => 'redis-server',
// reduce memory usage
'compression' => 'zstd',
'serializer' => 'igbinary',
// experiment with these, some sites may benefit, 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('OBJECTCACHE_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('OBJECTCACHE_CONFIG', [
'relay' => [
'adaptive' => [
// (integer) Number of horizontal cells. Ideally this should scale
// with the number of unique keys in the database. Supported values: 512 - 2147483647.
'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 it's 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,
],
],
]);
Table
Relay ships with Table, a key-value store that lives in PHP-FPM’s shared memory. Object Cache Pro can use it as the object cache without Redis.
define('OBJECTCACHE_CONFIG', [
'client' => 'relay-table',
'prefix' => 'site1', // unique for each site
]);
Keep in mind:
- The cache is lost when PHP-FPM restarts and isn’t shared between servers.
- Keys aren’t evicted. Once
relay.maxmemoryis full, new keys aren’t stored. - Without a
prefix, flushing clears the cache of all sites on the same PHP-FPM.
WP-CLI
Command-line PHP can’t access PHP-FPM’s shared memory, so WP-CLI is blocked unless the proxy option points to the site’s PHP-FPM pool:
define('OBJECTCACHE_CONFIG', [
'client' => 'relay-table',
'proxy' => 'unix:///run/php/php-fpm.sock', // or `tcp://127.0.0.1:9000`
]);
The proxy requires:
- An
AUTH_KEYwith at least 32 characters - The Relay extension in the PHP-FPM pool that
proxypoints to (WP-CLI’s PHP doesn’t need it) - WP-CLI running on the same server or container as PHP-FPM
- Access to the PHP-FPM socket for the user running WP-CLI
Anyone who can connect to PHP-FPM can run any PHP, so never use listen.mode = 0666 and keep tcp:// addresses on loopback or a private network.
Through the proxy, the watch, analytics, analytics-count, groups, prune-queries, slowlog, slowlog-reset, commands and commands-reset commands are not available.
WP-Cron
Running php wp-cron.php stays blocked. When DISABLE_WP_CRON is set, trigger cron over HTTP instead:
* * * * * curl -fsS https://example.com/wp-cron.php?doing_wp_cron