This article is part of our Academy Course titled Redis a NoSQL key-value store.
This is a crash course on Redis. You will learn how to install Redis and start up the server. Additionally, you will mess around with the Redis command line. More advanced topics follow, such as replication, sharding and clustering, while the integration of Redis with Spring Data is also explained. Check it out here!
1. Introduction
Commands are the foundational concept of Redis. It’s the only way clients communicate with the server. The set of Redis commands is so rich, that there are several categories of them. Each category corresponds to data structures (or a feature) it operates on:
- Keys
- Strings
- Hashes
- Lists
- Sets
- Sorted Sets
- Publish / Subscribe
- Transactions, Scripting
Additionally, there are a couple of service commands to manage server configuration, connections and inspect the runtime behavior
In this tutorial we will continue to familiarize ourselves with Redis command line tool, redis-cli
(redis-cli.exe
on Windows), which we have briefly discussed in the first tutorial. In the next sections we will go over all Redis commands and try out most of them. Quick note ahead, to see the effects of some commands the two instances of redis-cli
(redis-cli.exe
on Windows) should be running.
2. Keys
Despite its rich features, Redis is still a key / value store. Every value stored in Redis could be retrieved using the respective key (or sometimes keys). This subsection gives the overview of generic operations over keys, independently of their values.
Command | DEL key [key …] |
Description | Deletes a key (or multiple keys). Command returns how many keys have been removed successfully. |
Example | Assuming the key mykey exists, the command deletes it. |
Reference | http://redis.io/commands/del |
Table 1
Command | DUMP key |
Description | Returns a serialized version of the value stored at the specified key. |
Example | Assuming the key mykey exists and is assigned the simple string “value”, the command returns its serialized representation. |
Reference | http://redis.io/commands/dump |
Table 2
Command | EXISTS key |
Description | Determines if a key exists. Returns 1 if key exists, 0 otherwise. |
Example | Assuming the key mykey1 exists and mykey2 does not, the command returns 1 and 0 respectively. |
Reference | http://redis.io/commands/exists |
Table 3
Command | EXPIRE key seconds |
Description | Sets a key’s time to live in seconds. Once this time interval expires, key will be deleted. Command returns 1 if key exists and 0 otherwise. |
Example | Assuming the key mykey exists, its expiration time is set to 1 second. |
Reference | http://redis.io/commands/expire |
Table 4
Command | EXPIREAT key timestamp |
Description | Sets the expiration for a key as a UNIX timestamp. Once expiration time is reached, key will be deleted. Command returns 1 if key exists and 0 otherwise. |
Example | See EXPIRE command |
Reference | http://redis.io/commands/expireat |
Table 5
Command | KEYS pattern |
Description | Finds all keys matching the given pattern. This command is very dangerous to execute on a live server because it can match the whole set of keys. |
Example | Assuming the keys mykey1 and mykey2 exist, the command returns them as match to mykey * pattern. |
Reference | http://redis.io/commands/keys |
Table 6
Command | MIGRATE host port key destination-db timeout |
Description | Atomically transfers a key from a Redis instance to another one. |
Example | Assuming the key mykey exists and there is another instance of Redis server running on localhost and port 6378 , the mykey will transferred to database 0 with 1000 milliseconds (1 second) timeout. Please notice the mykey will be deleted from current Redis instance. If mykey doesn’t exist, command returns NOKEY response. |
Reference | http://redis.io/commands/migrate |
Table 7
Command | MOVE key db |
Description | Moves a key to another database. Command returns 1 if key exists and was moved properly and 0 otherwise. |
Example | Assuming the key mykey exists, it will be moved to database 2. Please notice the mykey will be deleted from current database. |
Reference | http://redis.io/commands/move |
Table 8
Command | OBJECT REFCOUNT key
OBJECT ENCODING key
OBJECT IDLETIME key |
Description | Inspects the internals of Redis objects associated with keys:REFCOUNT returns the number of references of the value associated with the specified keyENCODING returns the kind of internal representation used in order to store the value associated with a keyIDLETIME returns the number of seconds since the object stored at the specified key is idle (not requested by read or write operations), in seconds.
|
Example | Assuming the key mykey exists, the command provides some intrinsic details, useful mostly for debugging purposes. See also DEBUG OBJECT command. |
Reference | http://redis.io/commands/object |
Table 9
Command | PERSIST key |
Description | Removes the expiration from a key. Command returns 1 if key exists and has expiration (time to live) set and 0 otherwise. |
Example | Assuming the key mykey exists and has expiration (time to live) set (see please EXPIRE command), the command removes expiration from it. |
Reference | http://redis.io/commands/persist |
Table 10
Command | PEXPIRE key milliseconds |
Description | Sets a key’s time to live in milliseconds. Once this time interval expires, key will be deleted. Command returns 1 if key exists and 0 otherwise. |
Example | See EXPIRE command |
Reference | http://redis.io/commands/pexpire |
Table 11
Command | PEXPIREAT key milliseconds-timestamp |
Description | Sets the expiration for a key as a UNIX timestamp specified in milliseconds. Once expiration time is reached, key will be deleted. Command returns 1 if key exists and 0 otherwise. |
Example | See EXPIREAT command |
Reference | http://redis.io/commands/pexpireat |
Table 12
Command | PTTL key |
Description | Gets the time to live for a key in milliseconds. Command returns -2 if key does not exist, -1 if key exists but doesn’t have expiration (time to live) set. |
Example | Assuming the key mykey exists and has expiration (time to live) set to 300 seconds (see please EXPIRE command), the command returns the remaining time to live. |
Reference | http://redis.io/commands/pttl |
Table 13
Command | RANDOMKEY |
Description | Returns a random key from the keyspace. Command returns nothing if there are not keys in keyspace. |
Example | Assuming the keys mykey , mykey1 and mykey2 exist, command returns randomly one of them. |
Reference | http://redis.io/commands/randomkey |
Table 14
Command | RENAME key newkey |
Description | Renames a key. If key key does not exist, command returns ERR no such key . Please notice, if the key newkey exists, its value will be replace with value of key key . |
Example | Assuming the key mykey exists, it will be renamed to mykey3 . |
Reference | http://redis.io/commands/rename |
Table 15
Command | RENAMENX key newkey |
Description | Renames a key, only if the new key does not exist. If key key does not exist, command returns ERR no such key . Command returns 1 if key was renamed and 0 otherwise (in case key newkey exists). |
Example | Assuming the key mykey exists and mykey4 does not exist, it will be renamed to mykey4 . |
Reference | http://redis.io/commands/renamenx |
Table 16
Command | RESTORE key ttl serialized-value |
Description | Creates a key using the provided serialized value, previously obtained using DUMP . If key key exists, command returns ERR Target key name is busy . |
Example | Reusing the serialized value from DUMP command we have seen earlier, the command assigns to key mykey2 value “value”. |
Reference | http://redis.io/commands/restore |
Table 17
Command | TTL key |
Description | Gets the time to live for a key. Command returns -2 if key does not exist, -1 if key exists but doesn’t have expiration (time to live) set. |
Example | See PTTL command. |
Reference | http://redis.io/commands/ttl |
Table 18
Command | TYPE key |
Description | Determines the type stored at key. The command returns none if key doesn’t exist. |
Example | Assuming the key mykey exists and has value “value” assigned, the command returns string as a key type. |
Reference | http://redis.io/commands/type |
Table 19
Command | SCAN cursor [MATCH pattern] [COUNT count] |
Description | Incrementally iterates the keys space. Iteration starts when the cursor is set to 0, and terminates when the cursor returned by the server is 0. |
Example | Assuming the keys mykey , mykey1 , mykey2 and mykey3 exist, command returns a cursor over keyspace. |
Reference | http://redis.io/commands/scan |
Table 20
3. Strings
Strings are the simplest type which could be store as a value of particular key. This subsection goes over Redis command specific to string data type.
Command | APPEND key value |
Description | Appends a value to a key. If key doesn’t exist, it will be created and assigned this value. Command returns the new length of key’s string value. |
Example | Assuming the key mykey exists and has value “value”, yet another string “value” will be appended to it resulting into final value “valuevalue” (with length of 10). |
Reference | http://redis.io/commands/append |
Table 21
Command | BITCOUNT key [start] [end] |
Description | Counts set bits in a string. If key doesn’t exists, command returns 0. |
Example | Assuming the key mykey exists and has value “value”, the command returns 21 as the result. |
Reference | http://redis.io/commands/bitcount |
Table 22
Command | BITOP AND destkey key [key …]
BITOP OR destkey key [key …]
BITOP XOR destkey key [key …]
BITOP NOT destkey key [key …] |
Description | Performs bitwise operations between strings. Command returns the size of the string stored in the destination key that is equal to the size of the longest input string. Command returns 0 if key key doesn’t exist. |
Example | Assuming the keys mykey and mykey1 exist with values “value” and “eulav” respectively, command returns the length of resulting string (5). |
Reference | http://redis.io/commands/bitop |
Table 23
Command | DECR key |
Description | Decrements the integer value of a key by one. If key’s value is not an integer, command returns ERR value is not an integer or out of range . If key doesn’t exist, command assumes it has value 0 and decrements it. |
Example | Assuming mykey exists and has value 100, the command decrements its value by 1 and returns new value 99. |
Reference | http://redis.io/commands/decr |
Table 24
Command | DECRBY key decrement |
Description | Decrements the integer value of a key by the given number. If key’s value is not an integer, command returns ERR value is not an integer or out of range . If key doesn’t exist, command assumes it has value 0 and decrements it by decrement. |
Example | See DECR command |
Reference | http://redis.io/commands/decrby |
Table 25
Command | GET key |
Description | Gets the value of a key. Command returns (nil) if key doesn’t exist. |
Example | Assuming mykey exists and has value “value”, the command returns it. |
Reference | http://redis.io/commands/get |
Table 26
Command | GETBIT key offset |
Description | Returns the bit value at offset in the string value stored at key. Command returns 0 if key key doesn’t exist. |
Example | Assuming mykey exists and has value “value”, the command returns 1 for offset of 3. |
Reference | http://redis.io/commands/getbit |
Table 27
Command | GETRANGE key start end |
Description | Gets a substring of the string stored at a key. Command returns “” (empty string) if key key doesn’t exist. Please notice that indexing starts from 0. |
Example | Assuming mykey exists and has value “value”, the command returns substring “lue” for range (2,5]. |
Reference | http://redis.io/commands/getrange |
Table 28
Command | GETSET key value |
Description | Sets the string value of a key and returns its old value. If key key doesn’t exist, the command returns (nil) as its old value (but assigns the new value anyway). |
Example | Assuming mykey exists and has value “value”, the command returns old value while assigning to the key a new value “newvalue”. |
Reference | http://redis.io/commands/getset |
Table 29
Command | INCR key |
Description | Increments the integer value of a key by one. If key’s value is not an integer, command returns ERR value is not an integer or out of range . If key doesn’t exist, command assumes it has value 0 and increments it. |
Example | See DECR command |
Reference | http://redis.io/commands/incr |
Table 30
Command | INCRBY key increment |
Description | Increments the integer value of a key by the given amount. If key’s value is not an integer, command returns ERR value is not an integer or out of range . If key doesn’t exist, command assumes it has value 0 and increments it by increment. |
Example | See DECR command |
Reference | http://redis.io/commands/incrby |
Table 31
Command | INCRBYFLOAT key increment |
Description | Increments the float value of a key by the given amount. If key’s value is not a float or integer, command returns ERR value is not a valid float . If key doesn’t exist, command assumes it has value 0 and increments it by increment. |
Example | See DECR command |
Reference | http://redis.io/commands/incrbyfloat |
Table 32
Command | MGET key [key …] |
Description | Gets the values of all the given keys. If one of the keys doesn’t exist, command returns (nil) as its value. Very handy command to get value of multiple keys at once. |
Example | Assuming the keys mykey1 and mykey2 exist, the command returns their values. |
Reference | http://redis.io/commands/mget |
Table 33
Command | MSET key value [key value …] |
Description | Sets multiple keys to multiple values. Very handy command to set many keys at once. If any of the keys exists, its value will be overridden. |
Example | |
Reference | http://redis.io/commands/mset |
Table 34
Command | MSETNX key value [key value …] |
Description | Sets multiple keys to multiple values, only if none of the keys exist. |
Example | See MSET command |
Reference | http://redis.io/commands/msetnx |
Table 35
Command | PSETEX key milliseconds value |
Description | Sets the value and expiration in milliseconds of a key. Very powerful combination of SET and PEXPIRE as one atomic command. |
Example | See SET and PEXPIRE commands |
Reference | http://redis.io/commands/psetex |
Table 36
Command | SET key value [EX seconds] [PX milliseconds] [NX|XX] |
Description | Sets the string value of a key. Additional options allow to control command behavior:EX seconds : sets the specified expire time, in secondsPX milliseconds : set the specified expire time, in millisecondsNX : only set the key if it does not already existXX : only set the key if it already exist
|
Example | Assuming mykey doesn’t exist, the command sets its value to “value”. |
Reference | http://redis.io/commands/setbit |
Table 37
Command | SETBIT key offset value |
Description | Sets or clears the bit at offset in the string value stored at key. If value is not 0 or 1, command returns an error ERR bit is not an integer or out of range . |
Example | Assuming mykey exists and has value “value”, the command sets the bit at offset 2 to 1. |
Reference | http://redis.io/commands/setbit |
Table 38
Command | SETEX key seconds value |
Description | Set the value and expiration of a key. The variation of SET key value EX seconds command. |
Example | See SET command |
Reference | http://redis.io/commands/setex |
Table 39
Command | SETNX key value |
Description | Sets the value of a key, only if the key does not exist. The variation of SET key value NX command. |
Example | See SET command |
Reference | http://redis.io/commands/setnx |
Table 40
Command | SETRANGE key offset value |
Description | Overwrites part of a string at key starting at the specified offset. If key doesn’t exist, its value will be filled with 0 up to the offset. |
Example | Assuming mykey exists and has value “value”, the command replaces last three characters with “eul” resulting into final value “vaeul”. |
Reference | http://redis.io/commands/setrange |
Table 41
Command | STRLEN key |
Description | Gets the length of the value stored in a key. If key doesn’t exist, command returns 0. |
Example | Assuming mykey exists and has value “value”, the command returns its value length (5). |
Reference | http://redis.io/commands/strlen |
Table 42
4. Hashes
We may think about hashes as an analogy to the traditional object notation: it’s a container of fields (or properties) and their values. All commands which expect value of the key to be hash start with ‘H‘ (HGET
, HSET
, …). If key doesn’t hold a hash value, all Hxxx
commands return an error WRONGTYPE Operation against a key holding the wrong kind of value
.
Command | HDEL key field [field …] |
Description | Deletes one or more hash fields. Command returns how many fields have been deleted. |
Example | Assuming the key mykey exists and has field field1 , the command deletes this field from the key. |
Reference | http://redis.io/commands/hdel |
Table 43
Command | HEXISTS key field |
Description | Determines if a hash field exists. Command returns 1 if field exists and 0 otherwise. |
Example | Assuming the key mykey exists and has field field1 , the command confirms that by returning 1. |
Reference | http://redis.io/commands/hexists |
Table 44
Command | HGET key field |
Description | Gets the value of a hash field. Command returns (nil) if field doesn’t exist. |
Example | Assuming the key mykey exists and has field field1 with value “value”, the command returns string “value”. |
Reference | |
Table 45
Command | HGETALL key |
Description | Gets all the fields and values in a hash. Command returns every field name, followed by its value. |
Example | Assuming the key mykey exists and has the fields field1 , field2 , the command returns their names and values. |
Reference | http://redis.io/commands/hgetall |
Table 46
Command | HINCRBY key field increment |
Description | Increments the integer value of a hash field by the given number. |
Example | See DECR command |
Reference | http://redis.io/commands/hincrby |
Table 47
Command | HKEYS key |
Description | Gets all the fields in a hash. It’s very similar to HGETALL except this command returns only field names (without values). |
Example | See HGETALL command |
Reference | http://redis.io/commands/hkeys |
Table 49
Command | HLEN key |
Description | Gets the number of fields in a hash. |
Example | Assuming the key mykey exists and has the fields field1 , field2 , the command returns total number of fields (2). |
Reference | http://redis.io/commands/hlen |
Table 50
Command | HMGET key field [field …] |
Description | Gets the values of all the given hash fields. |
Example | Assuming the key mykey exists and has the fields field1 , field2 , field2 , the command returns only values of fields field1 , field2 . |
Reference | http://redis.io/commands/hmget |
Table 51
Command | HMSET key field value [field value …] |
Description | Sets multiple hash fields to multiple values. If some fields already exist for the given key, they will be overridden. |
Example | Assuming the key mykey doesn’t exist, the command sets its fields field1 to “value1” and field2 to “value2”. |
Reference | http://redis.io/commands/hset |
Table 52
Command | HSETNX key field value |
Description | Sets the value of a hash field, only if the field does not exist. |
Example | See HSET command |
Reference | http://redis.io/commands/hset |
Table 54
Command | HVALS key |
Description | Gets all the values in a hash. |
Example | Assuming the key mykey exists and has fields field1 , field2 with values “value1”, “value2”, the command returns only fields’ values. |
Reference | http://redis.io/commands/hvals |
Table 55
Command | HSCAN key cursor [MATCH pattern] [COUNT count] |
Description | Incrementally iterates hash fields and associated values. |
Example | See SCAN command |
Reference | http://redis.io/commands/hscan |
Table 56
5. Lists
Lists are the most widely used data type to represent the general-purpose collections. It’s very easy to understand and manipulate. Some list operations have blocking and non-blocking forms. If a given key doesn’t hold a list value, all list-related commands return an error WRONGTYPE Operation against a key holding the wrong kind of value
.
Command | BLPOP key [key …] timeout |
Description | Removes and get the first element in a list, or block until one is available. Please notice that timeout should be specified in seconds. |
Example | Assuming the key mykey exists and holds a list of values “value2”, “value1” the command returns the first element from the list. |
Reference | http://redis.io/commands/blpop |
Table 57
Command | BRPOP key [key …] timeout |
Description | Removes and gets the last element in the list or block until one is available. Please notice that timeout should be specified in seconds. |
Example | See BLPOP command |
Reference | http://redis.io/commands/brpop |
Table 58
Command | BRPOPLPUSH source destination timeout |
Description | Pops a value from a list, pushes it to another list and return it; or blocks until one is available. Please notice that timeout should be specified in seconds. |
Example | Assuming the key mykey exists and holds a list of values “value2”, “value1” the command pops “value1” from it and pushes it to the list behind mykey1 key. |
Reference | http://redis.io/commands/brpoplpush |
Table 59
Command | LINDEX key index |
Description | Gets an element from a list by its index. Please notice that index starts from 0. If index goes over the length of the list, command returns (nil) as a value. |
Example | Assuming the key mykey exists and holds a list of values “value2”, “value1” the command returns the “value2” as a first element of the list. |
Reference | http://redis.io/commands/lindex |
Table 60
Command | LINSERT key BEFORE|AFTER pivot value |
Description | Inserts an element before or after another element in a list. The command returns a total number of elements in the list after inserting the values. |
Example | Assuming the key mykey exists and holds a list of values “value2”, “value1” the command inserts a value “value3” after “value2”. |
Reference | http://redis.io/commands/linsert |
Table 61
Command | LLEN key |
Description | Gets the length of a list. |
Example | Assuming the key mykey exists and holds a list of values “value2”, “value1” the command returns total number of elements in the list (2). |
Reference | http://redis.io/commands/llen |
Table 62
Command | LPOP key |
Description | Removes and gets the first element in a list. Please notice if the list is empty, command returns (nil) as a value. |
Example | Assuming the key mykey exists and holds a list of values “value2”, “value1” the command pops the first element “value2” from the list. |
Reference | http://redis.io/commands/lpop |
Table 63
Command | LPUSH key value [value …] |
Description | Prepends one or multiple values to a list. The command returns the total number of elements in the list after prepending the values. If the given key doesn’t exist, it will be created. |
Example | Assuming the key mykey doesn’t exist the command creates it as list holder and puts “value1”, “value2” to it. Because it’s prepending operation, the actual order of the elements in the list will be “value2”, “value1”. |
Reference | http://redis.io/commands/lpush |
Table 64
Command | LRANGE key start stop |
Description | Gets a range of elements from a list. |
Example | Assuming the key mykey exists and holds a list of values “value3”, “value2”, “value1” the command returns elements “value3”, “value2” as a sub-list. |
Reference | http://redis.io/commands/lrange |
Table 66
Command | LREM key count value |
Description | Removes elements from a list. The command returns the total number of elements removed. The count argument influences the operation in the following ways:- count > 0: removes elements equal to value moving from head to tail
- count < 0: removes elements equal to value moving from tail to head
- count = 0: removes all elements equal to value
|
Example | Assuming the key mykey exists and holds a list of values “value3”, “value2”, “value1” the command removes element “value2” from the list. |
Reference | http://redis.io/commands/lrem |
Table 67
Command | LSET key index value |
Description | Sets the value of an element in a list by its index. |
Example | Assuming the key mykey exists and holds a list of values “value2”, “value1” the command sets the element at index 0 (first element) to “value3” (effectively, replaces “value2” with “value3”). |
Reference | http://redis.io/commands/lset |
Table 68
Command | LTRIM key start stop |
Description | Trims a list to the specified range. |
Example | Assuming the key mykey exists and holds a list of values “value3”, “value2”, “value1” the command trims the list to 2 elements (to hold only values “value3”, “value2”). |
Reference | http://redis.io/commands/ltrim |
Table 69
Command | RPOPLPUSH source destination |
Description | Removes the last elements in a list, append it to another list and return it. |
Example | See BRPOPLPUSH command |
Reference | http://redis.io/commands/rpoplpush |
Table 71
Command | RPUSH key value [value …] |
Description | Appends one or multiple values to a list. The command returns total elements in the list after appending the values. |
Example | See LPUSH command |
Reference | http://redis.io/commands/rpush |
Table 72
Command | SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern …]] [ASC|DESC] [ALPHA] [STORE destination] |
Description | Sorts the elements in a list, set or sorted set. |
Example | This is an extremely powerful command with rich set of options. In its simplest use, assuming the key mykey exists and holds a list of values “value3”, “value2”, “value1” the command sorts elements alphabetically and returns sorted list (“value1”, “value2”, “value3”). |
Reference | http://redis.io/commands/sort |
Table 74
6. Sets
Sets are a data structure very similar to lists except sets don’t allow duplicate elements. As such, couple of specific operations has more sense over sets: unions, intersections, and substractions. All commands which expect value of the key to be a set start with ‘S‘ (SADD
, SDIFF
, …). If a given key doesn’t hold a set value, all set-related commands return an error WRONGTYPE Operation against a key holding the wrong kind of value
.
Command | SADD key member [member …] |
Description | Adds one or more members to a set. The command returns number of elements added to a set. |
Example | Assuming the key mykey doesn’t exist, the command creates it as a set holder and puts member 1 to it. |
Reference | http://redis.io/commands/sadd |
Table 75
Command | SCARD key |
Description | Gets the number of members in a set. |
Example | Assuming the key mykey exists and holds a set of members 1, 2, 3 the command returns total number of members in the set (3) |
Reference | http://redis.io/commands/scard |
Table 76
Command | SDIFF key [key …] |
Description | Subtracts multiple sets. The command returns the members of the set resulting from the difference between the first set and all the successive sets. |
Example | Assuming the key mykey exists and holds a set of members 1, 2, 3 and mykey1 exists and holds a set of members 2, 3, 4 the command returns 1 (the only member which is not present in mykey1 ). |
Reference | http://redis.io/commands/sdiff |
Table 77
Command | SDIFFSTORE destination key [key …] |
Description | Subtracts multiple sets and store the resulting set in a key. |
Example | See SDIFF command |
Reference | http://redis.io/commands/sdiffstore |
Table 78
Command | SINTER key [key …] |
Description | Intersects multiple sets. |
Example | Assuming the key mykey exists and holds a set of members 1, 2, 3 and mykey1 exists and holds a set of members 2, 3, 4 the command returns 2, 3 (members present in mykey and mykey1 ). |
Reference | http://redis.io/commands/sinter |
Table 79
Command | SINTERSTORE destination key [key …] |
Description | Intersects multiple sets and stores the resulting set in a key. |
Example | See SINTER command |
Reference | http://redis.io/commands/sinterstore |
Table 80
Command | SISMEMBER key member |
Description | Determines if a given value is a member of a set. |
Example | Assuming the key mykey exists and holds a set of members 1, 2, 3, the command returns 1 (exists) for member 2 and 0 (doesn’t exist) for member 5. |
Reference | http://redis.io/commands/sismember |
Table 81
Command | SMEMBERS key |
Description | Gets all the members in a set. |
Example | Assuming the key mykey exists and holds a set of members 1, 2, 3, the command returns those members. |
Reference | http://redis.io/commands/smembers |
Table 82
Command | SMOVE source destination member |
Description | Moves a member from one set to another. The command returns 1 if member has been moved and 0 otherwise. |
Example | Assuming the key mykey exists and holds a set of members 1, 2, 3, the command moves 1 to another set, backed by the key mykey1 . |
Reference | http://redis.io/commands/smove |
Table 83
Command | SRANDMEMBER key [count] |
Description | Gets one or multiple random members from a set. |
Example | Assuming the key mykey exists and holds a set of members 1, 2, 3, the command returns randomly members 1, 3. |
Reference | http://redis.io/commands/srandmember |
Table 85
Command | SREM key member [member …] |
Description | Removes one or more members from a set. |
Example | See LREM command |
Reference | http://redis.io/commands/srem |
Table 86
Command | SUNION key [key …] |
Description | Adds multiple sets. |
Example | Assuming the key mykey exists and holds a set of members 1, 2, 3 and mykey1 exists and holds a set of members 2, 3, 4 the command returns 1, 2, 3, 4 (all members from mykey and mykey1 ). |
Reference | http://redis.io/commands/sunion |
Table 87
Command | SUNIONSTORE destination key [key …] |
Description | Adds multiple sets and stores the resulting set in a key. |
Example | See SUNION command |
Reference | http://redis.io/commands/sunionstore |
Table 88
Command | SSCAN key cursor [MATCH pattern] [COUNT count] |
Description | Incrementally iterates set elements. |
Example | See SCAN command |
Reference | http://redis.io/commands/sscan |
Table 89
7. Sorted Sets
The difference between sets and sorted sets is that sorted sets store not only members but their scores as well (and score defines sorting order for members in the sorted set). All commands which expect value of the key to be a sorted set start with ‘Z‘ (ZADD
, ZCARD
, …). If a given key doesn’t hold a sorted set value, all sorted sets-related commands return an error WRONGTYPE Operation against a key holding the wrong kind of value
.
Command | ZADD key score member [score member …] |
Description | Adds one or more members to a sorted set, or updates its score if it already exists. The command returns number of elements added to a set. |
Example | Assuming the key mykey doesn’t exist, the command creates it as a sorted set holder and puts member value1 with score 1 to it. |
Reference | http://redis.io/commands/zadd |
Table 90
Command | ZCOUNT key min max |
Description | Counts the members in a sorted set with scores within the given values. |
Example | Assuming the key mykey exists and holds a set of members “value1”, “value2”, “value3” with scores 1, 5, 10 respectively, the command returns number of members (1) with a score between 4 and 6. |
Reference | http://redis.io/commands/zcount |
Table 92
Command | ZINCRBY key increment member |
Description | Increments the score of a member in a sorted set. The command returns new score of the member. |
Example | Assuming the key mykey exists and holds a set of members “value1”, “value2”, “value3” with scores
1, 5, 10 respectively, the command increments score of member “value2” by 5 and returns its new score (10). |
Reference | http://redis.io/commands/zincrby |
Table 93
Command | ZINTERSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX] |
Description | Intersects multiple sorted sets and stores the resulting sorted set in a new key. |
Example | See SINTER command |
Reference | http://redis.io/commands/zinterstore |
Table 94
Command | ZRANGE key start stop [WITHSCORES] |
Description | Returns a range of members in a sorted set, by index. The index starts from 0. |
Example | Assuming the key mykey exists and holds a set of members “value1”, “value2”, “value3” with scores
1, 5, 10 respectively, the command returns first two members (with indexes 0 and 1). |
Reference | http://redis.io/commands/zrange |
Table 95
Command | ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] |
Description | Returns a range of members in a sorted set, by score. |
Example | Assuming the key mykey exists and holds a set of members “value1”, “value2”, “value3” with scores
1, 5, 10 respectively, the command returns member “value2” as the one with a score between 4 and 6. |
Reference | http://redis.io/commands/zrangebyscore |
Table 96
Command | ZRANK key member |
Description | Determines the index of a member in a sorted set. Please notice that indexing starts with 0. |
Example | Assuming the key mykey exists and holds a set of members “value1”, “value2”, “value3” with scores
1, 5, 10 respectively, the command returns 1 as an index for member “value2”. |
Reference | http://redis.io/commands/zrank |
Table 97
Command | ZREM key member [member …] |
Description | Removes one or more members from a sorted set. |
Example | See SREM command |
Reference | http://redis.io/commands/zrem |
Table 98
Command | ZREMRANGEBYRANK key start stop |
Description | Removes all members in a sorted set within the given indexes. The indexing starts from 0. The command returns number of successfully removed members. |
Example | Assuming the key mykey exists and holds a set of members “value1”, “value2”, “value3” with scores
1, 5, 10 respectively, the command returns (2) and removes first two members (with indexes 0 and 1). |
Reference | http://redis.io/commands/zremrangebyrank |
Table 99
Command | ZREMRANGEBYSCORE key min max |
Description | Removes all members in a sorted set within the given scores. The command returns number of successfully removed members. |
Example | Assuming the key mykey exists and holds a set of members “value1”, “value2”, “value3” with scores
1, 5, 10 respectively, the command returns (1) and removes member “value2” (with a score between 4 and 6). |
Reference | http://redis.io/commands/zremrangebyscore |
Table 100
Command | ZREVRANGE key start stop [WITHSCORES] |
Description | Returns a range of members in a sorted set, by index, with scores ordered from high to low. |
Example | See ZRANGE command |
Reference | http://redis.io/commands/zrevrank |
Table 101
Command | ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] |
Description | Returns a range of members in a sorted set, by score, with scores ordered from high to low. |
Example | See ZRANGEBYSCORE command |
Reference | http://redis.io/commands/zrevrangebyscore |
Table 102
Command | ZREVRANK key member |
Description | Determines the index of a member in a sorted set, with scores ordered from high to low. |
Example | See ZRANK command |
Reference | http://redis.io/commands/zrevrank |
Table 103
Command | ZSCORE key member |
Description | Gets the score associated with the given member in a sorted set. |
Example | Assuming the key mykey exists and holds a set of members “value1”, “value2”, “value3” with scores
1, 5, 10 respectively, the command returns (5) as a score for a member “value2”. |
Reference | http://redis.io/commands/zscore |
Table 104
Command | ZUNIONSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX] |
Description | Adds multiple sorted sets and stores the resulting sorted set in a new key. |
Example | See SUNION command |
Reference | http://redis.io/commands/zunionstore |
Table 105
Command | ZSCAN key cursor [MATCH pattern] [COUNT count] |
Description | Incrementally iterates sorted sets elements and associated scores. |
Example | See SCAN command |
Reference | http://redis.io/commands/zscan |
Table 106
8. Publish / Subscribe
Redis supports out-of-box publish / subscribe messaging style using its powerful data structures as a queue. If you need very simple, fast and easy to use pub/sub messaging solution, Redis is definitely a candidate to consider. For this section we need two running instances of redis-cli
, one will be publisher
(producer) and another will be subscriber
(consumer).
Command | PSUBSCRIBE pattern [pattern …] |
Description | Listens for messages published to channels matching the given patterns. |
Example | The following example subscribes the client to listen for any messages from all channels. |
Reference | http://redis.io/commands/psubscribe |
Table 107
Command | PUBSUB subcommand [argument [argument …]] |
Description | Inspects the state of the Pub/Sub subsystem. It is an introspection command and is composed of subcommands that are documented separately. |
Example | The following example inspects current active channels. |
Reference | http://redis.io/commands/pubsub |
Table 108
Command | PUBLISH channel message |
Description | Posts a message to a channel. |
Example | The following example publishes a message to channel mychannel .
And subscriber (see PSUBSCRIBE command) receives it. |
Reference | http://redis.io/commands/publish |
Table 109
9. Transactions
Redis supports the powerful notion of transactions. It’s not exactly the same as we used to (for example, when dealing with relational databases) but still very useful feature. The way transactions work in Redis could be described by this flow:
WATCH
is used to provide a check-and-set (CAS) behavior to transactions: watched keys are monitored in order to detect changes against them. If at least one watched key is modified before the EXEC
command, the whole transaction aborts and fails.MULTI
starts the transaction- At this point the user can issue multiple commands which will be queued
EXEC
executes all commandsDISCARD
aborts the transaction
Very important to notice that Redis commands can fail during a transaction, but still Redis will execute the rest of the transaction instead of rolling back. For more comprehensive details please refer to http://redis.io/topics/transactions.
10. Scripting
Redis support server-side scripting using Lua
programming language (http://www.lua.org/). This section just goes through the set of commands related to scripts manipulation. It’s quite a large topic worth a whole tutorial.
11. Connection
This small set of command allows to manage some connection (session) specific parameters like authentication and database to work on.
12. Server
This class of commands is related to Redis server internals and manages persistence, replication, configuration and connections. Some commands will be used later on in following parts of the tutorial.
Command | CLIENT KILL ip:port |
Description | Kills the connection of a client. The IP address and port could be obtained from CLIENT LIST command. |
Reference | http://redis.io/commands/client-kill |
Table 131
Command | CLIENT LIST |
Description | Gets the list of client connections. |
Example | Example of command’s result for 2 connected clients (the output is clipped a bit). |
Reference | http://redis.io/commands/client-list |
Table 132
Command | CLIENT GETNAME |
Description | Gets the current connection name. |
Example | The command returns the name of current connection, myconn (previously set by CLIENT SETNAME command). |
Reference | http://redis.io/commands/client-getname |
Table 133
Command | CLIENT SETNAME connection-name |
Description | Sets the current connection name. |
Example | The command sets the name of current connection to myconn . |
Reference | http://redis.io/commands/client-setname |
Table 134
Command | SLAVEOF host port |
Description | Makes the server a slave of another instance, or promotes it as master. We will see this command in action later on in the tutorial. |
Reference | http://redis.io/commands/slaveof |
Table 149