Redis Setup Notes and One-Liners


Being a heavy user of Redis has forced some weird Bash-fu and other commands when I want to find out how things are going. Because Redis is single threaded (see here for more information), I commonly run multiple Redis instances per machine. As a result, when running on AWS, I use a specific machine layout to get the best CPU utilization for Redis. On an m2.4xlarge machine, it comes with 8 cores and 68G of RAM. To take full advantage of that I run 7 instances of Redis and pin one instance to a CPU core (this can be done using taskset in schedutils package). For extra performance, I leave an entire core to the OS (even though the machines do little other than process Redis commands.

Because the machine is doing so much at once with Redis, it is a little more challenging to monitor. If I wanted to find out the stats on anything, I had to do a little scripting. Here are a few commands and scripts that I use regularly to check into the health and state of Redis.

  • To check the number of commands being executed per second per instance, you can use this script to get a rough idea. Just swap out the ports for the ones you use and the rest should work itself out.

    ports="6379 6380 6381 6382 6383 6384 6385"
    for port in ${ports}
        t0=$(redis-cli -p $port info | awk 'BEGIN {FS=":"} /command/ {printf("%d", $2)}')
        sleep 1
        t1=$(redis-cli -p $port info | awk 'BEGIN {FS=":"} /command/ {printf("%d", $2)}')
        echo "$port: $val"
  • If you want to check the ping time to Redis in thousandths of a second, you can use this script. Admittedly I stole this from a while back. But it’s so useful (for scripting) that I still use it from time to time.
    TIME=$( { time redis-cli -p 6385 PING; } 2>&1 ) ;
    echo $TIME | awk '{print $3}' | sed 's/0m//; s/\.//; s/s//; s/^0.[^[1-9]*//g;'
  • When running multiple instances of Redis on a single machine, I try to make sure that the instances don’t “step” on each other with memory. I do this by setting a maximum amount of memory per instance. To check the current utilization over max allowed for that instance, the follow command line was used (yes these print out on different lines intentionally because that makes it easier to read).
    for port in 63{79..85}; do echo -n "${port}: "; redis-cli -p $port info | \
    grep "used_memory:" | sed -e "s/used_memory://"; echo -n "    / "; \
    redis-cli -p $port config get maxmemory | grep 2; done;
  • One of the ways to keep track of what Redis instance is responsible for what keys is to use namespacing when adding keys. For instance, anything that has to do with caching has a top level namespace of cache. It is then followed by a second level of what is being cached and a third level of the specific item. Specifically the key name will look like this cache::account::12345. This way if you want to clear the cache on all Redis instances, you can use the following one liner.
    for port in 63{79..85}; do echo -n "${port}: "; \
    redis-cli -p $port keys cache::\* | xargs redis-cli -p $port del; \

If you have any more of these, feel free to leave them in the comments.

* Note: More information on using taskset can be found here.

One Response to “Redis Setup Notes and One-Liners”

  1. Nice article I’m sharing this! In the mean time if you are looking for a fully featured Redis GUI you may want to take a look at Redsmin !

Leave a Reply