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.
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.
If you execute the
ping command. It should respond with
Set a Key Value Pair
To set a key value pair in Redis is easy with the
Note: The commands are case insensitive so
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”:
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:
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:
Now let’s increment it using the
Decrement with DECR
Now let’s learn the decrement command
DECR by decrementing that same
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:
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:
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:
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:
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”:
Output to File
Let’s learn how to pipe the output of Redis to a file. First let’s set a key-value pair
Now in order to write to our file system we’ll exit the interactive shell:
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:
Now you can view the contents of
demo.txt with the
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:
Now in the second terminal let’s do the set command. First we need to get into the container with Redis:
Then we get into the Redis CLI:
Finally let’s set a key-value pair and see what happens in the first terminal:
Now in the first terminal we got this:
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
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:
Then we can set it to expire in 300 seconds like this:
We can check how long it has until it expires with the
TTL command like this:
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:
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!PILOT FREE FOR 30 DAYS