Using Redis is a quick & easy way to improve Magento performance. Setting it up is easy, performance benefits are great, and we never had problems with it – it’s fire and forget. Inchoo already covered that in detail: https://inchoo.net/magento/using-redis-cache-backend-and-session-storage-in-magento/.
But, come on! Are we really going to races with the stock engine? Are we tuning Magento performance or what? Redis is fast by default, but it can get a lot faster. Read on to see how.
Crunching numbers
Redis, by default, listens to TCP connections only. And Magento, by default, connects that way to Redis. But, if PHP and Redis are on the same machine, you can easily use Unix sockets instead for an impressive performance gain. Here are the numbers:
Remote TCP | Local TCP | Local socket | |
PING_INLINE | 290,697.66 | 1,020,408.19 | 1,162,790.62 |
PING_BULK | 364,963.53 | 1,408,450.62 | 1,785,714.25 |
SET | 5,531.28 | 591,716.00 | 724,637.69 |
GET | 5,852.06 | 485,436.91 | 833,333.38 |
INCR | 246,913.58 | 763,358.81 | 1,315,789.50 |
LPUSH | 5,572.58 | 320,512.81 | 487,804.88 |
LPOP | 5,852.74 | 495,049.50 | 564,971.75 |
SADD | 203,252.03 | 1,063,829.88 | 1,234,567.88 |
SPOP | 362,318.84 | 1,219,512.12 | 1,538,461.62 |
LPUSH | 5,572.27 | 454,545.47 | 500,000.00 |
LRANGE_100 | 58.56 | 5,753.74 | 7,227.00 |
LRANGE_300 | 1,535.20 | 1,690.93 | |
LRANGE_500 | 853.21 | 1,031.46 | |
LRANGE_600 | 619.66 | 749.81 | |
MSET | 65,402.22 | 90,090.09 |
Remote TCP is another server. Local TCP is same server over TCP connection, and Local socket is same server over unix socket connection. Numbers are requests per second.
The numbers speak for themselves, but let’s emphasize benchmarks for two most commonly used Redis commands in Magento: SADD, where local socket serves 607.41% more requests per second than remote TCP and 116.05% more than local TCP, and GET, where local socket serves incredible 14240.00% more than remote TCP and 171.67% more requests per second than local TCP. (In fact, Magento uses HGET, but redis-benchmark does not benchmark that command, so we consider GET as a proxy here.)
If you want to reproduce the benchmark on your machines, here is the command to use:
redis-benchmark -s /var/run/redis/redis.sock -c 10 -P 10 -d 2000
(Using socket, 10 parallel clients, 10 requests pipelined, 2000 bytes data size, default 100000 requests). Settings for data should be reasonably close to how actually Magento uses Redis. And if you want to do benchmarks of your own: http://redis.io/topics/benchmarks.
The takeaway here is that Redis itself is crazy fast. Basically, it’s only limited by RAM speed. And that means that even TCP overhead reduces its performance a lot. If you add physical network overhead, you get a few orders of magnitude worse performance than you could have.
Unix sockets FTW
Setting up Redis & Magento to communicate over unix sockets is actually very easy (assuming you’ve already got Redis working over default TCP, these are the only changes needed):
1. Edit redis.conf. (On Ubuntu 16.04, Redis configuration is located in /etc/redis/redis.conf)
Under “# Specify the path for the Unix socket…” add:
unixsocket /var/run/redis/redis.sock
unixsocketperm 777
(Just remember, this is for Ubuntu, and may be different on other Linuxes, like, for example, CentOS.) Restart Redis server.
2. Edit Magentos local.xml:
<redis_session>
<host>/var/run/redis/redis.sock</host>
<port>0</port>
…
<cache>
<backend>Cm_Cache_Backend_Redis</backend>
<backend_options>
<server>/var/run/redis/redis.sock</server>
<port>0</port>
…
(Everything else staying the same as when using Redis over TCP.) Clear Magento caches. Voila!
Conclusion
So, let’s face it: you are not eBay or Alibaba (yet). Before you get there, you can still run your Magento shop fine off of one server. Use that to your advantage.
Today, one server can (and should) mean plenty of CPU cores and heaps of RAM. And, we can take that big, bad engine and turbocharge it. Use unix sockets to get the maximum power out of Redis.