Redis Key Value Pair Command Basic Tutorial - Part 2


If you’ve spent any time working with Redis, you know that key-value pairs are used to store all the data in the system. Knowing how to access and manipulate these key-value pairs is essential to use Redis effectively. In the first installment of this two-part series, we looked at some common Redis key value pair commands. This article will pick up where we left off and continue our overview of basic Redis commands.


Before we begin executing Redis key value pair commands, let’s go over the prerequisites that are essential for this tutorial:

  • You’ll need to ensure that Redis is properly installed and configured. You can download Redis on their website
  • Make sure that your Redis server is running in the background.

Redis RENAME Command

The Redis RENAME command does exactly what its name implies– it will rename a Redis key.> SET object "ball"
OK> RENAME object bounce
OK> GET bounce

In the code shown above, we set a key named “object” and set its value to “ball”. We then rename “object”, giving it the new name “bounce”.

Next, we verify that our operation was successful using the “GET” command. It returns the value “ball” that we initially set to the key “object”.

Redis RENAMENX Command

The Redis RENAMENX command allows us to rename a key provided that a key with the new name does not already exist. Redis will not execute the command if such a key exists.> SET toy "airplane"
OK> RENAMENX toy bounce
(integer) 0>

In the example above, we created a key-value pair named “toy” with a value of “airplane”. We then try to perform the RENAMENX command against the key named “toy”, giving it the new name “bounce”. Keep in mind that we created a key named “bounce” in our previous example. Redis notices this and will not run the command, returning (integer) 0 as the response since the key “bounce” already exists.

Redis GETRANGE Command

The GETRANGE command returns the substring of a specified string value that is determined by offsets indicating its start and end points.> SET pstring "The quick brown fox"
OK> GETRANGE pstring 0 3
"The ">

Let’s take a closer look at what’s happening in these commands. First, we set a key named “pstring” with value of “The quick brown fox”. Then we perform GETRANGE. This command takes the parameter “0” as its starting point and the parameter “3” as its ending point. Redis returns the string “The”, which is the substring located between the specified starting and ending points.

Redis GETSET Command

As we discussed in the first part of this two-part series, GETSET will automatically set a key to a value and will return the old value.> GETSET pstring "jump over the lazy dog"
"The quick brown fox"> GET pstring
"jump over the lazy dog">

Notice how Redis returned the old value of the key “pstring”: the string “The quick brown fox”. We can verify that we did successfully replace the value of “pstring” using the command GET pstring.

Redis SETEX Command

The Redis SETEX command allows us to set a key to hold a given value for a certain period of time in seconds.> SET key1 "hello"
OK> EXPIRE key1 10
(integer) 1> ttl key1
(integer) 8> ttl key1
(integer) 4>

The code shown above will achieve the same result as SETEX, but it requires more commands to execute.

In the following command, we’ll use the SETEX command, and you can see the difference:> SETEX key1 20 "hello"
OK> ttl key1
(integer) 18> ttl key1
(integer) 11> ttl key1
(integer) 8>

Redis PSETX Command

The Redis PSETX is very similar in functionality to SETEX, but it only uses milliseconds:> PSETEX key1 5000 "Hello"
OK> pttl key1
(integer) 1249> pttl key1
(integer) -2>

In this example, we set a value for “key1” that will be held under 5000 milliseconds. As we execute the command PTTL key1, we can see that it returns a decreasing value of milliseconds until we get a -2 response, which tells us that the value already expired. This releases the key and removes the value that was temporarily occupying “key1”.


This Redis command will remove an existing timeout on a key. Let’s look at an example similar to the one in the previous section. This time, however, we will stop the expiration timer while it’s in effect:> PSETEX key1 20000 "hello"
OK> PTTL key1
(integer) 9763> PERSIST key1
(integer) 1> PTTL key1
(integer) -1> GET key1

The code shown above sets a timer of 20 seconds for the value “hello” to occupy “key1”. We can see that the timer is at 9731 milliseconds when we execute the PERSIST command. After executing PERSIST, we execute the PTTL key1 command again to check if the timer has stopped; we receive a result of “-1”, which means that the timer was terminated.

We then perform the GET key1 command. The response shows us that the value “hello” persists for the key “key1”, even though the timer that was originally set with PSETEX has expired.


In Redis, data is stored in key-value pairs, so it’s essential to know how to access and manipulate these pairs using the basic Redis commands. This second installment in our two-part series continued our overview of Redis key value pair commands, covering RENAME, GETSET, SETEX and many more. With the information provided in this tutorial, you’ll be prepared to work with key-value pairs in your own Redis implementation.

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.