Redis CLI (Command Line Interface) is a powerful tool that allows users to interact directly with the Redis data store. It's an essential utility for developers and database administrators, enabling them to perform various operations, from simple key-value manipulations to complex administrative tasks. This article provides an in-depth look at Redis CLI, covering its usage and a comprehensive list of all available commands.
Getting Started with Redis CLI
Redis CLI is typically included when you install Redis. To start the CLI, simply open your terminal and type:
redis-cli
This command connects to a Redis server running on localhost
at the default port 6379
. You can also specify a different host and port:
redis-cli -h <hostname> -p <port>
Once connected, you can start issuing commands to interact with your Redis instance.
Basic Usage
The Redis CLI allows you to execute any Redis command. For example, to set and get a key-value pair:
127.0.0.1:6379> SET mykey "Hello, Redis!"
OK
127.0.0.1:6379> GET mykey
"Hello, Redis!"
Comprehensive List of Redis Commands
Here is a detailed list of all the Redis commands, categorized by their functionality:
1. String Commands
APPEND key value
: Append a value to a key.BITCOUNT key [start end]
: Count set bits in a string.BITOP operation destkey key [key ...]
: Perform bitwise operations between strings.DECR key
: Decrement the integer value of a key by one.DECRBY key decrement
: Decrement the integer value of a key by the given number.GET key
: Get the value of a key.GETBIT key offset
: Returns the bit value at offset in the string value stored at key.GETRANGE key start end
: Get a substring of the string stored at a key.GETSET key value
: Set the string value of a key and return its old value.INCR key
: Increment the integer value of a key by one.INCRBY key increment
: Increment the integer value of a key by the given amount.INCRBYFLOAT key increment
: Increment the float value of a key by the given amount.MGET key [key ...]
: Get the values of all the given keys.MSET key value [key value ...]
: Set multiple keys to multiple values.MSETNX key value [key value ...]
: Set multiple keys to multiple values, only if none of the keys exist.PSETEX key milliseconds value
: Set the value and expiration in milliseconds of a key.SET key value [NX|XX] [EX seconds|PX milliseconds]
: Set the value of a key.SETBIT key offset value
: Sets or clears the bit at offset in the string value stored at key.SETEX key seconds value
: Set the value and expiration of a key.SETNX key value
: Set the value of a key, only if the key does not exist.SETRANGE key offset value
: Overwrite part of a string at key starting at the specified offset.STRLEN key
: Get the length of the value stored in a key.
2. List Commands
BLPOP key [key ...] timeout
: Remove and get the first element in a list, or block until one is available.BRPOP key [key ...] timeout
: Remove and get the last element in a list, or block until one is available.BRPOPLPUSH source destination timeout
: Pop a value from a list, push it to another list and return it; or block until one is available.LINDEX key index
: Get an element from a list by its index.LINSERT key BEFORE|AFTER pivot value
: Insert an element before or after another element in a list.LLEN key
: Get the length of a list.LPOP key
: Remove and get the first element in a list.LPUSH key value [value ...]
: Prepend one or multiple values to a list.LPUSHX key value
: Prepend a value to a list, only if the list exists.LRANGE key start stop
: Get a range of elements from a list.LREM key count value
: Remove elements from a list.LSET key index value
: Set the value of an element in a list by its index.LTRIM key start stop
: Trim a list to the specified range.
3. Set Commands
SADD key member [member ...]
: Add one or more members to a set.SCARD key
: Get the number of members in a set.SDIFF key [key ...]
: Subtract multiple sets.SDIFFSTORE destination key [key ...]
: Subtract multiple sets and store the resulting set in a key.SINTER key [key ...]
: Intersect multiple sets.SINTERSTORE destination key [key ...]
: Intersect multiple sets and store the resulting set in a key.SISMEMBER key member
: Determine if a given value is a member of a set.SMEMBERS key
: Get all the members in a set.SMOVE source destination member
: Move a member from one set to another.SPOP key [count]
: Remove and return one or multiple random members from a set.SRANDMEMBER key [count]
: Get one or multiple random members from a set.SREM key member [member ...]
: Remove one or more members from a set.SUNION key [key ...]
: Add multiple sets.SUNIONSTORE destination key [key ...]
: Add multiple sets and store the resulting set in a key.
4. Hash Commands
HDEL key field [field ...]
: Delete one or more hash fields.HEXISTS key field
: Determine if a hash field exists.HGET key field
: Get the value of a hash field.HGETALL key
: Get all the fields and values in a hash.HINCRBY key field increment
: Increment the integer value of a hash field by the given number.HINCRBYFLOAT key field increment
: Increment the float value of a hash field by the given amount.HKEYS key
: Get all the fields in a hash.HLEN key
: Get the number of fields in a hash.HMGET key field [field ...]
: Get the values of all the given hash fields.HMSET key field value [field value ...]
: Set multiple hash fields to multiple values.HSET key field value
: Set the string value of a hash field.HSETNX key field value
: Set the value of a hash field, only if the field does not exist.HVALS key
: Get all the values in a hash.
5. Sorted Set Commands
ZADD key score member [score member ...]
: Add one or more members to a sorted set, or update its score if it already exists.ZCARD key
: Get the number of members in a sorted set.ZCOUNT key min max
: Count the members in a sorted set with scores within the given values.ZINCRBY key increment member
: Increment the score of a member in a sorted set.ZINTERSTORE destination numkeys key [key ...]
: Intersect multiple sorted sets and store the resulting sorted set in a key.ZRANGE key start stop [WITHSCORES]
: Return a range of members in a sorted set, by index.ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
: Return a range of members in a sorted set, by score.ZRANK key member
: Determine the index of a member in a sorted set.ZREM key member [member ...]
: Remove one or more members from a sorted set.ZREMRANGEBYRANK key start stop
: Remove all members in a sorted set within the given indexes.ZREMRANGEBYSCORE key min max
: Remove all members in a sorted set within the given scores.ZREVRANGE key start stop [WITHSCORES]
: Return a range of members in a sorted set, by index, with scores ordered from high to low.ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
: Return a range of members in a sorted set, by score, with scores ordered from high to low.ZREVRANK key member
: Determine the index of a member in a sorted set, with scores ordered from high to low.ZSCORE key member
: Get the score associated with the given member in a sorted set.ZUNIONSTORE destination numkeys key [key ...]
: Add multiple sorted sets and store the resulting sorted set in a key.
6. HyperLogLog Commands
PFADD key element [element ...]
: Adds the specified elements to the specified HyperLogLog.PFCOUNT key [key ...]
: Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).PFMERGE destkey sourcekey [sourcekey ...]
: Merge N different HyperLogLogs into
a single one.
7. Geo Commands
GEOADD key longitude latitude member [longitude latitude member ...]
: Add one or more geospatial items to the geospatial index represented using a sorted set.GEODIST key member1 member2 [unit]
: Returns the distance between two members of a geospatial index.GEOHASH key member [member ...]
: Returns members of a geospatial index as standard geohash strings.GEOPOS key member [member ...]
: Returns longitude and latitude of members of a geospatial index.GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC]
: Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point.GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC]
: Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member.
8. Stream Commands
XACK key group id [id ...]
: Acknowledge one or more messages as processed.XADD key ID field value [field value ...]
: Append a new entry to a stream.XCLAIM key group consumer min-idle-time id [id ...] [IDLE ms] [TIME ms-unix-time] [RETRYCOUNT count] [FORCE] [JUSTID]
: Changes the ownership of a pending message.XDEL key id [id ...]
: Remove one or more messages from a stream.XGROUP CREATE key groupname id|$ [MKSTREAM]
: Create a consumer group.XGROUP SETID key groupname id|$
: Set the consumer group last delivered ID.XGROUP DELCONSUMER key groupname consumername
: Remove a consumer from a consumer group.XGROUP DESTROY key groupname
: Remove the consumer group.XINFO STREAM key [FULL]
: Get information about a stream.XINFO GROUPS key
: Get information about the consumer groups of a stream.XINFO CONSUMERS key groupname
: Get information about the consumers of a consumer group.XLEN key
: Return the number of entries in a stream.XPENDING key group [start end count] [consumer]
: Get information about pending messages.XRANGE key start end [COUNT count]
: Return a range of messages in a stream.XREVRANGE key end start [COUNT count]
: Return a range of messages in a stream, in reverse order.XREAD [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] ID [ID ...]
: Return the stream entries matching a given ID or IDs.XREADGROUP GROUP group consumer [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] ID [ID ...]
: Return the stream entries matching a given ID or IDs, using a consumer group.XTRIM key MAXLEN [~] count
: Trim the stream to the specified number of elements.
9. Pub/Sub Commands
PSUBSCRIBE pattern [pattern ...]
: Listen for messages published to channels matching the given patterns.PUBLISH channel message
: Post a message to a channel.PUNSUBSCRIBE [pattern [pattern ...]]
: Stop listening for messages posted to channels matching the given patterns.SUBSCRIBE channel [channel ...]
: Listen for messages published to the given channels.UNSUBSCRIBE [channel [channel ...]]
: Stop listening for messages posted to the given channels.
10. Transaction Commands
DISCARD
: Discard all commands issued after MULTI.EXEC
: Execute all commands issued after MULTI.MULTI
: Mark the start of a transaction block.UNWATCH
: Forget about all watched keys.WATCH key [key ...]
: Watch the given keys to determine execution of the MULTI/EXEC block.
11. Scripting Commands
EVAL script numkeys key [key ...] arg [arg ...]
: Execute a Lua script server-side.EVALSHA sha1 numkeys key [key ...] arg [arg ...]
: Execute a Lua script server-side by its SHA1 hash.SCRIPT EXISTS sha1 [sha1 ...]
: Check existence of scripts in the script cache.SCRIPT FLUSH
: Remove all the scripts from the script cache.SCRIPT KILL
: Kill the currently executing Lua script.SCRIPT LOAD script
: Load a script into the script cache without executing it.
12. Connection Commands
AUTH password
: Authenticate to the server.ECHO message
: Echo the given string.PING [message]
: Ping the server.QUIT
: Close the connection.SELECT index
: Change the selected database for the current connection.
13. Server Commands
BGREWRITEAOF
: Asynchronously rewrite the append-only file.BGSAVE
: Asynchronously save the dataset to disk.CLIENT KILL ip:port
: Kill the connection of a client.CLIENT LIST
: Get the list of client connections.CLIENT GETNAME
: Get the current connection name.CLIENT PAUSE timeout
: Stop processing commands from clients for a specified time.CLIENT SETNAME connection-name
: Set the current connection name.COMMAND
: Get array of Redis command details.CONFIG GET parameter
: Get the value of a configuration parameter.CONFIG REWRITE
: Rewrite the configuration file with the in-memory configuration.CONFIG SET parameter value
: Set a configuration parameter to the given value.CONFIG RESETSTAT
: Reset the stats returned by INFO.DBSIZE
: Return the number of keys in the selected database.DEBUG OBJECT key
: Get debugging information about a key.DEBUG SEGFAULT
: Make the server crash.FLUSHALL
: Remove all keys from all databases.FLUSHDB
: Remove all keys from the current database.INFO [section]
: Get information and statistics about the server.LASTSAVE
: Get the UNIX time stamp of the last successful save to disk.MONITOR
: Listen for all requests received by the server in real-time.ROLE
: Return the role of the instance in the context of replication.SAVE
: Synchronously save the dataset to disk.SHUTDOWN [NOSAVE|SAVE]
: Synchronously save the dataset to disk and then shut down the server.SLAVEOF host port
: Make the server a replica of another instance, or promote it as master.SLOWLOG subcommand [argument]
: Manages the Redis slow queries log.SYNC
: Internal command used for replication.TIME
: Return the current server time.
14. Cluster Commands
CLUSTER ADDSLOTS slot [slot ...]
: Assign new hash slots to receiving node.CLUSTER COUNT-FAILURE-REPORTS node-id
: Return the number of failure reports active for a given node.CLUSTER COUNTKEYSINSLOT slot
: Return the number of local keys in the specified hash slot.CLUSTER DELSLOTS slot [slot ...]
: Set hash slots as unbound in receiving node.CLUSTER FAILOVER [FORCE|TAKEOVER]
: Forces a replica to perform a manual failover of its master.CLUSTER FORGET node-id
: Remove a node from the nodes table.CLUSTER GETKEYSINSLOT slot count
: Return local keys in the specified hash slot.CLUSTER INFO
: Provides info about Redis Cluster node state.CLUSTER KEYSLOT key
: Returns the hash slot of the specified key.CLUSTER MEET ip port
: Force a node cluster to handshake with another node.CLUSTER NODES
: Get cluster config for the node.CLUSTER REPLICATE node-id
: Reconfigure a node as a replica of the specified master node.CLUSTER RESET [HARD|SOFT]
: Reset a Redis Cluster node.CLUSTER SAVECONFIG
: Forces the node to save cluster state on disk.CLUSTER SET-CONFIG-EPOCH config-epoch
: Set the configuration epoch in a new node.CLUSTER SETSLOT slot IMPORTING|MIGRATING|STABLE|NODE node-id
: Bind a hash slot to a specific node.CLUSTER SLAVES node-id
: List replica nodes of the specified master node.CLUSTER SLOTS
: Get array of cluster slots mapping.
15. Sentinel Commands
SENTINEL GET-MASTER-ADDR-BY-NAME master-name
: Return the IP and port number of the master with the specified name.SENTINEL FAILOVER master-name
: Force a failover as if the master was not reachable.SENTINEL MONITOR name ip port quorum
: Add a new master to be monitored by Sentinel.SENTINEL REMOVE master-name
: Remove a master from Sentinel's monitoring.SENTINEL SENTINELS master-name
: Show a list of Sentinel instances for this master.SENTINEL SET master-name [option value ...]
: Set Sentinel's monitoring parameters for a given master.SENTINEL SLAVES master-name
: Show a list of replica instances for this master.
Conclusion
The Redis CLI is an invaluable tool for managing Redis instances, offering a wide range of commands to handle all aspects of Redis operations. Whether you are performing basic key-value manipulations, managing data structures, or conducting advanced administrative tasks, Redis CLI provides the necessary functionality. Understanding and mastering these commands can significantly enhance your ability to work efficiently with Redis, making it easier to leverage its powerful features for your applications.