Redis Command Basics - Part 2


In this second installment of this multi-part article we’ll show you more Redis command basics so you continue to feel more and more comfortable working with Redis.

More Essential Redis Commands for Key-Value Pairs

In the first part of this tutorial we learned the basic SET, GET, DEL, EXISTS, INCR, and DECR commands. We’ll learn a couple more to our repertoire.


Similar to the INCR command which increases a value by one, we can also increase the value by a certain amount with the INCRBY command. Let’ see it in action. We’ll also show the SET and GET here as a refresher of what we covered in Part 1:> SET var 99

Now we haven’t told you the syntax of the command but notice that after you type the command INCRBY, the command line will let you know in grey what it’s expecting. In this case it’s expecting a key followed by an increment:

Image from Gyazo

So let’s follow it’s lead and increase our var by 100 like so:> INCRBY var 100
(integer) 199>


DECRBY works in the same fashion as INCRBY and would look like this:> DECRBY var 100
(integer) 99>


If you’d like to set multiple key value pairs at the same time you’ll use the MSET command. Let’s see how we’d set two key-value pairs:> MSET var1 apple var2 orange

Let’s verify that it worked by GETting the values:> GET var1
"apple"> GET var2

Note: If you run this command on existing keys it will replace the values. If you don’t want to overwrite the value check out the next command MSETNX.


If you want to set multiple key values but want to make sure you don’t replace any existing keys, then MSETNX is the command to use. If any of the keys you provide already exists it will abort the command entirely.

We’ll FLUSHALL our data so we have a fresh start then look at an example.> FLUSHALL

Now let’s set a variable:> SET var1 apple

Now let’s use MSETNX and try to overwrite var1 as well as set some other key values:> MSETNX var1 orange var2 pear
(integer) 0> GET var1
"apple"> GET var2

As you can see MSETNX returned a 0 meaning it failed. We then check var1 to see that it was not changed and var2 was not set. This is because since var1 already existed the entire operation was aborted and no values were set.


If you want to get multiple key values at the same time you’ll use the command MGET. For this example we’ll use MSET to set multiple key values then use MGET to read them back:> MSET var1 apple var2 orange
OK> MGET var1 var2
1) "apple"
2) "orange">


If you have a string value and want to append to a string onto it you can use the APPEND command. Let’s see an example:> SET str1 hi
OK> APPEND str1 lltop
(integer) 7> GET str1

Note: If the key you provide does not exist then it will just use that string as the value ( so it works exactly like a SET in that case).


If you want a substring of a value you’ll use GETRANGE and provide it with a key and a range.> SET str 1234567890
OK> GETRANGE str 0 5

Note It does also accept negative offsets if you want to start from the end of the string.


If you want to rename a key use the RENAME command. The value will of course stay unchanged.> SET cost 9.99
OK> RENAME cost price
OK> GET price


Like the other NX commands, this one performs a RENAME but only if the new key name doesn’t already exist. If the new key already exists it performs no action.> SET var1 99
OK> SET var2 100
OK> RENAMENX var1 var2
(integer) 0>

It returns a 0 so we know the action failed.


We’ve covered a lot more commands and demos to show you exactly how they work but there is still more ground to cover so we hope you’ll join us in Part 3 of this tutorial on some of the essential Redis commands.

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.