Redis Commands: A Deep Dive
Redis Commands: Classification and Usage
This document provides a detailed overview of Redis commands, categorized by their function. It includes descriptions, examples, and important warnings where applicable. Focus is given to commands useful in managing Redis clusters, optimizing performance, and managing clients.
Cluster Management
Redis Cluster provides a way to run a Redis installation where data is automatically split across multiple Redis nodes.
-
CLUSTER INFO
: Returns information about the Redis Cluster.- Description: Provides general information and statistics about the cluster, such as the cluster size, number of connected nodes, and slot allocation.
- Example:
CLUSTER INFO
-
CLUSTER NODES
: Lists the nodes in the Redis Cluster.- Description: Returns a list of all known nodes in the cluster, along with their IDs, addresses, roles (master or replica), and assigned slots. This is a critical command for understanding the cluster's topology.
- Example:
CLUSTER NODES
-
CLUSTER SLOTS
: Returns details about which slots each node is responsible for.- Description: Returns an array of slot ranges and the nodes serving those ranges. Provides a clear view of how data keys are distributed across the cluster.
- Example:
CLUSTER SLOTS
-
CLUSTER ADDSLOTS <slot> [slot ...]
: Assigns slots to the current node.- Description: This command is used to manually assign one or more slots to a specific Redis node in the cluster. It's typically utilized during cluster creation or rebalancing.
- Example:
CLUSTER ADDSLOTS 1 2 3 4
(adds slots 1, 2, 3, and 4 to the current node) - Caution: Incorrect slot assignments can lead to data inconsistencies and cluster instability.
-
CLUSTER DELSLOTS <slot> [slot ...]
: Removes slots from the current node.- Description: This command removes one or more slots from the current node. It's used during cluster rebalancing or when a node is being removed from the cluster.
- Example:
CLUSTER DELSLOTS 1 2 3 4
(removes slots 1, 2, 3, and 4 from the current node) - Caution: Ensure no data loss occurs when deleting slots. Migrate data beforehand using
CLUSTER SETSLOT MIGRATING
.
-
CLUSTER FLUSHSLOTS
: Removes all slots from the current node.- Description: Empties the current node's slot assignment, making it responsible for no slots.
- Example:
CLUSTER FLUSHSLOTS
- Caution: Requires coordination with cluster rebalancing to ensure data availability.
-
CLUSTER SETSLOT <slot> IMPORTING <node_id>
: Sets a slot to importing state for replica migration.- Description: Used in replica migration, sets the specified slot to the
IMPORTING
state. Data for that slot will be fetched from the node specified by<node_id>
. - Example:
CLUSTER SETSLOT 1234 IMPORTING 8e89b097aed2a903be0592f00394567b9c551c2
- Description: Used in replica migration, sets the specified slot to the
-
CLUSTER SETSLOT <slot> MIGRATING <node_id>
: Sets a slot to migrating state.- Description: Used when migrating a slot. Tells the node that we are migrating a slot from it to another instance.
- Example:
CLUSTER SETSLOT 1234 MIGRATING 8e89b097aed2a903be0592f00394567b9c551c2
-
CLUSTER SETSLOT <slot> NODE <node_id>
: Assigns a slot to a node.- Description: Assigns a specific slot to a particular node within the cluster. Crucial for rebalancing and data redistribution.
- Example:
CLUSTER SETSLOT 1234 NODE 8e89b097aed2a903be0592f00394567b9c551c2
-
CLUSTER SETSLOT <slot> STABLE
: Sets the slot as stable (no import / migrating in progress).- Description: Sets a slot's state to stable, meaning neither import nor migrating operations are in progress.
- Example:
CLUSTER SETSLOT 1234 STABLE
-
CLUSTER REPLICATE <node_id>
: Makes the current node a replica of the specified master node.- Description: Configures the current node as a replica of the master node identified by
<node_id>
. This command is essential for adding redundancy and fault tolerance to the cluster. - Example:
CLUSTER REPLICATE 8e89b097aed2a903be0592f00394567b9c551c2
- Description: Configures the current node as a replica of the master node identified by
-
SLOTSCAN
: Iterates over slots in a Redis Cluster. While functionally similar toSCAN
,SLOTSCAN
is designed specifically for Redis clusters. UseSCAN
within each node after usingCLUSTER SLOTS
to find the relevant nodes to scan.- Description: This command is used to scan all the slots in a Redis Cluster. It's useful for tasks like rebalancing the cluster or checking the distribution of keys. In general, SCAN is preferable for most uses.
- Example:
SLOTSCAN
(generally, use SCAN instead with more specific node targeting after determining slot ownership withCLUSTER SLOTS
)
-
NODESCAN
: Iterates over nodes in a Redis Cluster. In generally you should monitor the nodes through CLI, because of high impact on server by using this command.- Description: This command is used to manually trigger a full scan of the cluster's node table, which will attempt to discover any new nodes in the cluster and will update the data of any known nodes.
- Example:
NODESCAN
Performance Optimization and Caching
HOTKEY
: Identifies frequently accessed "hot keys".- Description: This command is used to identify the keys that are accessed most frequently in a Redis database. This information can be used to optimize caching strategies and improve performance. Requires the Redis Monitoring API be enabled. Often requires custom tooling or scripts.
- Example:
HOTKEY
PSLOWLOG
: Returns entries from the slow log (commands that take longer to execute).- Description: This command is used to retrieve entries from the slow log, which records commands that take longer than a specified time to execute. This information can be used to identify performance bottlenecks and optimize slow-running queries.
- Example:
SLOWLOG GET 10
(retrieves the 10 most recent slow log entries) - Note:
PSLOWLOG
in your original document implies a proxy-specific slow log, but this isn't standard Redis. I've corrected it to the standardSLOWLOG
command.
DEBUG OBJECT <key>
: Provides detailed debugging information about a Redis object. Useful for understanding memory usage, encoding type, and other internal details.- Description: Prints debugging information about the internal representation of a Redis value.
- Example:
DEBUG OBJECT mykey
Client Management
-
CLIENT LIST
: Returns information about connected clients.- Description: This command is used to retrieve information about the clients that are currently connected to the Redis server. This information includes the client's IP address, port, idle time, and the last command executed.
- Example:
CLIENT LIST
-
CLIENT KILL <ip:port>
orCLIENT KILL ID <client-id>
: Kills a specific client connection.- Description: Terminates the connection of a client identified by its IP address and port, or its client ID. Useful for managing resource usage and disconnecting problematic clients.
- Example:
CLIENT KILL 127.0.0.1:6379
CLIENT KILL ID 5
-
CLIENT PAUSE <timeout-milliseconds>
: Pauses all client connections for a specified duration.- Description: This pauses all the clients to perform some management actions (e.g. upgrading your Redis server) in atomic way.
- Example: CLIENT PAUSE 5000 (Pause all clients for 5 seconds)
Data Deletion (Caution!)
-
FLUSHDB [ASYNC]
: Deletes all keys in the currently selected database.- Description: Removes all keys from the current database. The
ASYNC
option allows the command to return immediately without waiting for the operation to complete. This can be useful for large databases. However, be aware that the background cleanup process can still impact performance. - Example:
FLUSHDB ASYNC
- Caution: Ensure you understand the impact of deleting all data in the current database.
- Description: Removes all keys from the current database. The
-
FLUSHALL [ASYNC]
: Asynchronously flushes all data in all Redis instances.- Description: This command is used to asynchronously flush all data in all Redis instances, including masters and replicas. This command should be used with extreme caution, as it will result in complete data loss. The
ASYNC
option ensures that the command returns immediately without waiting for the flush operation to complete. This is a globally dangerous operations. - Example:
FLUSHALL ASYNC
- Caution: This command is extremely dangerous and should only be used when absolutely necessary. Double (or triple) check that this is REALLY what you want to do.
- Description: This command is used to asynchronously flush all data in all Redis instances, including masters and replicas. This command should be used with extreme caution, as it will result in complete data loss. The
Proxy Functionality
PROXY related commands
: Commands related to proxy functionality.- Description: These commands relate to the proxy's functionality, managing connections and routing requests to the correct Redis instances. Specific proxy commands would depend on the proxy being used (e.g., Twemproxy, Redis Enterprise Proxy). There is no universal
PROXY
command in the standard Redis distribution. Command sets will vary by proxy used. - Example: (This will vary depending on the proxy being used). Consult your proxy's documentation. Examples would include health check endpoints, routing rules, and connection pool management.
- Description: These commands relate to the proxy's functionality, managing connections and routing requests to the correct Redis instances. Specific proxy commands would depend on the proxy being used (e.g., Twemproxy, Redis Enterprise Proxy). There is no universal
Limitations
Commands without Cross-Slot Support
: Commands that cannot operate across multiple slots in a Redis Cluster.- Description: This refers to commands that cannot operate across multiple slots within a Redis Cluster. Such commands need all related keys to reside in the same slot. This is a fundamental limitation of Redis Cluster. To work around this, consider key tagging or data modeling strategies.
- Example: Many basic Redis commands like
GET mykey1 mykey2
,MGET
,MSET
fall into this category when the keysmykey1
andmykey2
are assigned to different slots. - Workaround:
- Key Tagging: Use hash tags in your keys to ensure related keys are stored in the same slot (e.g.,
SET {user:1}:name "John"
andSET {user:1}:email "[email protected]"
will be in the same slot). - Data Modeling: Redesign your data model to minimize the need for cross-slot operations. Consider duplicating data in scenarios where cross-slot access is frequent.
- Key Tagging: Use hash tags in your keys to ensure related keys are stored in the same slot (e.g.,
String commands
- BITCOUNT key [start end]: Count the number of set bits (population counting) in a string.
- BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]: Perform bitfield operations on a string.
- BITOP operation destkey key [key ...]: Perform bitwise operations between strings.
- BITPOS key bit [start] [end]: Find the first bit set or clear in a string.
- 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 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 number.
- INCRBYFLOAT key increment: Increment the floating point 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 [EX seconds] [PX milliseconds] [NX|XX] [KEEPTTL]: Set the string 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.
- STRLEN key: Get the length of the value stored in a key.
- APPEND key value Append a value to a key
More classifications will be added upon request
Conclusion
This document provides a detailed overview of many important Redis commands. Be sure to consult the official Redis documentation for the complete list of commands and all their options. Remember to exercise caution when using commands that can result in data loss or performance degradation, especially in production environments.