Home » Java » Enterprise Java » Redis Commands – Using Redis Command Line

About Andrey Redko

Andrey Redko
Andriy is a well-grounded software developer with more then 12 years of practical experience using Java/EE, C#/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).

Redis Commands – Using Redis Command Line

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

  • Connection
  • Server

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.

CommandDEL key [key …]
DescriptionDeletes a key (or multiple keys). Command returns how many keys have been removed successfully.
ExampleAssuming the key mykey exists, the command deletes it.
Referencehttp://redis.io/commands/del

Table 1

CommandDUMP key
DescriptionReturns a serialized version of the value stored at the specified key.
ExampleAssuming the key mykey exists and is assigned the simple string “value”, the command returns its serialized representation.
Referencehttp://redis.io/commands/dump

Table 2

CommandEXISTS key
DescriptionDetermines if a key exists. Returns 1 if key exists, 0 otherwise.
ExampleAssuming the key mykey1 exists and mykey2 does not, the command returns 1 and 0 respectively.
Referencehttp://redis.io/commands/exists

Table 3

CommandEXPIRE key seconds
DescriptionSets 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.
ExampleAssuming the key mykey exists, its expiration time is set to 1 second.
Referencehttp://redis.io/commands/expire

Table 4

CommandEXPIREAT key timestamp
DescriptionSets 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.
ExampleSee EXPIRE command
Referencehttp://redis.io/commands/expireat

Table 5

CommandKEYS pattern
DescriptionFinds 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.
ExampleAssuming the keys mykey1 and mykey2 exist, the command returns them as match to mykey* pattern.
Referencehttp://redis.io/commands/keys

Table 6

CommandMIGRATE host port key destination-db timeout
DescriptionAtomically transfers a key from a Redis instance to another one.
ExampleAssuming 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.
Referencehttp://redis.io/commands/migrate

Table 7

CommandMOVE key db
DescriptionMoves a key to another database. Command returns 1 if key exists and was moved properly and 0 otherwise.
ExampleAssuming the key mykey exists, it will be moved to database 2. Please notice the mykey will be deleted from current database.
Referencehttp://redis.io/commands/move

Table 8

CommandOBJECT REFCOUNT key
OBJECT ENCODING key
OBJECT IDLETIME key
DescriptionInspects the internals of Redis objects associated with keys:

  • REFCOUNT returns the number of references of the value associated with the specified key
  • ENCODING returns the kind of internal representation used in order to store the value associated with a key
  • IDLETIME returns the number of seconds since the object stored at the specified key is idle (not requested by read or write operations), in seconds.
ExampleAssuming the key mykey exists, the command provides some intrinsic details, useful mostly for debugging purposes. See also DEBUG OBJECT command.
Referencehttp://redis.io/commands/object

Table 9

CommandPERSIST key
DescriptionRemoves the expiration from a key. Command returns 1 if key exists and has expiration (time to live) set and 0 otherwise.
ExampleAssuming the key mykey exists and has expiration (time to live) set (see please EXPIRE command), the command removes expiration from it.
Referencehttp://redis.io/commands/persist

Table 10

CommandPEXPIRE key milliseconds
DescriptionSets 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.
ExampleSee EXPIRE command
Referencehttp://redis.io/commands/pexpire

Table 11

CommandPEXPIREAT key milliseconds-timestamp
DescriptionSets 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.
ExampleSee EXPIREAT command
Referencehttp://redis.io/commands/pexpireat

Table 12

CommandPTTL key
DescriptionGets 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.
ExampleAssuming 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.
Referencehttp://redis.io/commands/pttl

Table 13

CommandRANDOMKEY
DescriptionReturns a random key from the keyspace. Command returns nothing if there are not keys in keyspace.
ExampleAssuming the keys mykey, mykey1 and mykey2 exist, command returns randomly one of them.
Referencehttp://redis.io/commands/randomkey

Table 14

CommandRENAME key newkey
DescriptionRenames 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.
ExampleAssuming the key mykey exists, it will be renamed to mykey3.
Referencehttp://redis.io/commands/rename

Table 15

CommandRENAMENX key newkey
DescriptionRenames 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).
ExampleAssuming the key mykey exists and mykey4 does not exist, it will be renamed to mykey4.
Referencehttp://redis.io/commands/renamenx

Table 16

CommandRESTORE key ttl serialized-value
DescriptionCreates a key using the provided serialized value, previously obtained using DUMP. If key key exists, command returns ERR Target key name is busy.
ExampleReusing the serialized value from DUMP command we have seen earlier, the command assigns to key mykey2 value “value”.
Referencehttp://redis.io/commands/restore

Table 17

CommandTTL key
DescriptionGets 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.
ExampleSee PTTL command.
Referencehttp://redis.io/commands/ttl

Table 18

CommandTYPE key
DescriptionDetermines the type stored at key. The command returns none if key doesn’t exist.
ExampleAssuming the key mykey exists and has value “value” assigned, the command returns string as a key type.
Referencehttp://redis.io/commands/type

Table 19

CommandSCAN cursor [MATCH pattern] [COUNT count]
DescriptionIncrementally iterates the keys space. Iteration starts when the cursor is set to 0, and terminates when the cursor returned by the server is 0.
ExampleAssuming the keys mykey, mykey1, mykey2 and mykey3 exist, command returns a cursor over keyspace.
Referencehttp://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.

CommandAPPEND key value
DescriptionAppends 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.
ExampleAssuming 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).
Referencehttp://redis.io/commands/append

Table 21

CommandBITCOUNT key [start] [end]
DescriptionCounts set bits in a string. If key doesn’t exists, command returns 0.
ExampleAssuming the key mykey exists and has value “value”, the command returns 21 as the result.
Referencehttp://redis.io/commands/bitcount

Table 22

CommandBITOP AND destkey key [key …] BITOP OR destkey key [key …] BITOP XOR destkey key [key …] BITOP NOT destkey key [key …]
DescriptionPerforms 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.
ExampleAssuming the keys mykey and mykey1 exist with values “value” and “eulav” respectively, command returns the length of resulting string (5).
Referencehttp://redis.io/commands/bitop

Table 23

CommandDECR key
DescriptionDecrements 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.
ExampleAssuming mykey exists and has value 100, the command decrements its value by 1 and returns new value 99.
Referencehttp://redis.io/commands/decr

Table 24

CommandDECRBY key decrement
DescriptionDecrements 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.
ExampleSee DECR command
Referencehttp://redis.io/commands/decrby

Table 25

CommandGET key
DescriptionGets the value of a key. Command returns (nil) if key doesn’t exist.
ExampleAssuming mykey exists and has value “value”, the command returns it.
Referencehttp://redis.io/commands/get

Table 26

CommandGETBIT key offset
DescriptionReturns the bit value at offset in the string value stored at key. Command returns 0 if key key doesn’t exist.
ExampleAssuming mykey exists and has value “value”, the command returns 1 for offset of 3.
Referencehttp://redis.io/commands/getbit

Table 27

CommandGETRANGE key start end
DescriptionGets 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.
ExampleAssuming mykey exists and has value “value”, the command returns substring “lue” for range (2,5].
Referencehttp://redis.io/commands/getrange

Table 28

CommandGETSET key value
DescriptionSets 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).
ExampleAssuming mykey exists and has value “value”, the command returns old value while assigning to the key a new value “newvalue”.
Referencehttp://redis.io/commands/getset

Table 29

CommandINCR key
DescriptionIncrements 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.
ExampleSee DECR command
Referencehttp://redis.io/commands/incr

Table 30

CommandINCRBY key increment
DescriptionIncrements 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.
ExampleSee DECR command
Referencehttp://redis.io/commands/incrby

Table 31

CommandINCRBYFLOAT key increment
DescriptionIncrements 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.
ExampleSee DECR command
Referencehttp://redis.io/commands/incrbyfloat

Table 32

CommandMGET key [key …]
DescriptionGets 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.
ExampleAssuming the keys mykey1 and mykey2 exist, the command returns their values.
Referencehttp://redis.io/commands/mget

Table 33

CommandMSET key value [key value …]
DescriptionSets 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
Referencehttp://redis.io/commands/mset

Table 34

CommandMSETNX key value [key value …]
DescriptionSets multiple keys to multiple values, only if none of the keys exist.
ExampleSee MSET command
Referencehttp://redis.io/commands/msetnx

Table 35

CommandPSETEX key milliseconds value
DescriptionSets the value and expiration in milliseconds of a key. Very powerful combination of SET and PEXPIRE as one atomic command.
ExampleSee SET and PEXPIRE commands
Referencehttp://redis.io/commands/psetex

Table 36

CommandSET key value [EX seconds] [PX milliseconds] [NX|XX]
DescriptionSets the string value of a key. Additional options allow to control command behavior:

  • EX seconds: sets the specified expire time, in seconds
  • PX milliseconds: set the specified expire time, in milliseconds
  • NX: only set the key if it does not already exist
  • XX: only set the key if it already exist
ExampleAssuming mykey doesn’t exist, the command sets its value to “value”.
Referencehttp://redis.io/commands/setbit

Table 37

CommandSETBIT key offset value
DescriptionSets 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.
ExampleAssuming mykey exists and has value “value”, the command sets the bit at offset 2 to 1.
Referencehttp://redis.io/commands/setbit

Table 38

CommandSETEX key seconds value
DescriptionSet the value and expiration of a key. The variation of SET key value EX seconds command.
ExampleSee SET command
Referencehttp://redis.io/commands/setex

Table 39

CommandSETNX key value
DescriptionSets the value of a key, only if the key does not exist. The variation of SET key value NX command.
ExampleSee SET command
Referencehttp://redis.io/commands/setnx

Table 40

CommandSETRANGE key offset value
DescriptionOverwrites 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.
ExampleAssuming mykey exists and has value “value”, the command replaces last three characters with “eul” resulting into final value “vaeul”.
Referencehttp://redis.io/commands/setrange

Table 41

CommandSTRLEN key
DescriptionGets the length of the value stored in a key. If key doesn’t exist, command returns 0.
ExampleAssuming mykey exists and has value “value”, the command returns its value length (5).
Referencehttp://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.

CommandHDEL key field [field …]
DescriptionDeletes one or more hash fields. Command returns how many fields have been deleted.
ExampleAssuming the key mykey exists and has field field1, the command deletes this field from the key.
Referencehttp://redis.io/commands/hdel

Table 43

CommandHEXISTS key field
DescriptionDetermines if a hash field exists. Command returns 1 if field exists and 0 otherwise.
ExampleAssuming the key mykey exists and has field field1, the command confirms that by returning 1.
Referencehttp://redis.io/commands/hexists

Table 44

CommandHGET key field
DescriptionGets the value of a hash field. Command returns (nil) if field doesn’t exist.
ExampleAssuming the key mykey exists and has field field1 with value “value”, the command returns string “value”.
Reference

Table 45

CommandHGETALL key
DescriptionGets all the fields and values in a hash. Command returns every field name, followed by its value.
ExampleAssuming the key mykey exists and has the fields field1, field2, the command returns their names and values.
Referencehttp://redis.io/commands/hgetall

Table 46

CommandHINCRBY key field increment
DescriptionIncrements the integer value of a hash field by the given number.
ExampleSee DECR command
Referencehttp://redis.io/commands/hincrby

Table 47

CommandHINCRBYFLOAT key field increment
DescriptionIncrements the float value of a hash field by the given amount.
ExampleSee DECR
Referencehttp://redis.io/commands/hincrbyfloat

Table 48

CommandHKEYS key
DescriptionGets all the fields in a hash. It’s very similar to HGETALL except this command returns only field names (without values).
ExampleSee HGETALL command
Referencehttp://redis.io/commands/hkeys

Table 49

CommandHLEN key
DescriptionGets the number of fields in a hash.
ExampleAssuming the key mykey exists and has the fields field1, field2, the command returns total number of fields (2).
Referencehttp://redis.io/commands/hlen

Table 50

CommandHMGET key field [field …]
DescriptionGets the values of all the given hash fields.
ExampleAssuming the key mykey exists and has the fields field1, field2, field2, the command returns only values of fields field1, field2.
Referencehttp://redis.io/commands/hmget

Table 51

CommandHMSET key field value [field value …]
DescriptionSets multiple hash fields to multiple values. If some fields already exist for the given key, they will be overridden.
ExampleAssuming the key mykey doesn’t exist, the command sets its fields field1 to “value1” and field2 to “value2”.
Referencehttp://redis.io/commands/hset

Table 52

CommandHSET key field value
DescriptionSets the string value of a hash field.
ExampleSee HMSET command
Referencehttp://redis.io/commands/hset

Table 53

CommandHSETNX key field value
DescriptionSets the value of a hash field, only if the field does not exist.
ExampleSee HSET command
Referencehttp://redis.io/commands/hset

Table 54

CommandHVALS key
DescriptionGets all the values in a hash.
ExampleAssuming the key mykey exists and has fields field1, field2 with values “value1”, “value2”, the command returns only fields’ values.
Referencehttp://redis.io/commands/hvals

Table 55

CommandHSCAN key cursor [MATCH pattern] [COUNT count]
DescriptionIncrementally iterates hash fields and associated values.
ExampleSee SCAN command
Referencehttp://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.

CommandBLPOP key [key …] timeout
DescriptionRemoves and get the first element in a list, or block until one is available. Please notice that timeout should be specified in seconds.
ExampleAssuming the key mykey exists and holds a list of values “value2”, “value1” the command returns the first element from the list.
Referencehttp://redis.io/commands/blpop

Table 57

CommandBRPOP key [key …] timeout
DescriptionRemoves and gets the last element in the list or block until one is available. Please notice that timeout should be specified in seconds.
ExampleSee BLPOP command
Referencehttp://redis.io/commands/brpop

Table 58

CommandBRPOPLPUSH source destination timeout
DescriptionPops 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.
ExampleAssuming 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.
Referencehttp://redis.io/commands/brpoplpush

Table 59

CommandLINDEX key index
DescriptionGets 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.
ExampleAssuming the key mykey exists and holds a list of values “value2”, “value1” the command returns the “value2” as a first element of the list.
Referencehttp://redis.io/commands/lindex

Table 60

CommandLINSERT key BEFORE|AFTER pivot value
DescriptionInserts 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.
ExampleAssuming the key mykey exists and holds a list of values “value2”, “value1” the command inserts a value “value3” after “value2”.
Referencehttp://redis.io/commands/linsert

Table 61

CommandLLEN key
DescriptionGets the length of a list.
ExampleAssuming the key mykey exists and holds a list of values “value2”, “value1” the command returns total number of elements in the list (2).
Referencehttp://redis.io/commands/llen

Table 62

CommandLPOP key
DescriptionRemoves and gets the first element in a list. Please notice if the list is empty, command returns (nil) as a value.
ExampleAssuming the key mykey exists and holds a list of values “value2”, “value1” the command pops the first element “value2” from the list.
Referencehttp://redis.io/commands/lpop

Table 63

CommandLPUSH key value [value …]
DescriptionPrepends 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.
ExampleAssuming 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”.
Referencehttp://redis.io/commands/lpush

Table 64

CommandLPUSHX key value
DescriptionPrepends a value to a list, only if the list exists.
ExampleSee LPUSH command
Referencehttp://redis.io/commands/lpushx

Table 65

CommandLRANGE key start stop
DescriptionGets a range of elements from a list.
ExampleAssuming the key mykey exists and holds a list of values “value3”, “value2”, “value1” the command returns elements “value3”, “value2” as a sub-list.
Referencehttp://redis.io/commands/lrange

Table 66

CommandLREM key count value
DescriptionRemoves 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
ExampleAssuming the key mykey exists and holds a list of values “value3”, “value2”, “value1” the command removes element “value2” from the list.
Referencehttp://redis.io/commands/lrem

Table 67

CommandLSET key index value
DescriptionSets the value of an element in a list by its index.
ExampleAssuming 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”).
Referencehttp://redis.io/commands/lset

Table 68

CommandLTRIM key start stop
DescriptionTrims a list to the specified range.
ExampleAssuming 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”).
Referencehttp://redis.io/commands/ltrim

Table 69

CommandRPOP key
DescriptionRemoves and gets the last element in a list.
ExampleSee LPOP command
Referencehttp://redis.io/commands/rpop

Table 70

CommandRPOPLPUSH source destination
DescriptionRemoves the last elements in a list, append it to another list and return it.
ExampleSee BRPOPLPUSH command
Referencehttp://redis.io/commands/rpoplpush

Table 71

CommandRPUSH key value [value …]
DescriptionAppends one or multiple values to a list. The command returns total elements in the list after appending the values.
ExampleSee LPUSH command
Referencehttp://redis.io/commands/rpush

Table 72

CommandRPUSHX key value
DescriptionAppends a value to a list, only if the list exists.
ExampleSee LPUSH command
Referencehttp://redis.io/commands/rpushx

Table 73

CommandSORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern …]] [ASC|DESC] [ALPHA] [STORE destination]
DescriptionSorts the elements in a list, set or sorted set.
ExampleThis 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”).
Referencehttp://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.

CommandSADD key member [member …]
DescriptionAdds one or more members to a set. The command returns number of elements added to a set.
ExampleAssuming the key mykey doesn’t exist, the command creates it as a set holder and puts member 1 to it.
Referencehttp://redis.io/commands/sadd

Table 75

CommandSCARD key
DescriptionGets the number of members in a set.
ExampleAssuming the key mykey exists and holds a set of members 1, 2, 3 the command returns total number of members in the set (3)
Referencehttp://redis.io/commands/scard

Table 76

CommandSDIFF key [key …]
DescriptionSubtracts multiple sets. The command returns the members of the set resulting from the difference between the first set and all the successive sets.
ExampleAssuming 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).
Referencehttp://redis.io/commands/sdiff

Table 77

CommandSDIFFSTORE destination key [key …]
DescriptionSubtracts multiple sets and store the resulting set in a key.
ExampleSee SDIFF command
Referencehttp://redis.io/commands/sdiffstore

Table 78

CommandSINTER key [key …]
DescriptionIntersects multiple sets.
ExampleAssuming 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).
Referencehttp://redis.io/commands/sinter

Table 79

CommandSINTERSTORE destination key [key …]
DescriptionIntersects multiple sets and stores the resulting set in a key.
ExampleSee SINTER command
Referencehttp://redis.io/commands/sinterstore

Table 80

CommandSISMEMBER key member
DescriptionDetermines if a given value is a member of a set.
ExampleAssuming 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.
Referencehttp://redis.io/commands/sismember

Table 81

CommandSMEMBERS key
DescriptionGets all the members in a set.
ExampleAssuming the key mykey exists and holds a set of members 1, 2, 3, the command returns those members.
Referencehttp://redis.io/commands/smembers

Table 82

CommandSMOVE source destination member
DescriptionMoves a member from one set to another. The command returns 1 if member has been moved and 0 otherwise.
ExampleAssuming 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.
Referencehttp://redis.io/commands/smove

Table 83

CommandSPOP key
DescriptionRemoves and returns a random member from a set.
ExampleSee LPOP command
Referencehttp://redis.io/commands/spop

Table 84

CommandSRANDMEMBER key [count]
DescriptionGets one or multiple random members from a set.
ExampleAssuming the key mykey exists and holds a set of members 1, 2, 3, the command returns randomly members 1, 3.
Referencehttp://redis.io/commands/srandmember

Table 85

CommandSREM key member [member …]
DescriptionRemoves one or more members from a set.
ExampleSee LREM command
Referencehttp://redis.io/commands/srem

Table 86

CommandSUNION key [key …]
DescriptionAdds multiple sets.
ExampleAssuming 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).
Referencehttp://redis.io/commands/sunion

Table 87

CommandSUNIONSTORE destination key [key …]
DescriptionAdds multiple sets and stores the resulting set in a key.
ExampleSee SUNION command
Referencehttp://redis.io/commands/sunionstore

Table 88

CommandSSCAN key cursor [MATCH pattern] [COUNT count]
DescriptionIncrementally iterates set elements.
ExampleSee SCAN command
Referencehttp://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.

CommandZADD key score member [score member …]
DescriptionAdds 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.
ExampleAssuming the key mykey doesn’t exist, the command creates it as a sorted set holder and puts member value1 with score 1 to it.
Referencehttp://redis.io/commands/zadd

Table 90

CommandZCARD key
DescriptionGets the number of members in a sorted set.
ExampleSee SCARDcommand
Referencehttp://redis.io/commands/zcard

Table 91

CommandZCOUNT key min max
DescriptionCounts the members in a sorted set with scores within the given values.
ExampleAssuming 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.
Referencehttp://redis.io/commands/zcount

Table 92

CommandZINCRBY key increment member
DescriptionIncrements the score of a member in a sorted set. The command returns new score of the member.
ExampleAssuming 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).
Referencehttp://redis.io/commands/zincrby

Table 93

CommandZINTERSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX]
DescriptionIntersects multiple sorted sets and stores the resulting sorted set in a new key.
ExampleSee SINTER command
Referencehttp://redis.io/commands/zinterstore

Table 94

CommandZRANGE key start stop [WITHSCORES]
DescriptionReturns a range of members in a sorted set, by index. The index starts from 0.
ExampleAssuming 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).
Referencehttp://redis.io/commands/zrange

Table 95

CommandZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
DescriptionReturns a range of members in a sorted set, by score.
ExampleAssuming 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.
Referencehttp://redis.io/commands/zrangebyscore

Table 96

CommandZRANK key member
DescriptionDetermines the index of a member in a sorted set. Please notice that indexing starts with 0.
ExampleAssuming 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”.
Referencehttp://redis.io/commands/zrank

Table 97

CommandZREM key member [member …]
DescriptionRemoves one or more members from a sorted set.
ExampleSee SREM command
Referencehttp://redis.io/commands/zrem

Table 98

CommandZREMRANGEBYRANK key start stop
DescriptionRemoves all members in a sorted set within the given indexes. The indexing starts from 0. The command returns number of successfully removed members.
ExampleAssuming 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).
Referencehttp://redis.io/commands/zremrangebyrank

Table 99

CommandZREMRANGEBYSCORE key min max
DescriptionRemoves all members in a sorted set within the given scores. The command returns number of successfully removed members.
ExampleAssuming 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).
Referencehttp://redis.io/commands/zremrangebyscore

Table 100

CommandZREVRANGE key start stop [WITHSCORES]
DescriptionReturns a range of members in a sorted set, by index, with scores ordered from high to low.
ExampleSee ZRANGE command
Referencehttp://redis.io/commands/zrevrank

Table 101

CommandZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
DescriptionReturns a range of members in a sorted set, by score, with scores ordered from high to low.
ExampleSee ZRANGEBYSCORE command
Referencehttp://redis.io/commands/zrevrangebyscore

Table 102

CommandZREVRANK key member
DescriptionDetermines the index of a member in a sorted set, with scores ordered from high to low.
ExampleSee ZRANK command
Referencehttp://redis.io/commands/zrevrank

Table 103

CommandZSCORE key member
DescriptionGets the score associated with the given member in a sorted set.
ExampleAssuming 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”.
Referencehttp://redis.io/commands/zscore

Table 104

CommandZUNIONSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX]
DescriptionAdds multiple sorted sets and stores the resulting sorted set in a new key.
ExampleSee SUNION command
Referencehttp://redis.io/commands/zunionstore

Table 105

CommandZSCAN key cursor [MATCH pattern] [COUNT count]
DescriptionIncrementally iterates sorted sets elements and associated scores.
ExampleSee SCAN command
Referencehttp://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).

CommandPSUBSCRIBE pattern [pattern …]
DescriptionListens for messages published to channels matching the given patterns.
ExampleThe following example subscribes the client to listen for any messages from all channels.
Referencehttp://redis.io/commands/psubscribe

Table 107

CommandPUBSUB subcommand [argument [argument …]]
DescriptionInspects the state of the Pub/Sub subsystem. It is an introspection command and is composed of subcommands that are documented separately.
ExampleThe following example inspects current active channels.
Referencehttp://redis.io/commands/pubsub

Table 108

CommandPUBLISH channel message
DescriptionPosts a message to a channel.
ExampleThe following example publishes a message to channel mychannel.
And subscriber (see PSUBSCRIBE command) receives it.
Referencehttp://redis.io/commands/publish

Table 109

CommandPUNSUBSCRIBE [pattern [pattern …]]
DescriptionStops listening for messages posted to channels matching the given. Patterns
Referencehttp://redis.io/commands/punsubscribe

Table 110

CommandSUBSCRIBE channel [channel …]
DescriptionListens for messages published to the given channels.
ExampleSee PSUBSCRIBE
Referencehttp://redis.io/commands/subscribe

Table 111

CommandUNSUBSCRIBE [channel [channel …]]
DescriptionStops listening for messages posted to the given channels.
Referencehttp://redis.io/commands/unsubscribe

Table 112

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 commands
  • DISCARD 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.

CommandDISCARD
DescriptionDiscards all commands issued after MULTI.
Referencehttp://redis.io/commands/discard

Table 113

CommandEXEC
DescriptionExecutes all commands issued after MULTI.
Referencehttp://redis.io/commands/exec

Table 114

CommandMULTI
DescriptionMarks the start of a transaction block.
Referencehttp://redis.io/commands/multi

Table 115

CommandUNWATCH
DescriptionForgets about all watched keys.
Referencehttp://redis.io/commands/unwatch

Table 116

CommandWATCH key [key …]
DescriptionWatches the given keys to determine execution of the MULTI/EXEC block.
Referencehttp://redis.io/commands/watch

Table 117

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.

CommandEVAL script numkeys key [key …] arg [arg …]
DescriptionExecutes a Lua script server side.
Referencehttp://redis.io/commands/eval

Table 118

CommandEVALSHA sha1 numkeys key [key …] arg [arg …]
DescriptionExecutes a Lua script server side.
Referencehttp://redis.io/commands/evalsha

Table 119

CommandSCRIPT EXISTS script [script …]
DescriptionChecks existence of scripts in the script cache.
Referencehttp://redis.io/commands/script-exists

Table 120

CommandSCRIPT FLUSH
DescriptionRemoves all the scripts from the script cache.
Referencehttp://redis.io/commands/script-flush

Table 121

CommandSCRIPT KILL
DescriptionKills the script currently in execution.
Referencehttp://redis.io/commands/script-kill

Table 122

CommandSCRIPT LOAD script
DescriptionLoads the specified Lua script into the script cache.
Referencehttp://redis.io/commands/script-load

Table 123

11. Connection

This small set of command allows to manage some connection (session) specific parameters like authentication and database to work on.

CommandAUTH password
DescriptionAuthenticates to the server.
Referencehttp://redis.io/commands/auth

Table 124

CommandECHO message
DescriptionEchoes the given string.
Referencehttp://redis.io/commands/echo

Table 125

CommandPING
DescriptionPings the server. On success, command returns PONG.
Referencehttp://redis.io/commands/ping

Table 126

CommandQUIT
DescriptionCloses the connection.
Referencehttp://redis.io/commands/quit

Table 127

CommandSELECT index
DescriptionChanges the selected database for the current connection.
Referencehttp://redis.io/commands/select

Table 128

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.

CommandBGREWRITEAOF
DescriptionAsynchronously rewrites the append-only file.
Referencehttp://redis.io/commands/bgrewriteaof

Table 129

CommandBGSAVE
DescriptionAsynchronously saves the dataset to disk.
Referencehttp://redis.io/commands/bgsave

Table 130

CommandCLIENT KILL ip:port
DescriptionKills the connection of a client. The IP address and port could be obtained from CLIENT LIST command.
Referencehttp://redis.io/commands/client-kill

Table 131

CommandCLIENT LIST
DescriptionGets the list of client connections.
ExampleExample of command’s result for 2 connected clients (the output is clipped a bit).
Referencehttp://redis.io/commands/client-list

Table 132

CommandCLIENT GETNAME
DescriptionGets the current connection name.
ExampleThe command returns the name of current connection, myconn (previously set by CLIENT SETNAME command).
Referencehttp://redis.io/commands/client-getname

Table 133

CommandCLIENT SETNAME connection-name
DescriptionSets the current connection name.
ExampleThe command sets the name of current connection to myconn.
Referencehttp://redis.io/commands/client-setname

Table 134

CommandCONFIG GET parameter
DescriptionGets the value of a configuration parameter.
Referencehttp://redis.io/commands/config-get

Table 135

CommandCONFIG REWRITE
DescriptionRewrites the configuration file with the in memory configuration.
Referencehttp://redis.io/commands/config-rewrite

Table 136

CommandCONFIG SET parameter value
DescriptionSets a configuration parameter to the given value.
Referencehttp://redis.io/commands/config-set

Table 137

CommandCONFIG RESETSTAT
DescriptionResets the stats returned by INFO.
Referencehttp://redis.io/commands/config-resetstat

Table 138

CommandDBSIZE
DescriptionReturns the number of keys in the selected database.
Referencehttp://redis.io/commands/dbsize

Table 139

CommandDEBUG OBJECT key
DescriptionGets debugging information about a key. This command includes the results of OBJECT command.
Referencehttp://redis.io/commands/debug-object

Table 140

CommandDEBUG SEGFAULT
DescriptionMakes the server crash.
Referencehttp://redis.io/commands/debug-segfault

Table 141

CommandFLUSHALL
DescriptionRemoves all keys from all databases.
Referencehttp://redis.io/commands/flushall

Table 142

CommandFLUSHDB
DescriptionRemoves all keys from the current database.
Referencehttp://redis.io/commands/flushdb

Table 143

CommandINFO [section]
DescriptionGets information and statistics about the server.
Referencehttp://redis.io/commands/info

Table 144

CommandLASTSAVE
DescriptionGets the UNIX time stamp of the last successful save to disk.
Referencehttp://redis.io/commands/lastsave

Table 145

CommandMONITOR
DescriptionListens for all requests received by the server in real time.
Referencehttp://redis.io/commands/monitor

Table 146

CommandSAVE
DescriptionSynchronously saves the dataset to disk
Referencehttp://redis.io/commands/save

Table 147

CommandSHUTDOWN [NOSAVE] [SAVE]
DescriptionSynchronously saves the dataset to disk and then shuts down the server.
Referencehttp://redis.io/commands/shutdown

Table 148

CommandSLAVEOF host port
DescriptionMakes the server a slave of another instance, or promotes it as master. We will see this command in action later on in the tutorial.
Referencehttp://redis.io/commands/slaveof

Table 149

CommandSLOWLOG subcommand [argument]
DescriptionManages the Redis slow queries log.
Referencehttp://redis.io/commands/slowlog

Table 150

CommandSYNC
DescriptionInternal command used for replication.
Referencehttp://redis.io/commands/sync

Table 151

CommandTIME
DescriptionReturn the current server time.
Referencehttp://redis.io/commands/time

Table 152

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of