Redis Key Value Pair Commands Basic Tutorial - Part 1

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

This is the first part of a tutorial series explaining the basic Redis key value pair command. Redis is not a plain key-value store, and as such it can hold more complex data structures, such as strings. There are a number of commands for operating on strings to set a key to a new value. This tutorial series will provide explanations and examples of some of the most commonly used Redis key value pair command.

Prerequisites

  • Redis must be properly installed and configured. Redis can be downloaded at: Redis.io
  • The Redis server must be running in the background.

Redis Basic Commands

This section will explain the most basic and commonly used Redis key value pair command and how to use the commands. The basic commands are as follows:

  • DECRBY – This command will decrease a number by a specified amount.
  • DECR – This command will decrease an amount or a value by one (1).
  • INCRBY – Increases a number by a specified amount.
  • INCR – Increases an amount or a value by one (1).
  • EXISTS – This command verifies if a key already exists.
  • DEL – This command deletes a variable.
  • GET – Gets a value of the specified variable.
  • SET – This will set a string variable.
  • MSET – This permits setting multiple keys to values and will replace existing values with new values.
  • MSETNX – This will set keys to respective values, provided the specified keys do not already exist.
  • MGET – This command will return all values of any specified keys.
  • APPEND – Appends the value at the end of the string, provided the key already exists and is a string.
  • GETRANGE This returns the substring of a specified string value and is determined by an offset start at the end.
  • RENAME – Renames a key; this command will throw an error if the key does not exist.
  • RENAMENX – This command will rename a key to newkey if the newkey does not exist. It will return an error if the key does not exist.
  • GETSET – This automatically sets the key to a value and will return the old value.
  • SETEX – This command is for setting a key to hold a value for a specified duration of time, in seconds.
  • PSETX – This works the same as the SETEX command, but it milliseconds.
  • PERSIST – This command removes an existing timeout on a key.
  • SETNX – This is like the SET command, but it only works if the key does not exist. If the key does exist, it will not change.

Redis Command Examples

With an understanding of the basic Redis commands, this section will explain how to use the commands via redis-cli.

Redis MSET command example

First, examine the following MSET command that allows for setting multiple keys to values:

1
MSET firstname "rommel" lastname "galisanao"

The following MGET command is used to retrieve all the values with the corresponding keys:

1
MGET firstname lastname

The results should resemble the follow:

1
2
3
127.0.0.1:6379> MGET firstname lastname
1) "rommel"
2) "galisanao

Redis MSETNX command example

The definition for the MSETNX command says the keys can be set to respective values only if the keys do not already exist. This is shown here:

1
2
3
4
5
127.0.0.1:6379> MSETNX age 37
(integer) 1
127.0.0.1:6379> MSETNX position "supervisor" age 39
(integer) 0
127.0.0.1:6379>

Note the age was successfully set at 37, however, Redis did not perform the operation when trying to set the age again in the following line, as indicated by the remark (integer) 0.

Here the key position is set to supervisor, but, as shown below, Redis did not successfully execute the command:

1
2
3
127.0.0.1:6379> GET position
(nil)
127.0.0.1:6379>

Here Redis did not execute the command because there is already an existing key in the command.

Redis APPEND command example

This section will explain how to use the Redis APPEND command. First, set up a key-value pair as shown here:

1
2
3
4
5
6
7
8
127.0.0.1:6379> SET statement "I Love"
OK
127.0.0.1:6379> Get statement
"I Love"
127.0.0.1:6379> APPEND statement " My Job"
(integer) 13
127.0.0.1:6379> GET statement
"I Love My Job"

After setting up the key statement with the string “I Love,” execute and APPEND command to the key statement with the string “My Job.” As shown above, this results in a new value of “I Love My Job” within the key statement.

Conclusion

This was the first part in a tutorial series explaining the basic Redis key value pair command. This article provided the prerequisites for part one of this series and gave explanations for 20 commands. The tutorial also provided specific examples for the Redis MSET Command, the Redis MSETNX command and the Redis APPEND command. Remember that the MSETNX command only allows the keys be set to respective values if the keys have not already been created.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.