Optimizing Redis for Performance – Best Practices
Redis is a popular in-memory Nosql database that persists on disk. Redis is superfast and support multiple data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries . It has following features
1. replication
2. native Lua support
3. LRU eviction
4. Persistance on Disk
redis is capable of handling hundereds of thousands of requests per second. In this article we will discuss Optimizing Redis for performance.
1. Always use the Latest Version
Redis upgrades found to be very simple and easy.So make sure you get the latest updates .
2. Disable Persistance (Snapshots)
Redis supports RDB persistence, whic performs point-in-time snapshots of your dataset at specified intervals. But RDB needs to fork() often in order to persist on disk using a child process. Fork() can be time consuming if the dataset is big, and may result in Redis to stop serving clients for some millisecond or even for one second if the dataset is very big and the CPU performance not great
With default configuration RDB snapshots are enabled, you can disable RDB persistence (snapshot) by commenting out all lines starring with “save” as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 | # Note: you can disable saving completely by commenting out all “save” lines. # # It is also possible to remove all the previously configured save # points by adding a save directive with a single empty string argument # like in the following example: # save “” save 900 1 save 300 10 save 60 10000 |
Use “append-only” for persistance
The append-only file is an alternative, fully-durable strategy for Redis. It became available in version 1.1.
You can turn on the AOF in your configuration file:
1 2 3 | appendonly yes |
3. Set the “vm.overcommit_memory” variable to 1.
This kernel setting affects how memory allocation is handled when a parent forks a child. Without this setting, Redis might fail when dumping its dataset to disk or during the aof rewrites .
Edit /etc/sysctl.conf, add the line and reboot the box:
1 2 3 | vm.overcommit_memory = 1 |
4. Modify “timeout” Setting
Default value is 0, That means Redis won’t timeout idle connections. This can be an issue if you have misconfigured clients connected to Redis and they never close the connections. These connections will pileup and can create issues . Try to set this to a small value, if you are not using persistant connections
5. Run Redis on Unix sockets or “Loop back” interface
Redis is usually an internal service that doesn’t need to be exposed. If you have multiple servers that need to access the Redis instance, feel free to expose it inside LAN.