16

While I could use memcached on Debian to use the default 11211 port, but I've had great difficulty setting up Unix socket.

From what I've read, I know that I need to create a memcache.socket and add:

-s /path/to/memcache.socket
-a 0766

To /etc/memcached.conf and comment out the default connection port and IP, i.e.

-p 11211 
-l 127.0.0.1

However, when I restart memcached I get internal server errors on Drupal site.

I'm trying to implement unix sockets to avoid TCP/IP overhead and boost overall memcached performance, however not sure how much performance gain one can expect of this tweak.

I appreciate your hints or possibly configs to to resolve this.

3
  • Assuming memcached is running, did you configure Drupal to use the socket?
    – cjc
    Sep 29, 2012 at 12:49
  • Yes, Drupal has been set up to use memcached and works properly when I use port 11211.
    – alfish
    Sep 29, 2012 at 13:17
  • Is the /path/to/ fully readable/executable by the user running Drupal? I don't mean the socket file itself; the Drupal user will also need to be able to descend the directory hierarchy.
    – cjc
    Sep 29, 2012 at 13:28

5 Answers 5

13

You may find that just setting the socket path doesn't work. Memcached drops privileges before it creates its socket, though after it's written its PID. It's common to put the socket in /var/run (e.g. as mysql does), but only root can write there, so create /var/run/memcached and chown it to nobody, then set /var/run/memcached/memcached.sock as the socket path. Writing it to /tmp would work too, but by its very nature that could get trashed; sticking it in /var/run is a bit more permanent.

You can check it's working by using netcat to connect to it:

nc -U /var/run/memcached/memcached.sock

Just type stats at the blank prompt; if it's working you'll get a load of output.

1
  • 1
    This worked for me, rather than the other answer. Obviously nobody can't write to a root owned folder w/o the appropriate permission.
    – Daniel W.
    Feb 10, 2014 at 10:15
7

I believe the socket will need to be world executable, not writable. If your seeing a PHP notice message containing errno=32 Broken pipe, adjust Memcached access mask to 0755

-s /path/to/memcache.socket
-a 0755

Also verify that Drupal's Memcached daemon has TCP port of 0 in the configuration. An unset port will default to 11211, and confuse the socket connection.

$conf += array(
    'memcache_servers' => array(
        'unix:///path/to/memcache.socket:0'  => 'default',
    )
);
1
  • 1
    I believe the mask should be 766 or 666 as you need read/write.
    – arleslie
    Jan 12, 2016 at 3:29
7

added to /etc/default/memcached:

[ ENABLE_MEMCACHED = yes ] && install -d -o memcache -g www-data -m 0770 /var/run/memcached
# or use setfacl and whatever user/group you want
true # because set -e in init-scripts

added to /etc/memcached.conf:

-s /var/run/memcached/memcached.sock
-a 0666
1

I benchmarked the performance boost of using memcached unix sockets, it is 33% faster to use unix sockets in my tests using the php cli.

I found that it is important to add the memcache user to the www-data group and give the unix socket 775 permissions, this way both the php handler and memcache user can execute it.

You can find the benchmarks and write-up here - although it is for WordPress it should work for Drupal as it is very likely a permissions issue.

0

Note if you are using an alpine container in Kubernetes the pod spec with health probes would look something like this:

spec:
  containers:
    - name: memcached
      image: memcached:1.5-alpine
      imagePullPolicy: "IfNotPresent"
      command:
      - memcached
      - --unix-socket=/tmp/memcached.sock
      - --unix-mask=0766
      - -m 64
      - -o modern
      - -v
...
      livenessProbe:
        exec:
          command:
          - /bin/sh
          - -c
          - 'echo "stats slabs" | nc local:/tmp/memcached.sock'
        initialDelaySeconds: 30
        timeoutSeconds: 5
      readinessProbe:
        exec:
          command:
          - /bin/sh
          - -c
          - 'echo "stats slabs" | nc local:/tmp/memcached.sock'
        initialDelaySeconds: 5
        timeoutSeconds: 1

Took a while to figure out the right settings, and how to connect to a unix local domain socket via busybox.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .