How to Use the Redis Get Set Command

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

Introduction

Although Redis is sometimes described as a database, it can best be defined as an in-memory key-value store. Because Redis stores data based on keys and values, it’s important to know how to use various Redis commands to manipulate data in this format. The Redis “get set” (GETSET) command allows you to set a key to a specified value and return the old value that was stored at the key. The Redis GETSET command is often used in conjunction with the INCR command to handle counting. In this article, we’ll take a closer look at the GETSET command and review some examples to understand how it’s used.

Prerequisites

Before you proceed with this Redis tutorial, make sure the following prerequisites are in place:

  • First, you need to make sure you have a Redis server and command-line interface (CLI) installed and working on your machine. You can check if it’s installed by using the command redis-server --version or redis-cli --version. Both commands will return the version number of your Redis installation.

  • Redis must be set up and running on your device in order for you to follow along with the examples in this tutorial. If you’re running a Linux distribution that uses systemd, you can use the command sudo systemctl status redis to display the current status of your Redis database.

The command shown below will start the Redis database:

1
sudo systemctl start redis

You can use the following command to automatically enable Redis after shutting down and restarting your machine.

1
sudo systemctl enable redis

The Redis GETSET command

The Redis GETSET command is a unique command that accomplishes two tasks at once– it returns the current value set at a key while also setting a new value stored at that key.

The basic syntax for the GETSET command can be seen below:

1
127.0.0.1:6379> GETSET key value

Let’s look at a simple example that demonstrates how you can GET and SET values in Redis. In this first step of the process, we’re going to SET a value stored at a key, and then we’ll retrieve the current value with GET. The GETSET command will come into play during the next step:

1
2
3
4
127.0.0.1:6379> SET test abc
OK
127.0.0.1:6379> GET test
"abc"

As you can see, we have a key named test. We set the value for key to be abc with the SET command. We then had Redis return this value to use with the GET command.

Now let’s try to utilize the GETSET command and see how Redis behaves:

1
2
127.0.0.1:6379> GETSET test "cde"
"abc"

What’s happening here? Here’s what’s going on: In the previous step of the process, we set the value of test to abc. The GETSET command returns the old key string value while simultaneously updating the key with the new value.

To confirm that a new value is now stored at the key, we can use the GET command one more time to retrieve the new value:

1
2
127.0.0.1:6379> GET test
"cde"

We can see that our GETSET worked correctly.

Use the GETSET command with a new key

In our previous example, we used GETSET on a key that already existed in Redis. What will happen if we specify a new key name with GETSET? In this situation, Redis would return nil.

1
2
127.0.0.1:6379> GET test1
"efg"

NOTE: The reason this happens is due to the fundamental way GETSET works. Because GETSET attempts to return the old value stored at a key, it will have to return nil when a new key name is specified because that key does not yet exist and thus has no value stored at it.

If you use the command GET it will display the following output:

1
2
127.0.0.1:6379> GET test1
"efg"

Using GETSET with the INCR command

We can also use GETSET in conjunction with the INCR command to create a value with an integer data type that we can increase by one:

1
2
3
4
5
6
127.0.0.1:6379> SET count 10
OK
127.0.0.1:6379> INCR count
(integer) 11
127.0.0.1:6379> GETSET count "21"
"11"

Notice that the value for the count returns the old value of the increased integer before it is updated with GETSET.

To get the current string value of count after the GETSET:

1
2
127.0.0.1:6379> GET count
"21"

The value of count has been converted to a string value when it is updated with a GETSET command. This is because GETSET always sets values in the form of a string.

Conclusion

Learning how to use different Redis commands allows you to harness the full power of your Redis in-memory key-value store. In this article, we focused on the Redis get set command, called GETSET. We explained how this command can be used to both return the old value stored at a key while also setting a new value at that key, and we looked at some examples of how this command works. With our instructions and examples, you’ll have no trouble utilizing the GETSET command in your own Redis environment.

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.