Introduction
Relay is a PHP extension that is a drop-in replacement for PhpRedis as well as a shared in-memory cache like APCu.
It allows PHP to interact with Redis just like PhpRedis does, however all retrieved keys are also held in PHP’s memory, which is shared across all FPM workers.
That means if Worker 1 fetches the users:count key from Redis, Worker 2, 3, … can instantaneously retrieve that key from Relay, without having to communicate with Redis.
$redis = new Relay;
$redis->connect('127.0.0.1', 6379);
// Only retrieve key from Redis, if it isn’t in Relay’s memory
$users = $redis->get('users:count');
Serving reads from local memory instead of over the network makes them dramatically faster — Relay’s benchmarks show cache reads up to 100× faster than going to Redis — while cutting bandwidth to a bare minimum. Because this partial replica lives in the PHP master process and uses a multi-threaded, lock-free design, every worker can read from it concurrently without blocking. A single node can serve millions of requests per second, sidestepping the single-threaded bottleneck that often pushes Redis deployments into extra replication or clustering.
To prevent its cache from going stale, Relay uses server-assisted client side caching to actively invalidate its in-memory cache. Meaning, Relay will instantaneously know when the dataset on Redis changes and invalidate its local cache.
$redis = new Relay('127.0.0.1', 6379);
// Retrieve key from Relay’s memory
$users = $redis->get('users:count'); // 42
// Relay will instantaneously delete its copy of `users:count`
shell_exec('redis-cli SET users:count 47');
// This call will retrieve the key from Redis (not Relay)
$users = $redis->get('users:count'); // 47
Furthermore, Relay offers event listeners to update your application runtime caches mid-request, or prevent data in long-running processes from going stale.
$redis = new Relay('127.0.0.1', 6379);
App::setUserCount($redis->get('users:count'));
$redis->onInvalidated(function ($event) {
if ($event->key === 'users:count') {
App::setUserCount(null);
}
});
To use Relay as a faster alternative to PhpRedis/Predis, in-memory caching can of course be disabled globally or per instance.
Continue to the installation instructions.