But, more often, Redis is not a bottle neck of you application. Before you start tuning make sure you have tested your hardware, network performance, optimized data structure.
Redis has a built-in benchmark tool, and it is a good idea to benchmark your setup before making any changes and after, compare the results and make a decision how to proceed.
To get detailed information about redis-benchmark
follow this official documentation.
Tuning Linux for best performance with Redis
Enabling overcommit_memory
It’s the best practice to avoid out of memory space issues.
Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition. To fix this issue add 'vm.overcommitmemory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommitmemory=1' for this to take effect. - you will get this warning in logs at redis startup if you have disabled memory overcommit.
To avoid it, run:
$ echo 1 > /proc/sys/vm/overcommit_memory
$ echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
Set swappiness at the most reasonable weight
Swappiness is a kernel parameter that allows system administrators to adjust how often the system utilizes the swap system. It's a range of values from 0 to 100. The higher value tells kernel to swap more often, the lower value will keep inactive pages in memory longer.
As Redis is an in-memory database, it's good to have all it's data in memory, not in swap. The lowes value for swappiness may helps us. Try to play with the values lower than default.
To get the current value of swappiness just run
$ sysctl vm.swappiness
or
$ cat /proc/sys/vm/swappiness
for major Linux distributions this value is equivalent to 60.
try to change it to 40, 20, 10 or even 1.
Disabling Transparent Hugepages
Under normal circumstances, it's good to have hugepages enabled. But disabling THP may significantly improve Redis performance.
To disable THP you can run
# echo never > /sys/kernel/mm/transparent_hugepage/enabled
This will stop creation and usage of the new THP. The THP which were created and used at the moment the above command was run would not be disabled. To get rid of THP completely the system should be rebooted with THP disabled at boot time.
Disabling THP at a boot time depends on your Linux distribution, but you can try the following ways:
Disabling THP via Grub configuration
Edit your boot loader (for example /etc/grub.conf
). Add the following to the end of the kernel line. However, consult the documentation for your system before editing your boot loader configuration.
transparent_hugepage=never
Disabling THP via systemd
systemd
users can create a service called disable-transparent-huge-pages.service
.
Just create a file /etc/systemd/system/disable-transparent-huge-pages.service
[Unit]
Description=Disable Transparent Huge Pages (THP)
DefaultDependencies=no
After=sysinit.target local-fs.target
Before=redis.service
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null'
[Install]
WantedBy=basic.target
Or you can nchoose any other way you would like better.
Tuning redis.conf for best performance
Memory Usage
Redis may use all of your available memory unless this is configured. It is a good idea to let Redis use up to 80% of your server memory.
maxmemory 13Gb
Setting eviction policies
If you use Redis as a cache engine, you should pay attention on key eviction policies. This is the behavior Redis follows when the maxmemory
limit is reached.
maxmemory-policy volatile-lfu
works best for me, but I recomment follow this official documentation and choose the value which will work for you.
Persistence managing
When using Redis for persistence storage, the best option for optimal and performant Redis is to enable both AOF and RDB. RDB is enabled by default, to enable AOF just modify appendonly
value.
appendonly yes
If you use Redis as a cache engine you can typically disable persistence by disabling AOF and RDB
appendonly no
save ""
Follow the official documentstion to find out how RDB and AOF work.