- | - |
---|---|
PSUBSCRIBE | Listen for messages published to channels matching the given patterns |
PUBSUB | Inspect the state of the Pub/Sub subsystem |
PUBLISH | Post a message to a channel |
PUNSUBSCRIBE | Stop listening for messages posted to channels matching the given patterns |
SUBSCRIBE | Listen for messages published to the given channels |
UNSUBSCRIBE | Stop listening for messages posted to the given channels |
Redis Cheat Sheet
This is a redis quick reference cheat sheet that lists examples of redis commands
Miscellaneous
Pubsub
Scripting
- | - |
---|---|
EVAL | Execute a Lua script server side |
EVALSHA | Execute a Lua script server side |
SCRIPT DEBUG | Set the debug mode for executed scripts. |
SCRIPT EXISTS | Check existence of scripts in the script cache. |
SCRIPT FLUSH | Remove all the scripts from the script cache. |
SCRIPT KILL | Kill the script currently in execution. |
SCRIPT LOAD | Load the specified Lua script into the script cache. |
Cluster
- | - |
---|---|
CLUSTER ADDSLOTS | Assign new hash slots to receiving node |
CLUSTER BUMPEPOCH | Advance the cluster config epoch |
CLUSTER COUNT-FAILURE-REPORTS | Return the number of failure reports active for a given node |
CLUSTER COUNTKEYSINSLOT | Return the number of local keys in the specified hash slot |
CLUSTER DELSLOTS | Set hash slots as unbound in receiving node |
CLUSTER FAILOVER | Forces a replica to perform a manual failover of its master. |
CLUSTER FLUSHSLOTS | Delete a node's own slots information |
CLUSTER FORGET | Remove a node from the nodes table |
CLUSTER GETKEYSINSLOT | Return local key names in the specified hash slot |
CLUSTER INFO | Provides info about Redis Cluster node state |
CLUSTER KEYSLOT | Returns the hash slot of the specified key |
CLUSTER MEET | Force a node cluster to handshake with another node |
CLUSTER MYID | Return the node id |
CLUSTER NODES | Get Cluster config for the node |
CLUSTER REPLICATE | Reconfigure a node as a replica of the specified master node |
CLUSTER RESET | Reset a Redis Cluster node |
CLUSTER SAVECONFIG | Forces the node to save cluster state on disk |
CLUSTER SET-CONFIG-EPOCH | Set the configuration epoch in a new node |
CLUSTER SETSLOT | Bind a hash slot to a specific node |
CLUSTER SLAVES | List replica nodes of the specified master node |
CLUSTER REPLICAS | List replica nodes of the specified master node |
CLUSTER SLOTS | Get array of Cluster slot to node mappings |
READONLY | Enables read queries for a connection to a cluster replica node |
READWRITE | Disables read queries for a connection to a cluster replica node |
Redis Stream command
XACK
XACK key group ID [ID ...]
#Example
redis> XACK mystream mygroup 1526569495631-0
ERR Unknown or disabled command 'XACK'
Marks a pending message as correctly processed, effectively removing it from the pending entries list of the consumer group. Return value of the command is the number of messages successfully acknowledged, that is, the IDs we were actually able to resolve in the PEL.
XREVRANGE
XREVRANGE key end start [COUNT count]
#Example
redis> XADD writers \* name Virginia surname Woolf
"1609040579130-0"
redis> XADD writers \* name Jane surname Austen
"1609040579130-1"
redis> XADD writers \* name Toni surname Morrison
"1609040579130-2"
redis> XADD writers \* name Agatha surname Christie
"1609040579131-0"
redis> XADD writers \* name Ngozi surname Adichie
"1609040579131-1"
redis> XLEN writers
(integer) 5
redis> XREVRANGE writers + - COUNT 1
1) 1) "1609040579131-1"
2) 1) "name"
2) "Ngozi"
3) "surname"
4) "Adichie"
Return a range of elements in a stream, with IDs matching the specified IDs interval, in reverse order (from greater to smaller IDs) compared to XRANGE
XRANGE
XRANGE key start end [COUNT count]
#Example
redis> XADD writers \* name Virginia surname Woolf
"1609040578002-0"
redis> XADD writers \* name Jane surname Austen
"1609040578002-1"
redis> XADD writers \* name Toni surname Morrison
"1609040578003-0"
redis> XADD writers \* name Agatha surname Christie
"1609040578003-1"
redis> XADD writers \* name Ngozi surname Adichie
"1609040578003-2"
redis> XLEN writers
(integer) 5
redis> XRANGE writers - + COUNT 2
1) 1) "1609040578002-0"
2) 1) "name"
2) "Virginia"
3) "surname"
4) "Woolf"
2) 1) "1609040578002-1"
2) 1) "name"
2) "Jane"
3) "surname"
4) "Austen"
Return a range of elements in a stream, with IDs matching the specified IDs interval
XTRIM
XTRIM key MAXLEN [=|~] length
#Example
redis> XADD mystream \* field1 A field2 B field3 C field4 D
"1609040575750-0"
redis> XTRIM mystream MAXLEN 2
(integer) 0
redis> XRANGE mystream - +
1) 1) "1609040575750-0"
2) 1) "field1"
2) "A"
3) "field2"
4) "B"
5) "field3"
6) "C"
7) "field4"
8) "D"
Trims the stream to (approximately if '~' is passed) a certain size
XADD
XADD key [MAXLEN [=|~] length] [NOMKSTREAM] *|ID field value [field value ...]
#Example
redis> XADD mystream \* name Sara surname OConnor
"1609040574632-0"
redis> XADD mystream \* field1 value1 field2 value2 field3 value3
"1609040574632-1"
redis> XLEN mystream
(integer) 2
redis> XRANGE mystream - +
1) 1) "1609040574632-0"
2) 1) "name"
2) "Sara"
3) "surname"
4) "OConnor"
2) 1) "1609040574632-1"
2) 1) "field1"
2) "value1"
3) "field2"
4) "value2"
5) "field3"
6) "value3"
Appends a new entry to a stream
Redis Connection command
CLIENT INFO
CLIENT INFO
#Example
redis> CLIENT INFO
"id=55542 addr=127.0.0.1:58710 laddr=127.0.0.1:6379 fd=8 name= age=114920 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=40928 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 events=r cmd=client user=default redir=-1\n"
Returns information about the current client connection.
Redis Server command
COMMAND INFO
COMMAND INFO command-name [command-name ...]
#Example
redis> COMMAND INFO get set eval
1) 1) "get"
2) (integer) 2
3) 1) "readonly"
2) "fast"
4) (integer) 1
5) (integer) 1
6) (integer) 1
7) 1) "@read"
2) "@string"
3) "@fast"
2) 1) "set"
2) (integer) -3
3) 1) "write"
2) "denyoom"
4) (integer) 1
5) (integer) 1
6) (integer) 1
7) 1) "@write"
2) "@string"
3) "@slow"
3) 1) "eval"
2) (integer) -3
3) 1) "noscript"
2) "movablekeys"
4) (integer) 0
5) (integer) 0
6) (integer) 0
7) 1) "@slow"
2) "@scripting"
Get array of specific Redis command details
COMMAND GETKEYS
COMMAND GETKEYS
#Example
redis> COMMAND GETKEYS MSET a b c d e f
1) "a"
2) "c"
3) "e"
redis> COMMAND GETKEYS EVAL "not consulted" 3 key1 key2 key3 arg1 arg2 arg3 argN
1) "key1"
2) "key2"
3) "key3"
redis> COMMAND GETKEYS SORT mylist ALPHA STORE outlist
1) "mylist"
2) "outlist"
Extract keys given a full Redis command
Redis Hyperloglog command
PFCOUNT
PFCOUNT key [key ...]
#Example
redis> PFADD hll foo bar zap
(integer) 1
redis> PFADD hll zap zap zap
(integer) 0
redis> PFADD hll foo bar
(integer) 0
redis> PFCOUNT hll
(integer) 3
redis> PFADD some-other-hll 1 2 3
(integer) 1
redis> PFCOUNT hll some-other-hll
(integer) 6
Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).
Redis Geo command
GEOSEARCH
GEOSEARCH key [FROMMEMBER member] [FROMLONLAT longitude latitude] [BYRADIUS radius m|km|ft|mi] [BYBOX width height m|km|ft|mi] [ASC|DESC] [COUNT count] [WITHCOORD] [WITHDIST] [WITHHASH]
#Example
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEOADD Sicily 12.758489 38.788135 "edge1" 17.241510 38.788135 "edge2"
(integer) 2
redis> GEOSEARCH Sicily FROMLONLAT 15 37 BYRADIUS 200 km ASC
1) "Catania"
2) "Palermo"
redis> GEOSEARCH Sicily FROMLONLAT 15 37 BYBOX 400 400 km ASC
1) "Catania"
2) "Palermo"
3) "edge2"
4) "edge1"
Query a sorted set representing a geospatial index to fetch members inside an area of a box or a circle.
GEORADIUSBYMEMBER
GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
#Example
redis> GEOADD Sicily 13.583333 37.316667 "Agrigento"
(integer) 1
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEORADIUSBYMEMBER Sicily Agrigento 100 km
1) "Agrigento"
2) "Palermo"
Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member
GEORADIUS
GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
#Example
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEORADIUS Sicily 15 37 200 km WITHDIST
1) 1) "Palermo"
2) "190.4424"
2) 1) "Catania"
2) "56.4413"
redis> GEORADIUS Sicily 15 37 200 km WITHCOORD
1) 1) "Palermo"
2) 1) "13.36138933897018433"
2) "38.11555639549629859"
2) 1) "Catania"
2) 1) "15.08726745843887329"
2) "37.50266842333162032"
redis> GEORADIUS Sicily 15 37 200 km WITHDIST WITHCOORD
1) 1) "Palermo"
2) "190.4424"
3) 1) "13.36138933897018433"
2) "38.11555639549629859"
2) 1) "Catania"
2) "56.4413"
3) 1) "15.08726745843887329"
2) "37.50266842333162032"
Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point
GEODIST
GEODIST key member1 member2 [m|km|ft|mi]
#Example
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEODIST Sicily Palermo Catania
"166274.1516"
redis> GEODIST Sicily Palermo Catania km
"166.2742"
redis> GEODIST Sicily Palermo Catania mi
"103.3182"
redis> GEODIST Sicily Foo Bar
(nil)
Returns the distance between two members of a geospatial index
GEOPOS
GEOPOS key member [member ...]
#Example
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEOPOS Sicily Palermo Catania NonExisting
1) 1) "13.36138933897018433"
2) "38.11555639549629859"
2) 1) "15.08726745843887329"
2) "37.50266842333162032"
3) (nil)
Returns longitude and latitude of members of a geospatial index
GEOADD
GEOADD key longitude latitude member [longitude latitude member ...]
#Example
redis> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
redis> GEODIST Sicily Palermo Catania
"166274.1516"
redis> GEORADIUS Sicily 15 37 100 km
1) "Catania"
redis> GEORADIUS Sicily 15 37 200 km
1) "Palermo"
2) "Catania"
Add one or more geospatial items in the geospatial index represented using a sorted set
Redis Sorted set command
ZUNIONSTORE
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
#Example
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZADD zset2 3 "three"
(integer) 1
redis> ZUNIONSTORE out 2 zset1 zset2 WEIGHTS 2 3
(integer) 3
redis> ZRANGE out 0 -1 WITHSCORES
1) "one"
2) "5"
3) "three"
4) "9"
5) "two"
6) "10"
Add multiple sorted sets and store the resulting sorted set in a new key
ZUNION
ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
#Example
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZADD zset2 3 "three"
(integer) 1
redis> ZUNION 2 zset1 zset2
1) "one"
2) "three"
3) "two"
redis> ZUNION 2 zset1 zset2 WITHSCORES
1) "one"
2) "2"
3) "three"
4) "3"
5) "two"
6) "4"
Add multiple sorted sets
ZREVRANK
ZREVRANK key member
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZREVRANK myzset "one"
(integer) 2
redis> ZREVRANK myzset "four"
(nil)
Determine the index of a member in a sorted set, with scores ordered from high to low
ZREVRANGEBYSCORE
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZREVRANGEBYSCORE myzset +inf -inf
1) "three"
2) "two"
3) "one"
redis> ZREVRANGEBYSCORE myzset 2 1
1) "two"
2) "one"
redis> ZREVRANGEBYSCORE myzset 2 (1
1) "two"
redis> ZREVRANGEBYSCORE myzset (2 (1
(empty list or set)
Return a range of members in a sorted set, by score, with scores ordered from high to low
ZREVRANGE
ZREVRANGE key start stop [WITHSCORES]
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZREVRANGE myzset 0 -1
1) "three"
2) "two"
3) "one"
redis> ZREVRANGE myzset 2 3
1) "one"
redis> ZREVRANGE myzset -2 -1
1) "two"
2) "one"
Return a range of members in a sorted set, by index, with scores ordered from high to low
ZREMRANGEBYSCORE
ZREMRANGEBYSCORE key min max
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZREMRANGEBYSCORE myzset -inf (2
(integer) 1
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "two"
2) "2"
3) "three"
4) "3"
Remove all members in a sorted set within the given scores
ZREMRANGEBYRANK
ZREMRANGEBYRANK key start stop
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZREMRANGEBYRANK myzset 0 1
(integer) 2
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "three"
2) "3"
Remove all members in a sorted set within the given indexes
ZREMRANGEBYLEX
ZREMRANGEBYLEX key min max
#Example
redis> ZADD myzset 0 aaaa 0 b 0 c 0 d 0 e
(integer) 5
redis> ZADD myzset 0 foo 0 zap 0 zip 0 ALPHA 0 alpha
(integer) 5
redis> ZRANGE myzset 0 -1
1) "ALPHA"
2) "aaaa"
3) "alpha"
4) "b"
5) "c"
6) "d"
7) "e"
8) "foo"
9) "zap"
10) "zip"
redis> ZREMRANGEBYLEX myzset [alpha [omega
(integer) 6
redis> ZRANGE myzset 0 -1
1) "ALPHA"
2) "aaaa"
3) "zap"
4) "zip"
Remove all members in a sorted set between the given lexicographical range
ZREM
ZREM key member [member ...]
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZREM myzset "two"
(integer) 1
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "three"
4) "3"
Remove one or more members from a sorted set
ZRANGEBYSCORE
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZRANGEBYSCORE myzset -inf +inf
1) "one"
2) "two"
3) "three"
redis> ZRANGEBYSCORE myzset 1 2
1) "one"
2) "two"
redis> ZRANGEBYSCORE myzset (1 2
1) "two"
redis> ZRANGEBYSCORE myzset (1 (2
(empty list or set)
Return a range of members in a sorted set, by score
ZREVRANGEBYLEX
ZREVRANGEBYLEX key max min [LIMIT offset count]
#Example
redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g
(integer) 7
redis> ZREVRANGEBYLEX myzset [c -
1) "c"
2) "b"
3) "a"
redis> ZREVRANGEBYLEX myzset (c -
1) "b"
2) "a"
redis> ZREVRANGEBYLEX myzset (g [aaa
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
ZRANGEBYLEX
ZRANGEBYLEX key min max [LIMIT offset count]
#Example
redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g
(integer) 7
redis> ZRANGEBYLEX myzset - [c
1) "a"
2) "b"
3) "c"
redis> ZRANGEBYLEX myzset - (c
1) "a"
2) "b"
redis> ZRANGEBYLEX myzset [aaa (g
1) "b"
2) "c"
3) "d"
4) "e"
5) "f"
Return a range of members in a sorted set, by lexicographical range
ZRANGE
ZRANGE key start stop [WITHSCORES]
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZRANGE myzset 0 -1
1) "one"
2) "two"
3) "three"
redis> ZRANGE myzset 2 3
1) "three"
redis> ZRANGE myzset -2 -1
1) "two"
2) "three"
Return a range of members in a sorted set, by index
ZINTERSTORE
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
#Example
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZADD zset2 3 "three"
(integer) 1
redis> ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3
(integer) 2
redis> ZRANGE out 0 -1 WITHSCORES
1) "one"
2) "5"
3) "two"
4) "10"
Intersect multiple sorted sets and store the resulting sorted set in a new key
ZINTER
ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
#Example
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZADD zset2 3 "three"
(integer) 1
redis> ZINTER 2 zset1 zset2
1) "one"
2) "two"
redis> ZINTER 2 zset1 zset2 WITHSCORES
1) "one"
2) "2"
3) "two"
4) "4"
Intersect multiple sorted sets
ZDIFFSTORE
ZDIFFSTORE destination numkeys key [key ...]
#Example
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset1 3 "three"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZDIFFSTORE out 2 zset1 zset2
(integer) 1
redis> ZRANGE out 0 -1 WITHSCORES
1) "three"
2) "3"
Subtract multiple sorted sets and store the resulting sorted set in a new key
ZDIFF
ZDIFF numkeys key [key ...] [WITHSCORES]
#Example
redis> ZADD zset1 1 "one"
(integer) 1
redis> ZADD zset1 2 "two"
(integer) 1
redis> ZADD zset1 3 "three"
(integer) 1
redis> ZADD zset2 1 "one"
(integer) 1
redis> ZADD zset2 2 "two"
(integer) 1
redis> ZDIFF 2 zset1 zset2
1) "three"
redis> ZDIFF 2 zset1 zset2 WITHSCORES
1) "three"
2) "3"
Subtract multiple sorted sets
ZCOUNT
ZCOUNT key min max
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 2 "two"
(integer) 1
redis> ZADD myzset 3 "three"
(integer) 1
redis> ZCOUNT myzset -inf +inf
(integer) 3
redis> ZCOUNT myzset (1 3
(integer) 2
Count the members in a sorted set with scores within the given values
ZADD
ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]
#Example
redis> ZADD myzset 1 "one"
(integer) 1
redis> ZADD myzset 1 "uno"
(integer) 1
redis> ZADD myzset 2 "two" 3 "three"
(integer) 2
redis> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "uno"
4) "1"
5) "two"
6) "2"
7) "three"
8) "3"
Add one or more members to a sorted set, or update its score if it already exists
Redis Hash command
HINCRBYFLOAT
HINCRBYFLOAT key field increment
#Example
redis> HSET mykey field 10.50
(integer) 1
redis> HINCRBYFLOAT mykey field 0.1
"10.6"
redis> HINCRBYFLOAT mykey field -5
"5.6"
redis> HSET mykey field 5.0e3
(integer) 0
redis> HINCRBYFLOAT mykey field 2.0e2
"5200"
Increment the float value of a hash field by the given amount