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 -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:
Drop DB
To delete databases in Redis, use the commands:
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:
Database Backup
To run a backup of your database, you can use the commands:
Show Connections
To get the list of connected clients and their information, run the command:
Terminate Connection
To terminate a specific client connection, use the command:
Get Current Configuration
To get the active configuration on the server, run the command:
Change Configuration at Runtime
To set or change a configuration at runtime, use the command:
Managing Keys
There following are useful commands when working with Redis keys:
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> 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> 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!!