Redis Command Basics - Part 1
Introduction
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.
Prerequisites
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:
1 | redis-cli |
It opens my Redis prompt which looks like this:
1 | 127.0.0.1:6379> |
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.
1 2 | 127.0.0.1:6379> ping PONG |
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”:
1 2 | 127.0.0.1:6379> SET foo bar OK |
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:
1 2 | 127.0.0.1:6379> GET foo "bar" |
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:
1 2 | 127.0.0.1:6379> SET num 99 OK |
Now let’s increment it using the INCR
command:
1 2 | 127.0.0.1:6379> INCR num (integer) 100 |
Decrement with DECR
Now let’s learn the decrement command DECR
by decrementing that same num
value:
1 2 | 127.0.0.1:6379> 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:
1 2 | 127.0.0.1:6379> 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:
1 2 | 127.0.0.1:6379> 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:
1 2 | 127.0.0.1:6379> 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:
1 2 | 127.0.0.1:6379> 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”:
1 2 | 127.0.0.1:6379> 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
.
1 2 | 127.0.0.1:6379> SET var 99 OK |
Now in order to write to our file system we’ll exit the interactive shell:
1 | 127.0.0.1:6379> 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:
1 | root@fe431bc3c9c2:/demo# redis-cli INCR var > demo.txt |
Now you can view the contents of demo.txt
with the cat
command:
1 2 | root@fe431bc3c9c2:/demo# cat demo.txt 100 |
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:
1 2 3 | 127.0.0.1:6379> MONITOR OK _ |
Now in the second terminal let’s do the set command. First we need to get into the container with Redis:
1 | docker exec -it silly_rubin bash |
Then we get into the Redis CLI:
1 2 | root@fe431bc3c9c2:/# redis-cli 127.0.0.1:6379> |
Finally let’s set a key-value pair and see what happens in the first terminal:
1 2 | 127.0.0.1:6379> SET var 88 OK |
Now in the first terminal we got this:
1 2 | 1562698466.653041 [0 127.0.0.1:37082] "COMMAND" 1562698567.121199 [0 127.0.0.1:37082] "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:
1 2 | 127.0.0.1:6379> FLUSHALL OK |
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:
1 2 | 127.0.0.1:6379> SET fresh true OK |
Then we can set it to expire in 300 seconds like this:
1 2 | 127.0.0.1:6379> EXPIRE fresh 300 (integer) 1 |
We can check how long it has until it expires with the TTL
command like this:
1 2 | 127.0.0.1:6379> 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:
1 2 | 127.0.0.1:6379> 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.
Conclusion
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!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started