Guides on Using the Commands in Redis Cheat Sheet Part 1
Introduction
This is part one of a Redis cheat sheet tutorial series. Redis, an acronym for Remote Directory Server, is an in-memory database that supports multiple data types. Redis works by mapping keys to values that functions as a key-value-based caching system. Redis commands perform some operations on Redis server, with some commands being able to be executed from the Redis command-line interface, or CLI. The CLI is an uncomplicated program that permits the sending of commands to and reading the replies from Redis directly from the terminal. This fist section of this tutorial series will provide instructions for using many of the Redis commands.
Prerequisites
A properly running Redis server and Redis CLI installed and working on the local device are required to follow the examples provided in this Redis cheat sheet tutorial.
Execute of the
redis-cli PING
command will return an output ofPONG
if Redis is installed. Thesudo systemctl status redis
command in a Linux distribution system will also verify if Redis is running.
Display the information on the Redis cli server statics
Execute the following INFO
command to obtain the statistics of the Redis server in a readable format:
1 | redis-cli INFO |
The results should provide information similar to the following on the parameters that a computer is able to parse:
server
: Information about the Redis server on the local device, such as the version number and pertinent data.clients
: The section where the clients connections are counted.memory
: Provides information on memory usage of the server and other related data.persistence
: Gives information about the Redis database backup and the Append Only file.stats
: Provides general information regarding the statistics in Redis.replication
: Supplies role master and replication data.cpu
: Information about CPU consumption.cluster
: This shows the cluster section of the Redis server.keyspace
: Relates to database statistics.
Accessing the interactive cli tool for Redis
Execute the redis-cli
command in the terminal to open the prompt that accesses the Redis command-line interface tool.
- Execute the following command to monitor the currently active commands in Redis:
1 2 3 4 5 | 127.0.0.1:6379> monitor OK 1580867465.612664 [0 127.0.0.1:51038] "PING" 1580867466.645500 [0 127.0.0.1:51038] "PING" 1580867466.708354 [0 127.0.0.1:51038] "PUBLISH" "__sentinel__:hello" "127.0.0.1,26379,05db92f504c2d072ff76c8d566502be6b73514c1,0,mymaster,127.0.0.1,6379,0" |
NOTE: Executing the CTRL + C will end the above operation.
- The following command will display the maximum number of the databases in Redis:
1 2 3 | 127.0.0.1:6379> config get databases 1) "databases" 2) "16" |
- The following command will list all available databases:
1 2 3 | info "keyspace" # Keyspace db0:keys=3,expires=0,avg_ttl=0 |
- Now execute the following command to select the desired database:
1 | SELECT index |
This will present an index of databases, displayed as numbers. For example, the default database in Redis is [0], so executing this command allows for switching to another database. The output should resemble the following:
1 | 127.0.0.1:6379[1]> |
Now the second database, which has the index of [1]
, can be selected.
- Execute the following command to drop the currently selected database:
1 2 | FLUSHDB OK |
Note that changing the DB
to ALL
after the FLUSH
command will drop all of the databases.
String commands in Redis
The basic type of value in Redis is known as a string and may contain any type of the data.
The APPEND function
- This shows how to append the key value:
1 2 3 4 5 6 | EXISTS mykey (integer) 1 APPEND mykey World (integer) 10 GET mykey "HelloWorld" |
BITCOUNT function
- This counts the set bits number.
1 2 | BITCOUNT mykey 1 1 (integer) 4 |
The DECR function
- This will decrement the integer by one:
1 2 3 4 | SET count 21 OK DECR count (integer) 20 |
The DECRBY function
- This function decrements the integer value of given number:
1 2 | DECRBY count 5 (integer) 15 |
The GET function
- This returns the value of the key:
1 2 3 4 | GET mykey "HelloWorld" GET count "15" |
The GETRANGE function
- This will return the substring value of the key:
1 2 | GETRANGE mykey 5 8 "Worl" |
The GETSET function
- This function will set the new value of the key and return the old value:
1 2 3 4 5 6 7 8 | GETSET count 0 "15" GETSET count 1 "0" GETSET count 12 "1" GETSET count 0 "12" |
The INCR function
- This increments the value of the key:
1 2 3 4 5 6 | INCR count (integer) 1 INCR count (integer) 2 INCR count (integer) 3 |
The INCRBY function
- This function will increment the integer value of given number:
1 2 | INCRBY count 100 (integer) 103 |
The INCRBYFLOAT function
- This will increment the given value of the key by a float value:
1 2 | INCRBYFLOAT count 2.6 "105.6" |
The MSET function
- This function sets a multiple key with a multiple-key value:
1 2 | MSET key1 "How " key2 "are " key3 "you?" OK |
The MGET function
- This will return the value of all of the given keys:
1 2 3 4 | MGET key1 key2 key3 1) "How " 2) "are " 3) "you?" |
The MSETNX function
- This function sets the multiple key value, but only if no value currently exists in the keys:
1 2 3 4 | MSETNX 1key "I'm" 2key "fine" (integer) 1 MSETNX 2key "hey" 3key "thankyou" (integer) 0 |
The SET function
- This sets the key value:
1 2 | SET kv "Redis" OK |
The SETBIT function
- This will offset the bit in the string value:
1 2 | SETBIT kv 5 1 (integer) 0 |
The SETNX function
- Like the MSETNX function, this will set the key value if no value currently exists:
1 2 3 4 | SETNX value "ObjectRocket" (integer) 1 SETNX value "orkb" (integer) 0 |
The SETRANGE function
- This function will overwrite the stored string in the key starting with a specified offset:
1 2 | SETRANGE mykey 5 " Redis" (integer) 11 |
The STRLEN function
- This returns the length of the key value:
1 2 | STRLEN mykey (integer) 11 |
Conclusion
This was part one in a Redis cheat sheet tutorial series that gave instructions for using many of the more common and basic Redis commands. Part one of of this series covered how to display the information on the Redis CLI server, how to access the interactive CLI tool for Redis and provided examples for over a dozen functions for Redis string commands. Remember that while the Redis CLI is a very useful tool for executing many Redis commands, it is not suitable for executing some of the more complicated Redis functions. Subsequent parts of this tutorial series will cover more of the commonly used Redis commands.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started