Easy Tutorial
❮ Strings Getbit Strings Setex ❯

Redis RANDOMKEY Command

Redis Keys

The Redis RANDOMKEY command returns a random key from the current database.

Syntax

The basic syntax of the redis RANDOMKEY command is as follows:

redis 127.0.0.1:6379> RANDOMKEY

Available Versions

= 1.0.0

Return Value

Returns a key when the database is not empty. Returns nil (or null on Windows systems) when the database is empty.

Examples

# Database is not empty

redis> MSET fruit "apple" drink "beer" food "cookies"   # Set multiple keys
OK

redis> RANDOMKEY
"fruit"

redis> RANDOMKEY
"food"

redis> KEYS *    # View all keys in the database, proving RANDOMKEY does not delete keys
1) "food"
2) "drink"
3) "fruit"

# Database is empty

redis> FLUSHDB  # Delete all keys in the current database
OK

redis> RANDOMKEY
(nil)

Redis Keys

❮ Strings Getbit Strings Setex ❯