73

I'm using an ORM called Ohm in Ruby that works on top of Redis and am curious to find out how the data is actually stored. I was wondering if there is way to list all the keys/values in a Redis db.

Update:
A note for others trying this out using redis-cli, use this:

$ redis-cli keys
* (press * followed by Ctrl-D)
... (prints a list of keys and exits)
$

Thanks @antirez and @hellvinz!

starball
  • 20,030
  • 7
  • 43
  • 238
Jagtesh Chadha
  • 2,632
  • 2
  • 23
  • 30

4 Answers4

119

You can explore the Redis dataset using the redis-cli tool included in the Redis distribution.

Just start the tool without arguments, then type commands to explore the dataset.

For instance KEYS will list all the keys matching a glob-style pattern, for instance with: keys * you'll see all the keys available.

Then you can use the TYPE command to check what type is a given key, if it's a list you can retrieve the elements inside using LRANGE mykey 0 -1. If it is a Set you'll use instead SMEMBERS mykey and so forth. Check the Redis documentation for a list of all the available commands and how they work.

mlissner
  • 17,359
  • 18
  • 106
  • 169
antirez
  • 18,314
  • 5
  • 50
  • 44
33

From the command line, you can also use the dump command, available since Redis 2.6.0

redis-cli KEYS \* | xargs -n 1 redis-cli dump

(note that this also works with the get command for earlier versions if you don't mind)

UPDATE (V2.8 or greater): SCAN is a superior alternative to KEYS, in the sense that it does not block the server nor does it consume significant resources. Prefer using it.

N.Martignole
  • 579
  • 5
  • 8
  • Or use the GET command if you have an older Redis version. And if you have a lot of keys then you can parallelise the xargs command with -P. Example: `redis-cli KEYS * |xargs -n 1 -P8 redis-cli get` – Andy Triggs Feb 14 '14 at 14:31
  • 9
    Yours is the first answer I came across which informed me I had to escape the * to \\* on the command line.... must have wasted about 20 minutes with "wrong number of arguments" for a redis command which works perfectly well in the client until I found this. Thanks. – Sam Critchley Jul 21 '14 at 20:23
  • 1
    This will work, but having to run a `redis-cli` for each key, it will be extremely slow. – tokland Nov 29 '16 at 12:18
21

Just adding a practical ruby example to the antirez response (I won't dare compete with him)

irb(main):002:0> require 'rubygems'
=> true
irb(main):003:0> require 'redis'
=> true
irb(main):004:0> r = Redis.new
=> #<Redis:0x8605b64 @sock=#<TCPSocket:0x8605ab0>, @timeout=5, @port=6379, @db=0, @host="127.0.0.1">
irb(main):005:0> r.keys('*')
Jagtesh Chadha
  • 2,632
  • 2
  • 23
  • 30
hellvinz
  • 3,460
  • 20
  • 16
3

I ended up here because I was looking for how to backup all key/values in redis. If this applies to you, this command might help:

redis-cli bgsave
hert
  • 1,012
  • 8
  • 16