We're busy tuning Redis and have configured a Unix socket instead of a TCP socket. This new configuration runs fine on the server. Only it seems it's not possible to configure a Redis caching store with a Unix socket in the Moodle Admin Caching configuration and in the Moodle config.php. Isn't this possible? I'm testing Moodle 3.5.5 in a Redhat 7 environment.
Howard I tried to configure this in the config.php, and in the Caching configuration of Redis, but the redis connection expects a server (IP address/hostname) and a port:
Check this code in cache/stores/redis/lib.php
protected function new_redis($server, $prefix = '', $password = '') {
$redis = new Redis();
$port = null;
if (strpos($server, ':')) {
$serverconf = explode(':', $server);
$server = $serverconf[0];
$port = $serverconf[1];
}
if ($redis->connect($server, $port)) {
if (!empty($password)) {
$redis->auth($password);
}
$redis->setOption(Redis::OPT_SERIALIZER, $this->serializer);
if (!empty($prefix)) {
$redis->setOption(Redis::OPT_PREFIX, $prefix);
}
// Database setting option...
$this->isready = $this->ping($redis);
} else {
$this->isready = false;
}
return $redis;
}
Check this code in cache/stores/redis/lib.php
protected function new_redis($server, $prefix = '', $password = '') {
$redis = new Redis();
$port = null;
if (strpos($server, ':')) {
$serverconf = explode(':', $server);
$server = $serverconf[0];
$port = $serverconf[1];
}
if ($redis->connect($server, $port)) {
if (!empty($password)) {
$redis->auth($password);
}
$redis->setOption(Redis::OPT_SERIALIZER, $this->serializer);
if (!empty($prefix)) {
$redis->setOption(Redis::OPT_PREFIX, $prefix);
}
// Database setting option...
$this->isready = $this->ping($redis);
} else {
$this->isready = false;
}
return $redis;
}
If I configure the unix socket path (/var/run/redis.sock) in the Redis Caching configuration I get an error "Redis server went away" when I try to login.
When I change this in 127.0.0.1:0 (also in config.php, the session configuration for Redis) I get the same error.
I read this article, and that's easy to change Redis to listen to a unix socket, but Moodle doesn't seem to understand this configuration: https://guides.wp-bullet.com/how-to-configure-redis-to-use-unix-socket-speed-boost/
When I change this in 127.0.0.1:0 (also in config.php, the session configuration for Redis) I get the same error.
I read this article, and that's easy to change Redis to listen to a unix socket, but Moodle doesn't seem to understand this configuration: https://guides.wp-bullet.com/how-to-configure-redis-to-use-unix-socket-speed-boost/

These are the messages I see in the Apache error_log:
PHP Warning: Redis::connect(): connect() failed: Connection refused in /var/www/moodle/cache/stores/redis/lib.php on line 159
PHP message: PHP Warning: Redis::connect(): connect() failed: Permission denied in /var/www/moodle/lib/classes/session/redis.php on line 171
PHP message: Failed to connect (try 1 out of 5) to redis at /var/run/redis/redis.sock:0
In config.php I changed this:
$CFG->session_redis_host = '/var/run/redis/redis.sock';
$CFG->session_redis_port = 0;
In redis.conf I changed this:
port 0
unixsocket /var/run/redis/redis.sock
unixsocketperm 660
In redis.log I see:
19666:M 07 Aug 14:22:19.504 * The server is now ready to accept connections at /var/run/redis/redis.sock
When I want to login in Moodle I see the error above.
As nobody could answer my question about running Redis via a Unix socket I have stopped to try to get this working in Moodle.
Testing with the benchmark of Redis showed much better performance when using Redis on the same application server of Moodle. Maybe interesting to have a look at this point for a future release of Moodle?
Testing with the benchmark of Redis showed much better performance when using Redis on the same application server of Moodle. Maybe interesting to have a look at this point for a future release of Moodle?
Hi,
The best results for Redis are in a multi frontend scenario, and Unix socket won't be available there, but you can still use it in a single server.
I've configured Redis listening via Unix socket. In Moodle settings I've used '/var/run/redis/redis.sock' (just like in redis.conf file) and it has worked without trouble. I think the clue is setting the redis user to be the same that runs PHP or setting the socket permissions to everybody (777) in redis.conf file so PHP can access read and write. It'd prefer the first option, but for a fast test the second is ok.
I've used Moodle 3.7 but it should work in older versions.
I can use Redis via socket for both cache and session. For session use set port=-1 in config.php. For cache use the socket path and leave port empty. Ensure PHP user has write/read permissions on socket.
This is my conf.
$CFG->session_handler_class = '\core\session\redis';
//$CFG->session_redis_host = '127.0.0.1';
//$CFG->session_redis_port = 6379; // Optional.
$CFG->session_redis_host = '/var/run/redis/redis.sock';
$CFG->session_redis_port = -1; // Optional if TCP. For socket use -1
$CFG->session_redis_database = 0; // Optional, default is db 0.
$CFG->session_redis_prefix = 'sess_'; // Optional, default is don't set one.
$CFG->session_redis_acquire_lock_timeout = 120;
$CFG->session_redis_lock_expire = 7200;
$CFG->session_handler_class = '\core\session\redis';
//$CFG->session_redis_host = '127.0.0.1';
//$CFG->session_redis_port = 6379; // Optional.
$CFG->session_redis_host = '/var/run/redis/redis.sock';
$CFG->session_redis_port = -1; // Optional if TCP. For socket use -1
$CFG->session_redis_database = 0; // Optional, default is db 0.
$CFG->session_redis_prefix = 'sess_'; // Optional, default is don't set one.
$CFG->session_redis_acquire_lock_timeout = 120;
$CFG->session_redis_lock_expire = 7200;