Tuning Redis for extra Magento performance

Tuning Redis for extra Magento performance

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 TCPLocal TCPLocal socket
PING_INLINE290,697.661,020,408.191,162,790.62
PING_BULK364,963.531,408,450.621,785,714.25
SET5,531.28591,716.00724,637.69
GET5,852.06485,436.91833,333.38
INCR246,913.58763,358.811,315,789.50
LPUSH5,572.58320,512.81487,804.88
LPOP5,852.74495,049.50564,971.75
SADD203,252.031,063,829.881,234,567.88
SPOP362,318.841,219,512.121,538,461.62
LPUSH5,572.27454,545.47500,000.00
LRANGE_10058.565,753.747,227.00
LRANGE_3001,535.201,690.93
LRANGE_500853.211,031.46
LRANGE_600619.66749.81
MSET65,402.2290,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.

You made it all the way down here so you must have enjoyed this post! You may also like:

How To Connect Google Analytics 4 To Magento 2 Bojan Mareljic
Bojan Mareljic, | 36

How To Connect Google Analytics 4 To Magento 2

3 best open-source eCommerce platforms in 2021 Zrinka Antolovic
Zrinka Antolovic, | 8

3 best open-source eCommerce platforms in 2021

Using Redis cache backend and session storage in Magento Marko Martinovic
Marko Martinovic, | 43

Using Redis cache backend and session storage in Magento

4 comments

  1. How would you modify the Redis cache tag cleanup script if using Unix sockets instead of TCP?

    For example, in your Magento redis cache setup tutorial you suggest a cron command such as:
    30 2 * * * /usr/bin/php /some/path/cm_redis_tools/rediscli.php -s 127.0.0.1 -p 6379 -d 0,1

    Will this tool work for sockets, and if so how do you configure the cron command?

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.