Connection & Server Commands
Command |
Example |
Description |
redis-cli |
redis-cli -h host -p port -a password |
Starts the CLI. Can connect to a remote/protected instance. |
PING |
PING |
Checks the connection. Should return PONG . |
INFO [section] |
INFO memory |
Provides detailed information and statistics about the server. |
FLUSHDB |
FLUSHDB |
Deletes all keys in the current database. |
FLUSHALL |
FLUSHALL |
DANGER: Deletes all keys in all databases on the server. |
Key Management
These commands work on keys of any data type.
Command |
Example |
Description |
KEYS <pattern> |
KEYS user:* |
Finds all keys matching a pattern. Warning: Slow, avoid in production. Use SCAN . |
EXISTS <key> |
EXISTS user:101 |
Checks if a key exists. Returns 1 if it does, 0 if not. |
DEL <key> ... |
DEL user:101 temp:session |
Deletes one or more keys. |
TYPE <key> |
TYPE mylist |
Returns the data type of the value stored at a key (e.g., "string", "list", "hash"). |
EXPIRE <key> <secs> |
EXPIRE session:xyz 3600 |
Sets a timeout on a key (in seconds). The key is auto-deleted after expiry. |
TTL <key> |
TTL session:xyz |
Gets the remaining time to live of a key with a timeout. |
PERSIST <key> |
PERSIST session:xyz |
Removes the timeout from a key. |
RENAME <key> <newkey> |
RENAME user:101 user:102 |
Renames a key. |
String Commands
The most basic Redis data type. Simple key-value pairs.
Command |
Example |
Description |
SET <key> <value> |
SET user:101 "Alice" |
Sets the string value of a key, overwriting any existing value. |
GET <key> |
GET user:101 |
Gets the string value of a key. |
SETEX <key> <secs> <val> |
SETEX otp:123 60 "9876" |
Sets a key with an expiration time (atomic operation). |
SETNX <key> <value> |
SETNX lock:resource "true" |
Sets a key only if it does not already exist. Used for locks. |
MGET <key> ... |
MGET user:101 user:102 |
Gets the values of all specified keys. |
MSET <key> <v> ... |
MSET user:101 "Alice" user:102 "Bob" |
Sets multiple keys to multiple values. |
INCR <key> |
INCR page:views |
Increments the number stored at a key by one. |
DECR <key> |
DECR items:in_stock |
Decrements the number stored at a key by one. |
INCRBY <key> <amt> |
INCRBY score 10 |
Increments the number stored at a key by a given amount. |
Hash Commands
Hashes are maps between string fields and string values, ideal for storing objects.
Command |
Example |
Description |
HSET <key> <field> <value> |
HSET user:101 name "Alice" email "[email protected]" |
Sets one or more field-value pairs in a hash. |
HGET <key> <field> |
HGET user:101 name |
Gets the value of a specific field in a hash. |
HMGET <key> <f1> <f2> |
HMGET user:101 name email |
Gets the values of multiple fields in a hash. |
HGETALL <key> |
HGETALL user:101 |
Gets all fields and values in a hash. Warning: Can be slow for large hashes. |
HDEL <key> <field> |
HDEL user:101 email |
Deletes a field from a hash. |
HKEYS <key> |
HKEYS user:101 |
Gets all field names in a hash. |
HVALS <key> |
HVALS user:101 |
Gets all values in a hash. |
HINCRBY <key> <f> <amt> |
HINCRBY user:101 score 5 |
Increments the number in a hash field by a given amount. |
List Commands
Lists are collections of strings sorted by insertion order. Useful for queues and timelines.
Command |
Example |
Description |
LPUSH <key> <value> |
LPUSH tasks "task1" |
Adds a value to the beginning (left) of a list. |
RPUSH <key> <value> |
RPUSH logs "log entry" |
Adds a value to the end (right) of a list. |
LPOP <key> |
LPOP tasks |
Removes and returns the first element of a list. |
RPOP <key> |
RPOP logs |
Removes and returns the last element of a list. |
LRANGE <key> <start> <stop> |
LRANGE tasks 0 -1 |
Gets a range of elements from a list. 0 -1 gets all elements. |
LLEN <key> |
LLEN tasks |
Gets the length of a list. |
BLPOP <key> <timeout> |
BLPOP tasks 10 |
Blocking version of LPOP . Waits for an element to appear (up to timeout in seconds). |
Set Commands
Sets are unordered collections of unique strings.
Command |
Example |
Description |
SADD <key> <member> |
SADD tags "redis" |
Adds a member to a set. Ignores it if it already exists. |
SMEMBERS <key> |
SMEMBERS tags |
Returns all members of a set. |
SISMEMBER <key> <member> |
SISMEMBER tags "redis" |
Checks if a member exists in a set. Returns 1 or 0 . |
SREM <key> <member> |
SREM tags "mongo" |
Removes a member from a set. |
SCARD <key> |
SCARD tags |
Gets the number of members in a set (cardinality). |
SUNION <k1> <k2> |
SUNION groupA groupB |
Returns the union of multiple sets. |
SINTER <k1> <k2> |
SINTER groupA groupB |
Returns the intersection of multiple sets. |
Sorted Set (ZSET) Commands
Sorted Sets are like Sets, but each member is associated with a score, which is used for ordering. Great for leaderboards.
Command |
Example |
Description |
ZADD <key> <score> <member> |
ZADD leaderboard 100 "Alice" |
Adds a member with a score to a sorted set. |
ZRANGE <key> <start> <stop> |
ZRANGE leaderboard 0 9 |
Gets a range of members by rank (lowest score first). |
ZREVRANGE <k> <start> <stop> |
ZREVRANGE leaderboard 0 9 |
Gets a range of members by rank (highest score first). |
ZRANGEBYSCORE <k> <min> <max> |
ZRANGEBYSCORE scores 80 100 |
Gets a range of members by score. |
ZREM <key> <member> |
ZREM leaderboard "Bob" |
Removes a member from a sorted set. |
ZCARD <key> |
ZCARD leaderboard |
Gets the number of members in a sorted set. |
ZINCRBY <key> <amt> <member> |
ZINCRBY leaderboard 5 "Alice" |
Increments the score of a member by a given amount. |
ZRANK <key> <member> |
ZRANK leaderboard "Alice" |
Gets the rank of a member (0-based, lowest score is rank 0). |
Publish / Subscribe
Command |
Example |
Description |
SUBSCRIBE <channel> |
SUBSCRIBE news |
Subscribes to a channel. This is a blocking command. |
PUBLISH <channel> <msg> |
PUBLISH news "Hello" |
Publishes a message to a channel. |
UNSUBSCRIBE [channel] |
UNSUBSCRIBE news |
Unsubscribes from one or more channels. |