Redis

How to use Redis Keys

Redis is a key-value data store. Hence, understanding how to work with keys is a fundamental concept when using Redis.

This guide will look at how to work with Redis keys and various commands to manage them.

Creating a Redis Key

The first step is to create a Redis key. Redis provides you with the SET command to create a Redis key and its associated value.

The syntax is as shown:

SET <key> <value>

For example, to create a key called database and holds the value Redis, you can execute the command:

127.0.0.1:6379> SET database redis
OK

The command returns a string “OK” if the command is executed successfully.

NOTE: When creating keys in Redis, you have to provide an associated value. If you give only the key name, Redis will return an error as shown:

127.0.0.1:6379> SET keyname
(error) ERR wrong number of arguments for 'set' command

Deleting a Key

To delete a key in Redis, use the DEL command. The command takes the name of the key as the argument.

The syntax can be expressed as:

DEL <key_name>

For example, to delete the key “database”, we can run the command as:

127.0.0.1:6379> DEL database
(integer) 1

The DEL command will return an integer value indicating the number of keys removed by the command.

If the provided key does not exist, the command will return an integer 0.

Rename a Key

To rename a key, use the RENAME command in Redis. This simple command takes the original key name and the new key name as the argument.

127.0.0.1:6379> RENAME database databases
OK

Similar to the SET command, RENAME will return the string “OK” if the command is executed successfully.

Ensure the key that you wish to rename exists in the database. If not, Redis will return an error as shown:

127.0.0.1:6379> RENAME nokey newkey
(error) ERR no such key

Check if Key Exists

You can check if a key exists by using the EXISTS command. It takes the name of the key as the argument.

For example:

127.0.0.1:6379> EXISTS databases
(integer) 1

The command will return an integer 1 indicating the key exists in the database and 0 if the key does not exist.

Relocate a Key

Suppose you want to move a key from a specific database to another. For that, you can use the MOVE command.

The command takes the key and the target database.

For example, to move the key “databases” from the database at index 0 to index 10, we can run the command:

127.0.0.1:6379> MOVE databases 10
(integer) 1

If the key exists in the source database, Redis will move it to the specified target database and return an integer 1. If not, the command returns integer 0.

Fetch Random Key

If you want to grab a random key from the Redis database, you can use the RANDOMKEY command. This command does not take any arguments but returns a random key.

127.0.0.1:6379> RANDOMKEY
"captains"

However, the key has to exist within the database.

Get Key Type

If you want to determine the data type that a specific key holds, you can use the TYPE command followed by the name of the key.

For example:

127.0.0.1:6379> TYPE captains
zset

The command shows the captains key holds a sorted set.

Ensure the key exists; otherwise, Redis will return a None type.

127.0.0.1:6379> TYPE nokey
none

Expire a Key

You can set an expiration timer for a specific key. If the expiry duration elapses, the key is removed from the database.

For that, you can use the EXPIRE command followed by the key name and the expiry time for the key in seconds.

127.0.0.1:6379> EXPIRE captains 60
(integer) 1

This tells Redis to set the key “captains” lifetime to 60 seconds. After 60 seconds, the key is dropped from the database.

Get Remaining Key Duration

You can use the TTL command if you want to know how many seconds a specific key has before it expires.

TTL captains
10

Dump Values

You can use the DUMP command to get a serialized version of all the values stored in a key.

127.0.0.1:6379> DUMP captains

The resulting value is as shown below:

"\x0c@ii\x00\x00\x00f\x00\x00\x00\n\x00\x00\x0fJonathan Archer\x11\xf2\x02\rCarol Freeman\x0f\xf3\x02\x0fKathryn Janeway\x11\xf4\x02\x10Christopher Pike\x12\xf5\x02\x0fJean-Luc Picard\x11\xf6\xff\t\x00\xd1\xb8\xd1\r\x03\xd5\x0f\x15"

The above represents a serialized version of the values stored in the specified key.

Closing

This tutorial taught you how to work with keys and various commands to manage keys in a Redis database.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list