Use Memcached with Unix sockets for caching

The official documentation recommends using Redis for caching like this:

'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Redis',
'redis' => [
     'host'     => '/run/redis/redis-server.sock',
     'port'     => 0,
     'dbindex'  => 0,
     'password' => 'secret',
     'timeout'  => 1.5,
],

I have Memcached installed and running. It’s configured to use Unix sockets instead of TCP connections because it’s much faster. How can I use Memcached instead of Redis in the configuration from above ? I know that the same documentation recommends using Memcached like this:

'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Memcached',
'memcached_servers' => [
     [ 'server0.example.com', 11211 ],
     [ 'server1.example.com', 11211 ],
     [ 'server2.example.com', 11211 ],
 ],

but as I mentioned, I’m using Unix sockets (/var/run/memcached/memcached.sock), not TCP connections. I have good reasons not to use Redis. I’m already using Memcached for WordPress object caching with great success, and I also want to use it for Nextcloud. I also don’t use APCu for local caching because I had a bad experience with APCu. Could I use Memcached as memcache.local too ? Thank you !

Not sure if the question is “can I use memcached with unix socket on nextcloud”, but I will answser this one here anyway :wink:

Yes you can use a Memcached listening on a Unix Socket with the following configuration :

'memcached_servers' => [
     [ '/path/to/your/memcached.sock', 0 ],
],

Since Nextcloud is directly using the PHP \Memcached class, the documentation to describe a server is here : https://www.php.net/manual/en/memcached.addserver.php

Also, I’m using this socket-based memcached both for memcache.local and memcache.distribnuted as below without any problem (on NC 21):

  'memcached_servers' => [
      [ '/tmp/ben.memcached.sock', 0 ],
  ],
  'memcache.local' => '\OC\Memcache\Memcached',
  'memcache.distributed' => '\OC\Memcache\Memcached',

1 Like

Thank you very much octopuce for your reply ! I almost lost hope on getting an answer on this topic. I’m so glad that you have been able to use Memcached with Unix sockets for Nextcloud. Those who don’t use Memcached don’t know what they are loosing. If it worked for you, it should also work for me.
However, I’m still on Nextcloud 20 and it didn’t work with the settings specified above. I get the following error in my web server’s error log (I use Nginx and PHP 7.3, on a Debian 10 VPS):

PHP Fatal error:  Uncaught OC\HintException: [0]: Memcache \OC\Memcache\Memcached not available for distributed cache (Is the matching PHP module installed and enabled?)    thrown in /var/www/cloud.mydomain.com/lib/private/Memcache/Factory.php on line 113" while reading response header from upstream, client: 1.2.3.4, server: cloud.mydomain.com, request: "GET /ocs/v2.php/apps/notifications/api/v2/notifications HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php7.3-fpm.sock:", host: "cloud.mydomain.com

My Memcached server is working fine and I can see it stores data from my WordPress websites which are really fast because of this cache. I installed Memcached standard from the Debian repository, and my relevant Memcached configuration in /etc/memcached.conf is:

-d
logfile /var/log/memcached.log
-m 512
-u memcache
-s /var/run/memcached/memcached.sock
-a 770
-c 2048
-P /var/run/memcached/memcached.pid

Anyway, I’ll update Nextcloud to version 21 and I hope it will work with the new version.

I finally got it to work in Nextcloud 22. The problem was that I didn’t have the php-memcached package installed. To sum up for anyone interested in using the Memcached server for both local and distributed caching in Nextcloud 22 (on Debian 11, with Nginx), here is what you have to do:

  • First install Memcached like this:
apt-get install memcached libmemcached-tools php-memcached
  • Then configure Memcached by opening /etc/memcached.conf and adding the following settings:
-d
logfile /var/log/memcached.log
-m 512
-u memcache
-s /var/run/memcached/memcached.sock
-a 770
-c 2048
-P /var/run/memcached/memcached.pid
  • Next, add the following block in your Nextcloud config.php file, right below the 'installed' => true, line:
  'memcache.local' => '\OC\Memcache\Memcached',
  'memcache.distributed' => '\OC\Memcache\Memcached',
  'memcached_servers' => [
     [ '/var/run/memcached/memcached.sock', 0 ],
  ],
  • Change ownership and permissions for the /var/run/memcached directory, to allow the web server user, www-data, to write to the socket file inside it.
chown -R memcache:www-data /var/run/memcached
chmod 2750 /var/run/memcached
  • Finally, to avoid permission problems for the Memcached socket file, /var/run/memcached/memcached.sock, which will become unwritable by www-data after restarting the server, create the following small script, to add the right permissions at every server boot up. You can call it change-memcached-permissions and place it in a proper location, like in /srv/scripts or /opt:
#!/bin/bash

sleep 7
chown -R memcache:www-data /var/run/memcached
chmod 2750 /var/run/memcached
  • Add the following cron job, to run the script from above at every server boot up:
# Change ownership and permissions for /var/run/memcached at every reboot:
@reboot /srv/scripts/change-memcached-permissions
  • Finally, restart Memcached and php7.4-fpm
systemctl restart memcached
systemctl restart php7.4-fpm

Thank you octopuce for showing how to use Memcached with Unix sockets. I wish the official Nextcloud documentation mentioned the sample lines needed to add Memcached caching using Unix sockets instead of TCP connections, since everyone knows that connections on Unix sockets run much faster than on TCP.