Redis

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

Databases

Redis provides 16 databases which you can switch using the SELECT command:

127.0.0.1:6379> SELECT 5 // select database at index 5

Drop DB

To delete databases in Redis, use the commands:

127.0.0.1:6379> FLUSHALL // delete everything

127.0.0.1:6379> 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:

127.0.0.1:6379> BGSAVE // background save the database to dump.rdb file

Show Connections

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

127.0.0.1:6379> client list

Terminate Connection

To terminate a specific client connection, use the command:

127.0.0.1:6379> CLIENT KILL <IP>:<port>

Get Current Configuration

To get the active configuration on the server, run the command:

127.0.0.1:6379> config get *

Change Configuration at Runtime

To set or change a configuration at runtime, use the command:

127.0.0.1:6379> CONFIG SET DIRECTIVE VALUE

Managing Keys

There following are useful commands when working with Redis keys:

127.0.0.1:6379> keys <pattern

127.0.0.1:6379> keys [a-c]* // uses grep-like pattern

127.0.0.1:6379> DEL <key> // delete key

127.0.0.1:6379> EXISTS <key> // check if key exists

127.0.0.1:6379> EXPIRE <key> <duration_in_seconds> // set key to expire in set duration

127.0.0.1:6379> GET <key> // get the value of the specified key

127.0.0.1:6379> SET <key> <value> // set new key value pair

127.0.0.1:6379> MGET <key1 key2 key3…keyN> // batch fetch values

127.0.0.1:6379> MSET <key1 value1 key2 value2…keyN valueN> // batch set key value pairs

127.0.0.1:6379> RENAME old_key_name new_key_name // rename a key

127.0.0.1:6379> TYPE <key> // get key type

Transaction Commands

If you want to execute a transaction in Redis, use the following commands:

127.0.0.1:6379> MULTI // start transaction

127.0.0.1:6379> EXEC // exec the unit

127.0.0.1:6379> DISCARD // discard commands

127.0.0.1:6379> WATCH // watch key changes

127.0.0.1:6379> UNWATCH // unwatch keys

Other Commands

Other commands include:

127.0.0.1:6379> AUTH <username> <password> // auth username and password

127.0.0.1:6379> PING // test if server is up

127.0.0.1:6379> QUIT // exit CLI

Closing

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