How to Configure Redis to Use Unix Socket Speed Boost

Redis is a high speed caching system for storing objects in RAM. It works well with WordPress and WooCommerce as an object cache for storing transients so they are not stored in the MySQL database. On WooCommerce particularly this can help a lot because of the number of transients used by plugins, orders and shoppers.

Unix sockets operate at a lower level OSI model layer than TCP sockets so they should be faster.

When I ran some basic Redis unix socket benchmarks I found the results quite surprising – unix sockets were 25% faster than TCP sockets for Redis.

These are microsecond benefits, but why not use Redis unix sockets if we can get some extra performance boost?


Configure Redis with Unix Socket

Modify your redis user under which your Redis server daemon should be running.

Test which user Redis is running as with this command

sudo ps aux | grep redis

You should see

redis       78  0.0  0.0  31360  6800 ?        Ssl  Aug24  61:13 redis-server *:6379

We are going to make the redis user a member of the www-data group which Apache, nginx, php5-fpm and php7.0-fpm run as by default on Debian and Ubuntu systems.

sudo usermod -g www-data redis

Create your redis folder that the unix socket will be in.

sudo mkdir -p /var/run/redis/

Set the permissions so the redis user and www-data group own it

sudo chown -R redis:www-data /var/run/redis

Open your Redis configuration

sudo nano /etc/redis/redis.conf

Add the unix socket path and the permissions 775 so the redis user and www-data group can execute the redis unix socket.

# create a unix domain socket to listen on
unixsocket /var/run/redis/redis.sock
# set permissions for the socket
unixsocketperm 775
#requirepass passwordtouse
daemonize yes
stop-writes-on-bgsave-error no
rdbcompression yes
# maximum memory allowed for redis
maxmemory 50M
# how redis will evice old objects - least recently used
maxmemory-policy allkeys-lru

Ctrl+X, Y and Enter to Save and Exit

Restart Redis

sudo service redis-server restart

See if the Redis unix socket was created

ls -lh /var/run/redis

And there we go

total 0
srwxr-xr-x 1 redis www-data 0 Sep 10 17:00 redis.sock

Now you have successfully reconfigured your service to use the Redis socket 🙂

14 thoughts on “How to Configure Redis to Use Unix Socket Speed Boost”

  1. Hi Mike,

    I’m a wannabe-sysadmin 😉 trying to install ownCloud. I’ve managed to setup REDIS, from the command line, and wanted to optimize following your procedure. Unfortunately, it seems the installation didn’t create the “redis” user, so the “ps aux | grep redis” command returns “root” as user… How can I go on and tell the system to use the socket, as described?

    Thanks in advance for any help!

      • Thanks Mike, I’ve indeed found a solution (switched to nextCloud, way better) and managed to get my Redis server up and running! Thanks anyway for the answer!

    • You are welcome to test those permissions, 755 did not work for me and 775 had to because of the www-data group php-fpm runs as, your mileage may vary of course

  2. Thanks for this information and guide. I have a question,
    If I make these changes to Redis, will it still be available over TCP as it currently is?
    I don’t want to have to change the configuration of certain apps (no idea how), which are using Redis over TCP. But I would like to make Redis available over SOCK.

    • You should be able to access Redis over both unix sockets and TCP sockets concurrently. I only use one or the other but you can also set up an nginx proxy for passing TCP to the redis unix socket if you like.

  3. I assume the port used for TCP should be disabled right? There is a lot more in the redis.conf than your specificaitons above. might be good to tell which settings to disable (#).

    >Now reconfigure your service to use the Redis socket
    What do you mean? after all settings is done and redis i restarted it should work as expected no? You might provide with the correct configs in wp-config.php as well used for object-cache.php for WordPress. Any changes needed in the code due to unix socket I assume?

    • You can leave TCP enabled, from my tests it made no difference. Ever redis.conf can be different, the one used here is based on my redis installation tutorial so you can find the rest of the config there if you are curious. Please note that it is a very generic redis.conf and should always be tweaked by a professional to suit your needs!

      There are several object cache solutions out there so covering them all is beyond the scope of this tutorial. Redis can also be used for non-WordPress things so this tutorial aims to help as many as possible. If the Redis cache plugin you are using supports unix sockets then hopefully it is in their documentation :).

  4. Hi sir,
    how about the user and group config on redis.service file?
    should i change it to user: redis, group: www-data?

Comments are closed.