Redis caching and unix socket configuration

Redis caching and unix socket configuration

Alain Raap -
回帖数:14
Particularly helpful Moodlers的头像

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.

回复Alain Raap

Re: Redis caching and unix socket configuration

Howard Miller -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像
I've never tried it but the redis extension certainly allows connection via a socket. Specify the path to the socket in place of the hostname.
回复Howard Miller

Re: Redis caching and unix socket configuration

Alain Raap -
Particularly helpful Moodlers的头像
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;
}
回复Alain Raap

Re: Redis caching and unix socket configuration

Howard Miller -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像
Maybe I'm being stupid - but I'm missing your point here. The code splits the server/port if the string contains a colon but other than that it just seems to pass '$server' to the $redis->connect(). That allows a path for $server.
回复Howard Miller

Re: Redis caching and unix socket configuration

Alain Raap -
Particularly helpful Moodlers的头像
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/
回复Alain Raap

Re: Redis caching and unix socket configuration

Howard Miller -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像
Is that because the socket string isn't making it to the connect() or something else?

You're assuming that Moodle doesn't understand the configuration but I'd want proof of that.
回复Howard Miller

Re: Redis caching and unix socket configuration

Alain Raap -
Particularly helpful Moodlers的头像
error MoodleMaybe I say it wrong, with parameters server and socket I can't make a connection with Redis:

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.



回复Alain Raap

Re: Redis caching and unix socket configuration

Howard Miller -
Core developers的头像 Documentation writers的头像 Particularly helpful Moodlers的头像 Peer reviewers的头像 Plugin developers的头像
Can you connect using redis-cli?
回复Howard Miller

Re: Redis caching and unix socket configuration

Alain Raap -
Particularly helpful Moodlers的头像
Yes, Redis works fine on a unix socket, this is the redis-cli output:

redis-cli -s /var/run/redis/redis.sock
redis /var/run/redis/redis.sock> ping
PONG
redis /var/run/redis/redis.sock>
回复Alain Raap

Re: Redis caching and unix socket configuration

Alain Raap -
Particularly helpful Moodlers的头像
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?
回复Alain Raap

Re: Redis caching and unix socket configuration

Juan Carrera -

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.

回复Juan Carrera

Re: Redis caching and unix socket configuration

Alain Raap -
Particularly helpful Moodlers的头像
Juan, where did you configure Redis, in the configuration of the Plugins -> Caching server?


And did you use Redis for session caching as described in config-dist.php? I couldn’t configure that part of the caching, that caused the message I mentioned earlier.

回复Alain Raap

Re: Redis caching and unix socket configuration

Juan Carrera -
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;


回复Juan Carrera

Re: Redis caching and unix socket configuration

Alain Raap -
Particularly helpful Moodlers的头像
Thanks very much Juan, I’m going to try these configuration settings next week!
回复Juan Carrera

Re: Redis caching and unix socket configuration

Alain Raap -
Particularly helpful Moodlers的头像
Juan, I've got it working too, with the permissions 666 on the Redis socket it worked for me. I also had to make a Selinux policy to be able to connect to the Redis socket, this was denied by Selinux. Thanks for your advice.