RANDOMKEY

Since 1.1.0Keys

Syntax

RANDOMKEY
  • ACL categories: @generic, @keyspace, @read, @slow
  • Arity: 1

Returns the name of a key of the current database, picked at random, or nil if the database has no keys. It is useful to sample a database: what kinds of keys it holds, or how their sizes or times to live look, without listing them all. To go through every key, use SCAN.

A key whose time to live has run out is not returned: it is deleted as it is found, and another key is picked. While writes are paused, by CLIENT PAUSE WRITE, such a key cannot be deleted, and if every pick finds one, it is returned, as in Redis.

Only the connection's current database is looked at; SELECT another to sample it.

Reply

  • A key name.
  • nil if the database is empty.

Examples

> FLUSHDB
OK
> RANDOMKEY
(nil)
> SET k1 1
OK
> RANDOMKEY
"k1"
> RANDOMKEY extra
(error) ERR wrong number of arguments for 'randomkey' command