The Redis Slow Log isn’t just a list of slow commands; it’s a diagnostic tool that reveals the impact of those commands on your Redis instance’s ability to serve requests.
Let’s see it in action. Imagine you’re running a web application and users are complaining about sluggish page loads. You suspect Redis might be the bottleneck. First, you need to enable and configure the slow log.
redis-cli config set slowlog-log-slower-than 100000 # Log commands taking longer than 100ms
redis-cli config set slowlog-max-len 1024 # Keep the last 1024 slow log entries
Now, let’s retrieve the slow log entries:
redis-cli slowlog get 10
This might output something like:
1) 1) (integer) 1234
2) (integer) 1678886400 # Timestamp
3) (integer) 500000 # Execution time in microseconds
4) 1) "SMEMBERS"
2) "user:12345:sessions"
2) 1) (integer) 1233
2) (integer) 1678886390
3) (integer) 250000
4) 1) "KEYS"
2) "*:user:*"
This output tells us that at timestamp 1678886400, a SMEMBERS command on the key user:12345:sessions took 500000 microseconds (0.5 seconds) to execute. Another slow command was KEYS "*:user:*" which took 250000 microseconds (0.25 seconds).
The problem the slow log solves is identifying which Redis commands are consuming disproportionate amounts of time, thereby impacting overall Redis performance and potentially your application’s responsiveness. It works by Redis internally tracking the execution time of every command. When a command’s execution time exceeds the slowlog-log-slower-than threshold (measured in microseconds), it’s added to a circular buffer of a fixed size (slowlog-max-len).
The key levers you control are:
slowlog-log-slower-than: This is your sensitivity knob. A lower value means you log more commands, potentially capturing shorter but still impactful delays. A higher value focuses on the truly egregious offenders. The default is 10000 microseconds (10ms).slowlog-max-len: This determines how much history you keep. A larger value retains more data but consumes a small amount of memory. A smaller value might lose important historical context. The default is 128 entries.
You can also clear the slow log with redis-cli slowlog reset.
The most surprising thing about the Redis Slow Log is how often it points to commands that shouldn’t be slow, like a simple GET or SET. This usually indicates a problem not with the command itself, but with the state of the Redis data structure it’s operating on. For example, a SMEMBERS command on a set with millions of members will naturally take longer than one on a small set. The slow log doesn’t just show you slow commands; it shows you commands that are slow in your current data context.
When a command appears repeatedly in the slow log, especially if it’s a command that typically runs very fast like GET or INCR, it’s a strong indicator that you might have a significant number of keys expiring in a very short period. This can lead to a "thundering herd" problem where Redis spends a lot of effort cleaning up expired keys, impacting its ability to serve other requests.
The next concept you’ll likely explore is using this information to optimize your data structures and application logic, potentially by avoiding expensive commands altogether or by splitting large data sets.