søndag 7. februar 2016

Redis, put your socks on!

Instead of handling connections via the TCP connection, one of the most important yields of Redis performance enhancment would have to be getting the sockets up and running.

On unbuntu server 14.04 you have the settings by doing like so:
sudo su
vi /etc/redis/redis.conf

Find the socket by searching in vim:
:/unixsocket <enter>

When you find the line with redis socket, just uncomment it, so it becomes:
unixsocket /var/run/redis/redis.sock
unixsocketperm 755

Then write and quit vim.
:wq

Then you need to reload the redis server, you can do it like so:
service redis-server restart

You can of course try to force a reload instead of doing a restart, but I find redis to often need a restart where Nginx often have enough with a reload. 

So, what are the gains my dear sir?
I did some benchmarks on my 5$ VPS.

First look at redis without socks (TCP):
root@xxxx:/var/www/kontekst# redis-benchmark -q -n 10000
PING_INLINE: 57471.27 requests per second
PING_BULK: 54347.82 requests per second
SET: 24038.46 requests per second
GET: 25974.03 requests per second
INCR: 26315.79 requests per second
LPUSH: 25641.03 requests per second
LPOP: 40650.41 requests per second
SADD: 65789.48 requests per second
SPOP: 44247.79 requests per second
LPUSH (needed to benchmark LRANGE): 43290.04 requests per second
LRANGE_100 (first 100 elements): 24570.02 requests per second
LRANGE_300 (first 300 elements): 11185.68 requests per second
LRANGE_500 (first 450 elements): 6997.90 requests per second
LRANGE_600 (first 600 elements): 6930.01 requests per second
MSET (10 keys): 37735.85 requests per second

Then the next one is looking at socks for less overhead:

(Remember to change your sock location!)
root@xxxx:/var/www/kontekst# redis-benchmark -q -n 10000 -s /.../.../.../redis.sock

PING_INLINE: 108695.65 requests per second
PING_BULK: 112359.55 requests per second
SET: 109890.11 requests per second
GET: 111111.11 requests per second
INCR: 90090.09 requests per second
LPUSH: 128205.12 requests per second
LPOP: 129870.13 requests per second
SADD: 67114.09 requests per second
SPOP: 61728.39 requests per second
LPUSH (needed to benchmark LRANGE): 60606.06 requests per second
LRANGE_100 (first 100 elements): 23752.97 requests per second
LRANGE_300 (first 300 elements): 9149.13 requests per second
LRANGE_500 (first 450 elements): 8361.20 requests per second
LRANGE_600 (first 600 elements): 5896.23 requests per second
MSET (10 keys): 56497.18 requests per second

So the socks connection gives: 53497.18 - 37735.85 = 15761,33 increase in requests per second!
This is completely free and takes you 2 lines of configuration... So why not do it? Even if your Redis handles it's IO today, you will of course put less strain on CPU and Memory too.