We love Redis because it’s fast (and fun!), so as we begin to consider scaling out Redis, we first want to make sure we've done everything we can to maximize its performance.
Let's start by looking at some important tuning parameters.
Max Clients
Redis has a default of max of 10,000 clients; after that maximum has been reached, Redis will respond to all new connections with an error. If you have a lot of connections (or a lot of application instances), then you may need to go higher. You can set the max number of simultaneous clients in the Redis config file:
Max Memory
By default, Redis has no max memory limit, so it will use all available system memory. If you are using replication, you will want to limit the memory usage in order to have overhead for replica output buffers. It’s also a good idea to leave memory for the system. Something like 25% overhead. You can update this setting in Redis config file:
Set TCP-BACKLOG
The Redis server uses the value of tcp-backlog to specify the size of the complete connection queue.
Redis passes this configuration as the second parameter of the listen(int s, int backlog) call.
If you have many connections, you will need to set this higher than the default of 511. You can update this in Redis config file:
As the comment in redis.conf indicates, the value of somaxconn and tcp_max_syn_backlog may need to be increased at the OS level as well.
Set Read Replica Configurations
One simple way to scale Redis is to add read replicas and take load off of the primary. This is most effective when you have a read-heavy (as opposed to write-heavy) workload. You will probably want to have the replica available and still serving stale data, even if the replication is not completed. You can update this in the Redis config:
You will also want to prevent any writes from happening on the replicas. You can update this in the Redis config:
Kernel Memory
Under high load, occasional performance dips can occur due to memory allocation. This is something Salvatore, the creator of Redis, blogged about in the past. The performance issue is related to transparent hugepages, which you can disable at the OS level if needed.
Kernel Network Stack
If you plan on handling a large number of connections in a high performance environment, we recommend tuning the following kernel parameters:
File Descriptor Limits
If you do not set the correct number of file descriptors for the Redis user, you will see errors indicating that “Redis can’t set maximum open files..” You can increase the file descriptor limit at the OS level.
Here's an example on Ubuntu using systemd:
You will then need to reload the daemon and restart the redis service.
Enabling RPS (Receive Packet Steering) and CPU preferences
One way we can improve performance is to prevent Redis from running on the same CPUs as those handling any network traffic. This can be accomplished by enabling RPS for our network interfaces and creating some CPU affinity for our Redis process.
Here is an example. First we can enable RPS on CPUs 0-1:
Then we can set the CPU affinity for redis to CPUs 2-8: