Most Important Redis Commands

Unlike a relational database that provides a customized SQL language, Redis uses a set of commands to manipulate and manage the server.

Having the most basic and essential commands at hand can be beneficial when working with Redis.

This article outlines some most essential commands to manage the Redis server.

Accessing Redis

The first most essential command in Redis is redis-cli. This command allows you to access the Redis CLI interface:

$ redis-cli // login to localhost and default port

$ redis-cli -p // login to localhost on custom port

$ redis-cli -h <host_address> -p <port> // login to redis on remote host and port


Redis provides 16 databases which you can switch using the SELECT command:> SELECT 5 // select database at index 5

Drop DB

To delete databases in Redis, use the commands:> FLUSHALL // delete everything> FLUSHDB // delete data in the current database

Performance Test

Redis comes with a benchmarking tool. To run the benchmark, use the command:

$ redis-benchmark -h <host> -p <port>

Database Backup

To run a backup of your database, you can use the commands:> BGSAVE // background save the database to dump.rdb file

Show Connections

To get the list of connected clients and their information, run the command:> client list

Terminate Connection

To terminate a specific client connection, use the command:> CLIENT KILL <IP>:<port>

Get Current Configuration

To get the active configuration on the server, run the command:> config get *

Change Configuration at Runtime

To set or change a configuration at runtime, use the command:> CONFIG SET DIRECTIVE VALUE

Managing Keys

There following are useful commands when working with Redis keys:> keys <pattern> keys [a-c]* // uses grep-like pattern> DEL <key> // delete key> EXISTS <key> // check if key exists> EXPIRE <key> <duration_in_seconds> // set key to expire in set duration> GET <key> // get the value of the specified key> SET <key> <value> // set new key value pair> MGET <key1 key2 key3…keyN> // batch fetch values> MSET <key1 value1 key2 value2…keyN valueN> // batch set key value pairs> RENAME old_key_name new_key_name // rename a key> TYPE <key> // get key type

Transaction Commands

If you want to execute a transaction in Redis, use the following commands:> MULTI // start transaction> EXEC // exec the unit> DISCARD // discard commands> WATCH // watch key changes> UNWATCH // unwatch keys

Other Commands

Other commands include:> AUTH <username> <password> // auth username and password> PING // test if server is up> QUIT // exit CLI


This short tutorial outlines some of the most common and essential commands to use when working with Redis.

Stay tuned for more!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list