When you encounter a Redis instance and you quickly want to learn about the setup you just need a few simple commands to peak into the setup. Of course it doesn't hurt to look at the official full command documentation
, but below is a listing just for sysadmins.
First thing to know is that you can use "telnet" (usually on default port 6397)
telnet localhost 6397
or the Redis CLI client
to connect to Redis. The advantage of redis-cli is that you have a help interface and command line history.
Here is a short list of some basic data extraction commands:
|Type||Syntax and Explanation|
Watch current live commands. Use this with care on production.
Cancel with Ctrl-C.
slowlog get 25 # print top 25 slow queries
keys pattern # Find key matching exactly
keys pattern* # Find keys matching in back
keys *pattern* # Find keys matching somewhere
keys pattern* # Find keys matching in front
On production servers use "KEYS" with care as it causes a full scan of all keys!
dump <key> # Serialize key
expire <key> <seconds>
set <key> <value>
setnx <key> <value> # Set key value only if key does not exist
mget <key> <key> ...
mset <key> <value> <key> <value> ...
lrange <key> <start> <stop>
lrange mylist 0 -1 # Get all of a list
lindex mylist 5 # Get by index
llen mylist # Get length
lpush mylist "value"
lpush mylist 5
rpush mylist "value"
lpushx mylist 6 # Only push in mylist exists
rpushx mylist 0
lrem mylist 1 "value" # Remove 'value' count times
lset mylist 2 6 # mylist = 6
ltrim <key> <start> <stop>
hexists myhash field1 # Check if hash key exists
hget myhash field1
hdel myhash field2
hset myhash field1 "value"
hsetnx myhash field1 "value"
hmget <key> <key> ...
hmset <key> <value> <key> <value> ...
hincrby myhash field1 1
hincrby myhash field1 5
hincrby myhash field1 -1
hincrbrfloat myhash field2 1.123445
For scripting just pass commands to "redis-cli". For example:
$ redis-cli INFO | grep connected
The statistics command is "INFO" and will give you an output as following.
$ redis-cli INFO
connected_clients:2 # <---- connection count
used_memory_human:6.29M # <---- memory usage
role:master # <---- master/slave in replication setup
Changing Runtime Configuration
CONFIG GET *
gives you a list of all active configuration variables you can change. The output might look like this:
redis 127.0.0.1:6379> CONFIG GET *
22) "everysec" # <---- how often fsync() is called
24) "900 1 300 10 60 10000" # <---- how often Redis dumps in background
Note that keys and values are alternating and you can change each key by issuing a "CONFIG SET" command like:
CONFIG SET timeout 900
Such a change will be effective instantly. When changing values consider also updating the redis configuration file.
Redis has a concept of separated namespaces called "databases". You can select the database number you want to use with "SELECT". By default the database with index 0 is used. So issuing
redis 127.0.0.1:6379> SELECT 1
switches to the second database. Note how the prompt changed and now has a "" to indicate the database selection.
To find out how many databases there are you might want to run redis-cli from the shell:
$ redis-cli INFO | grep ^db
To drop the currently selected database run
to drop all databases at once run
Checking for Replication
To see if the instance is a replication slave or master issue
redis 127.0.0.1:6379> INFO
and watch for the "role" line which shows either "master" or "slave".
Starting with version 2.8 the "INFO" command also gives you per slave replication status looking like this
Setting up Replication
If you quickly need to set up replication just issue
SLAVEOF <IP> <port>
on a machine that you want to become slave of the given IP. It will immediately get values from the master. Note that this instance will still be writable. If you want it to be read-only change the redis config file (only available in most recent version, e.g. not on Debian).
To revert the slave setting run
SLAVEOF NO ONE
Install the Redis tools and run the provided benchmarking tool
redis-benchmark -h <host> [-p <port>]
If you are migrating from/to memcached protocol check out how to run the same benchmark
for any key value store with memcached protocol.
First measure system latency on your Redis server with
redis-cli --intrinsic-latency 100
and then sample from your Redis clients with
redis-cli --latency -h <host> -p <port>
If you have problems with high latency check if transparent huge pages are disabled. Disable it with
echo never > /sys/kernel/mm/transparent_hugepage/enabled
Dump Database Backup
As Redis allows RDB database dumps in background, you can issue a dump at any time. Just run:
When running this command Redis will fork and the new process will dump into the "dbfilename" configured in the Redis configuration without the original process being blocked. Of course the fork itself might cause an interruption.
Use "LASTSAVE" to check when the dump file was last updated. For a simple backup solution just backup the dump file.
If you need a synchronous save run "SAVE" instead of "BGSAVE".
Starting with version 2.4 you can list connections with
and you can terminate connections with
CLIENT KILL <IP>:<port>
The propably most useful command compared to memcached where you need to trace network traffic is the "MONITOR" command which will dump incoming commands in real time.
redis 127.0.0.1:6379> MONITOR
1371241109.735725 "keys" "*"
1371241152.344504 "set" "testkey" "1"
1371241165.169184 "get" "testkey"
additionally use "SLOWLOG" to track the slowest queries in an interval. For example
# wait for some time
SLOWLOG GET 25
and get the 25 slowest command during this time.