Redis CLI — The definitive guide

Redis is a data storage tool or you can consider it a special type of database. Unlike relational databases -SQL databases — which is stored in tables and NoSQL databases which are stored in documents, Redis is a key-value pair database. What that means is that there is no columns with rows of data that has the same structure. There are no tables or collections which groups data under a certain entity. In redis there are databases, and then under it a certain amount of keys and values.

Think of Redis database as a very long javascript object. You have a key and then you have a value which can have many types. Because of this way of data storage, Redis is being used in data caching, queuing and pubsub operations.

In this article, we will start deep diving into how redis cliis being used. Main while, I’ve created a 2 part video tutorial to cover redis cli even deeper with the use of more options, flags and going through redis configs:

Redis cli is a tool provided with Redis to help navigate through databases and keys.

You can install redis on debian based linux OSs using the command:

$ sudo apt install redis

Then to make sure it’s downloaded successfully type:

$ redis-cli ping
[output]
PONG

Data manipulation

Now, let’s explore further with redis cli data storage commands. Let’s set our first key:

$ redis-cli set greeding hello
[output]
OK

Now you have stored the value “hello” to the key “greeting” which you can call to retrieve this value:

$ redis-cli get greeting
[output]
hello

You can also grape the length of the value in a key:

$ redis-cli strlen greeting
[output]
5

The type of data that we set was string. We can also store integers in redis:

$ redis-cli set num 5
[output]
OK

Since we stored an integer, we can manipulate it with many ways including incrementing the number:

$ redis-cli incr num
[output]
6

Another type we can set is list. With a list, we can use other command called ‘lpush’:

$ redis-cli lpush newList 1 2 3 4 5

You can fetch list data using:

$ redis-cli lrange newList 0 -1

You can list a range of items with in a list. You have to add a start and end. The start index here is 0 which is the very start and if you want to list the whole list then you need to add the end as -1.

Databases

You can list all the keys inside a certain database by writing:

$ redis-cli keys *

In Redis, databases have indexes and the default database is in index 0. You can create or switch to a new database by using the command ‘select’:

$ redis-cli select 1

Now, you are in a whole new data bases. And to make sure that you are, try to list the keys inside the database again and you will find none.

You can also check a size of a database by:

$ redis-cli dbsize

This will list the number of keys in the current database.

Using databases is very useful when you connect to redis from different environments in your application. You do not need to create a redis server for each environment.

Messaging & Pubsub

Messaging is a very important concept in microservices applications. It’s basically the way you small apps are talking together. Redis helps in this aspect very well. We will use the cli to help you know how does that happen.

First, let’s make redis create a channel for us and start listen to it:

$ redis-cli subscribe train
[output]
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "train"
3) (integer) 1

That’s it. The output indicates that redis is now listening to any message that is going to be published to redis through a channel called ‘train’.

Now, open a new tab in your terminal and lets try to publish anything to this channel:

$ redis-cli publish train TOOOOOOT

First, you specify the operation ‘publish’ then the channel you want to use then the message you want to deliver. Now check your first tab where you are listening:

Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "train"
3) (integer) 1
1) "message"
2) "train"
3) "TOOOOT"

The channel has fetched the published message. That’s how microservices use redis to talk to each other. Services are listen and publish data between one another. To list all currently listening channels:

$ redis-cli pubsub channels *

You can replace * with a pattern of the channels you want to list. It could be tags.* or any type of pattern.

Conclusion

You can do may more than that with redis. Although you may not use redis mainly with CLI, being familiar with it is very vital for debugging and monitoring purposes.

Lead Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store