Redis Command Basics - Part 1


In this multi-part article we’ll show you the Redis command basics so you start to feel comfortable working with Redis. This article is for developers or anybody new Redis who want to learn quickly how to use it. We think people learn best by doing so we’ll show you commands that you should execute on your own Redis instance. This will get your Redis muscle memory going.


You should have some way to interact with Redis whether it’s installed on your local machine or it’s on a server somewhere, as long as you have access to the Redis CLI, the command line interface, you should be able to follow along. I’ve created Redis in a Docker container on my Macbook running OSX and ran the REdis CLI like this:


It opens my Redis prompt which looks like this:>

This is the prompt you’ll see in the remainder of the article.

Basic Commands

Let’s first start with the “Hello World” of Redis commands. The commands that you execute just as a sanity check to know that Redis is running and responding to your commands.> ping

If you execute the ping command. It should respond with PONG.

Set a Key Value Pair

To set a key value pair in Redis is easy with the SET command.

Note: The commands are case insensitive so set and SET will do the same thing but we prefer the capital version for readability’s sake.

Let’s use the SET command to set a key of foo to a value of “bar”:> SET foo bar

Get a Value by the Key

We just set a key-value pair and now we want to retrieve it. To get the value of the foo key, we’ll use the GET command like so:> GET foo

Increment with INCR

Next we’ll try to increment a number value with the INCR command. First let’s store a number value like this:> SET num 99

Now let’s increment it using the INCR command:> INCR num
(integer) 100

Decrement with DECR

Now let’s learn the decrement command DECR by decrementing that same num value:> DECR num
(integer) 99

Check if a value exists with EXISTS

A great command to know is the EXISTS command which check if a value exists for a key. Let’s first check for a key fantasyVar that doesn’t exist:> EXISTS fantasyVar
(integer) 0

It returned 0 which means it doesn’t exist. 1 means a value does exist. We created a key num earlier let’s check that it exists:> EXISTS num
(integer) 1

It returned a 1 indicating it does exist.

Remove or Delete a key-value pair

To remove or delete a key-value pair use the DEL command. We’ll delete our num variable from earlier like so:> DEL num
(integer) 1

It returned a 1 indicating that it was a success. Now that num is deleted let’s see what happens if we try to delete it again:> DEL num
(integer) 0

This time it returned a 0 indicating that it failed because the key-value pair didn’t exist and there was nothing to delete.

Print with Echo

You can print using the ECHO command. Let’s print the classic “Hello World”:> echo "Hello World"
"Hello World"

Output to File

Let’s learn how to pipe the output of Redis to a file. First let’s set a key-value pair var to 99.> SET var 99

Now in order to write to our file system we’ll exit the interactive shell:> exit

Even though we’re not in the interactive Redis shell we can still execute Redis commands by prefixing them with redis-cli. So let’s increment var from here and send it to a text file demo.txt like so:

root@fe431bc3c9c2:/demo# redis-cli INCR var > demo.txt

Now you can view the contents of demo.txt with the cat command:

root@fe431bc3c9c2:/demo# cat demo.txt

Monitor Redis activiy

To monitor the activity you can use the MONITOR command. So you’ll see in the terminal whatever is happening in Redis. We’ll demo this by having two Redis terminals open and execute the MONITOR command in one terminal, and execute a SET command in the other:> MONITOR

Now in the second terminal let’s do the set command. First we need to get into the container with Redis:

docker exec -it silly_rubin bash

Then we get into the Redis CLI:

root@fe431bc3c9c2:/# redis-cli>

Finally let’s set a key-value pair and see what happens in the first terminal:> SET var 88

Now in the first terminal we got this:

1562698466.653041 [0] "COMMAND"
1562698567.121199 [0] "SET" "var" "88"

So this is a great way to monitor the activity happening in your Redis instance.

Delete everything with FLUSHALL

If you want to delete everything you’ve stored with Redis then use the FLUSHALL command:> FLUSHALL

Use the EXISTS command to verify that all your data has been deleted.

Set expiration on data with EXPIRE

You can set key-values to expire after a certain amount of time with the EXPIRE command. Let’s look at an example. First we set a key-value:> SET fresh true

Then we can set it to expire in 300 seconds like this:> EXPIRE fresh 300
(integer) 1

We can check how long it has until it expires with the TTL command like this:> TTL fresh
(integer) 277

It has now 277 seconds until it expires. Now let’s wait more than 300 seconds let’s use get to verify it’s expired:> TTL fresh
(integer) -2

This -2 let’s you know it has expired. This DOES NOT mean that it expired two seconds ago but just that it has expired. Why? Well, if you do a TTL on a key without an expiration then you get -1 meaning it does not expire. So since -1 means it does not expire, -2 means it has an expiration that is up.


Please keep reading on to Part 2 where we’ll learn more Redis commands including RENAME and SETNX. Aren’t you curious?

This is just an introduction to the Redis Command Line Interface and some of the basic commands that you’ll need. In production things will be infinitely more complex and that’s when you’ll want to reach out to us at Object Rocket. We can manage your production databases so that you never have to worry about the nitty gritty.

Pilot the ObjectRocket platform free for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!


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.