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?

redis-object-cache-unix-socket-speed

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.

port 0
# create a unix domain socket to listen on
unixsocket /var/run/redis/redis.sock
# set permissions for the socket
unixsocketperm 777
#requirepass passwordtouse
bind 127.0.0.1
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 🙂