In Redis: Part 1, we covered the basics of Redis, installation and data structures in Redis.
In this article we will cover the basic commands of Redis, along with examples of how to use them

1.0 String commands

 

1.1 SET

SET key value [EX seconds] [PX milliseconds] [NX|XX]

Behavior

  • Set key to hold the string value.
  • If key already holds a value, it is overwritten, regardless of its type.
  • Any previous time to live(TTL) associated with the key is discarded on successful SET operation.

Additional Options

  • EX seconds — Set 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.

Return Values

OK if SET was executed correctly.
a Null Reply is returned if the SET operation was not performed because the user specified the NX or XX option but the condition was not met.

e.g

redis:6379> set document:1000:title "03.02 Canada Audit" EX 100
OK

redis:6379> get document:1000:title
"03.02 Canada Audit"

redis:6379> set document:1000:title "03.02 Australia Audit" EX 100 NX
(nil)

redis:6379> get document:1000:title
"03.02 Canada Audit"

redis:6379>

 

1.2 GET

GET key

Get the value of key. If the key does not exist the special value nil is returned.

An error is returned if the value stored at key is not a string, because GET only handles string values.

redis:6379> set user:1000:firstName "Rohan"
OK

redis:6379> get user:1000:firstName
"Rohan"

redis:6379> get user:1000:lastName
(nil)

redis:6379>

 

1.3 INCR/ DECR/ INCRBY/ DECRBY

  • When setting a STRING value in Redis, if that value could be interpreted as a base-10 integer or a floating-point value, Redis will detect this and allow you to manipulate the value using the various INCR* and DECR*operations.
  • If you try to increment or decrement a key that doesn’t exist or is an empty string, Redis will operate as though that key’s value were zero.
  • If you try to increment or decrement a key that has a value that can’t be interpreted as an integer or float, you’ll receive an error.

 

INCR
Increments the value stored at the key by 1

INCR key-name

 

DECR
Decrements the value stored at the key by 1

DECR key-name

 

INCRBY
Increments the value stored at the key by the provided integer value.

INCRBY key-name amount

 

DECRBY
Decrements the value stored at the key by the provided integer value

DECRBY key-name amount

 

redis:6379> get connection:timeout
(nil)

redis:6379> incrby connection:timeout 60
(integer) 60

redis:6379> decrby connection:timeout 31
(integer) 29

redis:6379> incr connection:timeout
(integer) 30

redis:6379> get connection:timeout
"30"

redis:6379>

 

Why do we need an INCR command? Why can we just fetch it and increment it?
Instead of writing INCR count, we could just do the following

x = GET count
x = x + 1
SET count x

The problem is that doing the increment in this way will only work as long as there is a single client using the key.
If more than one client read the value of same key and try to increment it manually, there will be a race condition and one of the client will have the wrong result.
Calling the INCR command in Redis will prevent this from happening, because it is an atomic operation.

 

1.4 APPEND

APPEND key value

If key already exists and is a string, this command appends the value at the end of the string.

If key does not exist it is created and set as an empty string, so APPEND will be similar to SET in this special case.

Returns the length of the string after the append operation.

redis:6379> append user:1000:name "Sunit"
(integer) 5

redis:6379> get user:1000:name
"Sunit"

redis:6379> append user:1000:name "Chatterjee"
(integer) 15

redis:6379> get user:1000:name
"SunitChatterjee"

redis:6379>

 

 

 

2.0 General commands

2.1 KEYS

KEYS pattern

Returns all keys matching pattern.

While the time complexity for this operation is O(N), the constant times are fairly low. For example, Redis running on an entry level laptop can scan a 1 million key database in 40 milliseconds.

 

Warning: Don’t use KEYS in production environments, as it may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout.
Don’t use KEYS in your regular application code. If you’re looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

redis:6379> set user:1000:firstName "Rohan Bhagwat"
OK

redis:6379> set user:1001:name "Pranali Hirve"
OK

redis:6379> set user:1002 "Anushri Sathe"
OK

redis:6379> set connection:limit 50
OK

redis:6379> set email:size:limit "10 MB"
OK

redis:6379> keys user*name
1) "user:1001:name"

redis:6379> keys user*ame
1) "user:1000:firstName"
2) "user:1001:name"

redis:6379> keys *limit
1) "connection:limit"
2) "email:size:limit"

redis:6379>

 

2.2 DEL

DEL key [key ...]

Removes the specified keys. A key is ignored if it does not exist.
Returns the number of keys that were removed.

redis:6379> keys *
1) "user:1000:firstName"
2) "conn:limit"
3) "user:1000:name"
4) "connection:timeout"

redis:6379> del user:1000
(integer) 0

redis:6379> del user:1000:firstName
(integer) 1

redis:6379> del conn:limit user:1000:name connection:timeout
(integer) 3

redis:6379> keys *
(empty list or set)

redis:6379>

 

2.3 EXPIRE / EXPIREAT / TTL

EXPIRE

  • Set a timeout on key.
  • After the timeout has expired, the key will automatically be deleted.
  • The timeout will only be cleared by commands that delete or overwrite the contents of the key, including DEL, SET, GETSET and all the *STOREcommands.
  • This means that all the operations that conceptually alter the value stored at the key without replacing it with a new one will leave the timeout untouched. For instance, incrementing the value of a key with INCR, pushing a new value into a list with LPUSH, or altering the field value of a hash with HSET are all operations that will leave the timeout untouched.
  • The timeout can also be cleared, turning the key back into a persistent key, using the PERSIST command.
  • If a key is renamed with RENAME, the associated time to live is transferred to the new key name.
  • Return value : 1 if the timeout was set. 0 if key does not exist.

 

EXPIREAT

  • EXPIREAT has the same effect and semantic as EXPIRE, but instead of specifying the number of seconds representing the TTL (time to live), it takes an absolute Unix timestamp (seconds since January 1, 1970).
  • EXPIREAT was introduced in order to convert relative timeouts to absolute timeouts for the AOF persistence mode

 

Note: Note that calling EXPIRE/PEXPIRE with a non-positive timeout or EXPIREAT/PEXPIREAT with a time in the past will result in the key being deleted rather than expired

TTL/PTTL

  • Returns the remaining time to live of a key that has a timeout.
  • In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has no associated expire.
  • Starting with Redis 2.8 the return value in case of error changed:
    • The command returns -2 if the key does not exist.
    • The command returns -1 if the key exists but has no associated expire.
  •  PTTL command that returns the same information with milliseconds resolution (Only available in Redis 2.6 or greater).

 

redis:6379> set user:1000 "Rohan Bhagwat" EX 1000
OK

redis:6379> set user:1001 "Nilesh Gundecha"
OK

redis:6379> set user:1002 "Pranali Hirve"
OK

redis:6379> set user:1003 "Anushri Sathe"
OK

redis:6379> EXPIRE user:1001 200
(integer) 1

redis:6379> EXPIREAT user:1002 1523662200
(integer) 1

redis:6379> TTL user:1000
(integer) 880

redis:6379> TTL user:1001
(integer) 118

redis:6379> TTL user:1002
(integer) 51419

redis:6379> TTL user:1003
(integer) -1

redis:6379> PTTL user:100
(integer) -2

redis:6379> PTTL user:1000
(integer) 851084

redis:6379>

 

2.5 EXISTS

EXISTS key [key ...]
  • Returns if key exists.
  • Since Redis 3.0.3 it is possible to specify multiple keys instead of a single one. In such a case, it returns the total number of keys existing.
  • If the same existing key is mentioned in the arguments multiple times, it will be counted multiple times.

 

redis:6379> EXISTS user:1001:name
(integer) 1

redis:6379> EXISTS user:1001:name user:1001:name user:1001:name
(integer) 3

redis:6379> EXISTS user:1000
(integer) 0

redis:6379>

 

 

2.6 RENAME

 

  • Renames key to newkey.
  • It returns an error when key does not exist.
  • If newkey already exists it is overwritten, when this happens RENAME executes an implicit DEL operation, so if the deleted key contains a very big value it may cause high latency even if RENAME itself is usually a constant-time operation.
redis:6379> get user:1001:name
"Pranali Hirve"

redis:6379> RENAME user:1001:NAME user:1001:fullname
(error) ERR no such key

redis:6379> RENAME user:1001:name user:1001:fullname
OK

redis:6379> get user:1001:name
(nil)

redis:6379> get user:1001:fullname
"Pranali Hirve"

redis:6379>

2.7 SCAN

SCAN cursor [MATCH pattern] [COUNT count]
  • The SCAN command and the closely related commands SSCAN, HSCAN and ZSCAN are used in order to incrementally iterate over a collection of elements.
    • SCAN
      • iterates the set of keys in the currently selected Redis database.
      • returns  list of keys.
    • SSCAN
      • iterates elements of Sets types.
      • returns list of Set members
    • HSCAN
      • iterates fields of Hash types and their associated values.
      • returns an array of elements contain two elements, a field and a value, for every returned element of the Hash.
    • ZSCAN
      • iterates elements of Sorted Set types and their associated scores.
      • returns an `array of elements contain two elements, a member and its associated score, for every returned element of the sorted set.
  • Since these commands allow for incremental iteration, returning only a small number of elements per call, they can be used in production without the downside of commands like KEYS or SMEMBERS that may block the server for a long time  when called against big collections of keys or elements.

 

SCAN basic usage

  • SCAN is a cursor based iterator. This means that at every call of the command, the server returns an updated cursor that the user needs to use as the cursor argument in the next call.
  • An iteration starts when the cursor is set to 0, and terminates when the cursor returned by the server is 0.

Scan Example

Consider a situation where we have keys from u1 to u15 and n1 to n15 (total 31 keys) in our redis database.

We start the iteration by specifying the cursor as 0

redis:6379> scan 0
1) "18"
2) 1) "n14"
 2) "u12"
 3) "n16"
 4) "n5"
 5) "u15"
 6) "u8"
 7) "n13"
 8) "u4"
 9) "n1"
 10) "u13"
 11) "u7"

SCAN return value is an array of two values: the first value is the new cursor to use in the next call, the second value is an array of elements.

We start the next scan by specifying cursor as 18

redis:6379> scan 18
1) "5"
2) 1) "u9"
 2) "n12"
 3) "n7"
 4) "n9"
 5) "n6"
 6) "u14"
 7) "u11"
 8) "n8"
 9) "n10"
 10) "n3"

Next we specify cursor as 5

redis:6379> scan 5
1) "15"
2) 1) "u1"
 2) "u10"
 3) "u6"
 4) "n15"
 5) "n2"
 6) "n4"
 7) "n11"
 8) "u3"
 9) "u2"
 10) "u5"

Next we specify the cursor as 15

redis:6379> scan 15
1) "0"
2) (empty list or set)
redis:6379>

The cursor returned is 0, which means the iteration is complete. Also this iteration did not have any additional results, so second part of result contains an empty list.

 

Scan guarantees

The SCAN command, and the other commands in the SCAN family, are able to provide to the user a set of guarantees associated to full iterations.

  • A full iteration (iteration that starts and ends with cursor 0), always retrieves all the elements that were present in the collection from the start to the end of a full iteration. This means that if a given element is inside the collection when an iteration is started, and is still there when an iteration terminates, then at some point SCAN returned it to the user.
  • A full iteration never returns any element that was NOT present in the collection from the start to the end of a full iteration. So if an element was removed before the start of an iteration, and is never added back to the collection for all the time an iteration lasts, SCAN ensures that this element will never be returned.

However because SCAN has very little state associated (just the cursor) it has the following drawbacks:

  • A given element may be returned multiple times. It is up to the application to handle the case of duplicated elements.
  • Elements that were not constantly present in the collection during a full iteration, may be returned or not: it is undefined.

Number of elements returned at every SCAN call

SCAN family functions do not guarantee that the number of elements returned per call are in a given range. The commands are also allowed to return zero elements, and the client should not consider the iteration complete as long as the returned cursor is not zero.

However there is a way for the user to tune the order of magnitude of the number of returned elements per call using the COUNT option.

 

COUNT

Basically with COUNT the user specified the amount of work that should be done at every call in order to retrieve elements from the collection. This is just a hint for the implementation

  • The default COUNT value is 10.
  • When iterating the key space, or a Set, Hash or Sorted Set that is big enough to be represented by a hash table, assuming no MATCH option is used, the server will usually return count or a bit more than count elements per call.
  • When iterating Sets encoded as intsets (small sets composed of just integers), or Hashes and Sorted Sets encoded as ziplists (small hashes and sets composed of small individual values), usually all the elements are returned in the first SCAN call regardless of the COUNT value.

Important: there is no need to use the same COUNT value for every iteration. The caller is free to change the count from one iteration to the other as required, as long as the cursor passed in the next call is the one obtained in the previous call to the command.

The MATCH option

It is possible to only iterate elements matching a given glob-style pattern, similarly to the behavior of the KEYS command that takes a pattern as only argument.

To do so, just append the MATCH <pattern> arguments at the end of the SCAN command (it works with all the SCAN family commands).

 

redis:6379> scan 0 match n* count 5
1) "20"
2) 1) "n14"
 2) "n16"
 3) "n5"

redis:6379> scan 20 match n* count 10
1) "22"
2) 1) "n13"
 2) "n1"
 3) "n12"
 4) "n7"
 5) "n9"

redis:6379> scan 22 match n* count 10
1) "7"
2) 1) "n6"
 2) "n8"
 3) "n10"
 4) "n3"
 5) "n15"
 6) "n2"
 7) "n4"

redis:6379> scan 7 match n* count 5
1) "0"
2) 1) "n11"

redis:6379>

 

If you remove the match for subsequent cursor queries, the result will be different from original match query.

redis:6379> scan 0 match n*
1) "18"
2) 1) "n14"
 2) "n16"
 3) "n5"
 4) "n13"
 5) "n1"

redis:6379> scan 18 match n*
1) "5"
2) 1) "n12"
 2) "n7"
 3) "n9"
 4) "n6"
 5) "n8"
 6) "n10"
 7) "n3"

redis:6379> scan 5
1) "15"
2) 1) "u1"
 2) "u10"
 3) "u6"
 4) "n15"
 5) "n2"
 6) "n4"
 7) "n11"
 8) "u3"
 9) "u2"
 10) "u5"

redis:6379> scan 15 
1) "0"
2) (empty list or set)

redis:6379>

 

Example of SSCAN on a SET

redis:6379> SADD myset 1 2 3 4 5 6 aa ab ac ad ae af ag ah
(integer) 14

redis:6379> sscan myset 0 match a* count 3
1) "6"
2) 1) "ad"
 2) "af"

redis:6379> sscan myset 6 match a* count 3
1) "1"
2) 1) "ab"

redis:6379> sscan myset 1 match a* count 3
1) "5"
2) 1) "aa"
 2) "ac"
 3) "ah"

redis:6379> sscan myset 5 match a* count 3
1) "7"
2) 1) "ag"
 2) "ae"

redis:6379> sscan myset 7 match a* count 3
1) "0"
2) (empty list or set)

redis:6379>

 

HSCAN Example

redis:6379> hmset users name "Sunit" dept "R&D" skills "Java, Scala, Spring, Redis, Elastic"
OK

redis:6379> hscan users 0
1) "0"
2) 1) "name"
 2) "Sunit"
 3) "dept"
 4) "R&D"
 5) "skills"
 6) "Java, Scala, Spring, Redis, Elastic"

redis:6379>

 

3.0 List Commands commands

 

3.1 LPUSH/ RPUSH

LPUSH key value [value ...]
  • Insert all the specified values at the head of the list stored at key.
  • If keydoes not exist, it is created as empty list before performing the push operations.
  • Elements are inserted one after the other to the head of the list, from the leftmost element to the rightmost element.
  • e.g command LPUSH mylist a b c will create a list [c , b, a]

 

RPUSH key value [value ...]
  • Same as LPUSH, except that it adds element to end/tail of the list
  • e.g command RPUSH mylist a b c will create a list [a, b, c]

 

3.2 LPOP / RPOP

LPOP key

Removes and returns the first element of the list stored at key.

RPOP key

Removes and returns the last element of the list stored at key.

 

3.3 LLEN

LLEN key
  • Returns the length of the list stored at key.
  • If key does not exist, it is interpreted as an empty list and 0 is returned.
  • An error is returned when the value stored at key is not a list.

 

3.4 LRANGE

LRANGE key start stop
  • Used to iterate a list.
  • Returns the element of the lists from start to stop position, for the specified key.
  • Indexes start at 0.
  • -1 is used to indicate end of list, -2 to indicate penultimate element, etc.
  • Out of range indexes will not produce an error.

 

3.5 LTRIM

LTRIM key start stop
  • Trim an existing list, from the start to stop index.
  • The start and stop index work same as in LRANGE.

 

3.6 LSET

LSET key index value

Sets the list element at index to value.

3.7 BLPOP/ BRPOP

BLPOP key [key ...] timeout
BRPOP key [key ...] timeout
  • BLPOP and BRPOP are the blocking version of LPOP and RPOP respectively, because they block the connection when there are no elements to pop from any of the given lists.
  • When BLPOP/BRPOP causes a client to block and if a non-zero timeout is specified, the client will unblock returning a nil multi-bulk value when the specified timeout has expired without a push operation against at least one of the specified keys.
  • The timeout argument is interpreted as an integer value specifying the maximum number of seconds to block.
  • A timeout of zero can be used to block indefinitely.
  • Blocking for multiple keys
    • If the client tries to blocks for multiple keys, but at least one key contains elements, the returned key / element pair is the first key from left to right that has one or more elements.
    • In this case the client is not blocked.
    • So for instance BLPOP key1 key2 key3 key4 0, assuming that both key2 and key4 are non-empty, will always return an element from key2.
  • Blocking by multiple clients
    • first client to be served is the one that was waiting for more time (the first that blocked for the key).

 

Examples

redis:6379> GET mylist
(nil)

redis:6379> LPUSH mylist "A" "B" "C"
(integer) 3

redis:6379> LRANGE mylist 0 -1
1) "C"
2) "B"
3) "A"

redis:6379> RPUSH mylist "D" "E" "F" "G"
(integer) 7

redis:6379> LRANGE mylist 0 -1
1) "C"
2) "B"
3) "A"
4) "D"
5) "E"
6) "F"
7) "G"

redis:6379> LRANGE mylist 3 2
(empty list or set)

redis:6379> LRANGE mylist 3 5
1) "D"
2) "E"
3) "F"

redis:6379> LLEN mylist
(integer) 7

redis:6379> LPOP mylist
"C"

redis:6379> RPOP mylist
"G"

redis:6379> LLEN mylist
(integer) 5

redis:6379> LRANGE mylist 0 -1
1) "B"
2) "A"
3) "D"
4) "E"
5) "F"

redis:6379> LSET mylist 0 "A"
OK

redis:6379> LRANGE mylist 0 2
1) "A"
2) "A"
3) "D"

redis:6379> LSET mylist 1 "B"
OK

redis:6379> LRANGE mylist 0 -1
1) "A"
2) "B"
3) "D"
4) "E"
5) "F"

redis:6379> LTRIM mylist 3 4
OK

redis:6379> LLEN mylist
(integer) 2

redis:6379> LRANGE mylist 0 -1
1) "E"
2) "F"

redis:6379>

redis:6379> KEYS mylist*
(empty list or set)

redis:6379> BLPOP mylist1 mylist2 mylist3 5
(nil)
(5.05s)

redis:6379> LPUSH mylist3 "A" "B"
(integer) 2

redis:6379> BLPOP mylist1 mylist2 mylist3 5
1) "mylist3"
2) "B"

redis:6379> LPUSH mylist2 "1" "2"
(integer) 2

redis:6379> BLPOP mylist1 mylist2 mylist3 5
1) "mylist2"
2) "2"

redis:6379> LRANGE mylist2 0 -1
1) "1"

redis:6379> LRANGE mylist3 0 -1
1) "A"

redis:6379>

 

4.0 SET Commands

4.1 SADD

SADD key member [member ...]
  • Add the specified members to the set stored at key.
  • Specified members that are already a member of this set are ignored.
  • If key does not exist, a new set is created before adding the specified members.
  • An error is returned when the value stored at key is not a set.
  • Returns the number of elements that were added to the set.

 

4.2 SISMEMBER

SISMEMBER key member
  • Returns if member is a member of the set stored at key.
    • 1 if the element is a member of the set.
    • 0 if the element is not a member of the set, or if key does not exist.

 

4.3 SMEMBERS

SMEMBERS key

Returns all the members of the set value stored at key.

 

4.4 SPOP

SPOP key [count]
  • Removes and returns one or more random elements from the set value store at key.
  • Returns the removed element, or nil when key does not exist.

 

4.5 SREM

SREM key member [member ...]
  • Remove the specified members from the set stored at key.
  • Specified members that are not a member of this set are ignored.
  • If key does not exist, it is treated as an empty set and this command returns 0.
  • An error is returned when the value stored at key is not a set.
  • Returns the number of members that were removed from the set, not including non existing members.

 

4.6 SSCAN

SSCAN key cursor [MATCH pattern] [COUNT count]
  • iterates elements of Sets types.
  • returns list of Set members

 

4.7 SUNION

SUNION key [key ...]

Returns the members of the set resulting from the union of all the given sets.

 

Examples of Set commands

redis:6379> KEYS myset*
(empty list or set)

redis:6379> SADD myset1 1 2 3 4 5
(integer) 5

redis:6379> SADD myset1 3 5 7 9 
(integer) 2

redis:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "7"
7) "9"

redis:6379> SISMEMBER myset1 "10"
(integer) 0

redis:6379> SISMEMBER myset1 "2"
(integer) 1

redis:6379> SPOP myset1 3
1) "2"
2) "5"
3) "4"

redis:6379> SMEMBERS myset1
1) "1"
2) "3"
3) "7"
4) "9"

redis:6379> SADD myset1 2 4 6 8 10
(integer) 5

redis:6379> SMEMBERS myset1
1) "1"
2) "2"
3) "3"
4) "4"
5) "6"
6) "7"
7) "8"
8) "9"
9) "10"

redis:6379> SREM myset1 1 2 3 4 5
(integer) 4

redis:6379> SMEMBERS myset1
1) "6"
2) "7"
3) "8"
4) "9"
5) "10"

redis:6379> SADD myset2 "A" "B" "C" 5 7 9
(integer) 6

redis:6379> SADD myset3 "C" "D" 10 11
(integer) 4

redis:6379> SUNION myset1 myset2 myset3
 1) "11"
 2) "10"
 3) "D"
 4) "C"
 5) "A"
 6) "8"
 7) "9"
 8) "7"
 9) "B"
10) "5"
11) "6"

redis:6379>

 

Example of SSCAN on a SET

redis:6379> SADD myset 1 2 3 4 5 6 aa ab ac ad ae af ag ah
(integer) 14

redis:6379> sscan myset 0 match a* count 3
1) "6"
2) 1) "ad"
 2) "af"

redis:6379> sscan myset 6 match a* count 3
1) "1"
2) 1) "ab"

redis:6379> sscan myset 1 match a* count 3
1) "5"
2) 1) "aa"
 2) "ac"
 3) "ah"

redis:6379> sscan myset 5 match a* count 3
1) "7"
2) 1) "ag"
 2) "ae"

redis:6379> sscan myset 7 match a* count 3
1) "0"
2) (empty list or set)

redis:6379>

 

 

 

5. Sorted Sets

 

5.1 ZADD

ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
  • Adds members along with their scores to the sorted set, stored at give KEY.
  • It is possible to specify multiple score / member pairs.
  • If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.
  • If key does not exist, a new sorted set with the specified members as sole members is created, like if the sorted set was empty.
  • If the key exists but does not hold a sorted set, an error is returned.
  • Range of Integer scores are -(2^53) and +(2^53)
  • Elements with same score are sorted lexicographically.

 

Additional OPTIONS (3.0.2 onwards)

ZADD supports a list of options, specified after the name of the key and before the first score argument. Options are:

  • XX: Only update elements that already exist. Never add elements.
  • NX: Don’t update already existing elements. Always add new elements.
  • CH:
    • Modify the return value from the number of new elements added, to the total number of elements changed.
    • Changed elements are new elements added and elements already existing for which the score was updated.
  • INCR: When this option is specified ZADD acts like ZINCRBY. Only one score-element pair can be specified in this mode.

 

5.2. ZINCRBY

ZINCRBY key increment member

Increments the score of member in the sorted set stored at key byincrement. If member does not exist in the sorted set, it is added with increment as its score

5.3 ZCARD 

ZCARD key

Returns the sorted set cardinality (number of elements) of the sorted set stored at key.

 

5.4 ZCOUNT

ZCOUNT key min max

Returns the number of elements in the sorted set at key with a score between min and max.

 

5.5 ZRANGE / ZRANGEBYSCORE

ZRANGE key start stop [WITHSCORES]

Returns the specified range of elements in the sorted set

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

Returns all the elements in the sorted set at key with a score between minand max

By default, the interval specified by min and max is closed (inclusive). It is possible to specify an open interval (exclusive) by prefixing the score with the character (.

 

5.6 ZRANK

ZRANK key member

Returns the rank of member in the sorted set, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.

 

 

5.7 ZREM

ZREM key member [member ...]

Removes the specified members from the sorted set stored at key. Non existing members are ignored.SCAN key cursor [MATCH pattern] [COUNT count]

 

 

5.8 ZSCORE

ZSCORE key member

Returns the score of member in the sorted set at key.

 

5.9 ZSCAN

ZSCAN key cursor [MATCH pattern] [COUNT count]

 

 

Examples

redis:6379> ZADD users CH 20 "Anderson" 21 "Bob" 13 "John" 3 "Maxwell" 
(integer) 4

redis:6379> ZRANGE users 0 50 
1) "Maxwell"
2) "John"
3) "Anderson"
4) "Bob"

redis:6379> ZRANGE users 0 50 WITHSCORES
1) "Maxwell"
2) "3"
3) "John"
4) "13"
5) "Anderson"
6) "20"
7) "Bob"
8) "21"

redis:6379> ZADD users CH 15 "Bob"
(integer) 1

redis:6379> ZRANGE users 0 50 
1) "Maxwell"
2) "John"
3) "Bob"
4) "Anderson"

redis:6379> ZRANGE users 0 50 WITHSCORES
1) "Maxwell"
2) "3"
3) "John"
4) "13"
5) "Bob"
6) "15"
7) "Anderson"
8) "20"

redis:6379> ZINCRBY users 10 "John"
"23"

redis:6379> ZRANGE users 0 50 WITHSCORES
1) "Maxwell"
2) "3"
3) "Bob"
4) "15"
5) "Anderson"
6) "20"
7) "John"
8) "23"

redis:6379> ZADD users 10 "Rishabh Pant" 10 "Colin Munro" 10 "Jason Roy"
(integer) 3

redis:6379> ZRANGE users 0 50 WITHSCORES
 1) "Maxwell"
 2) "3"
 3) "Colin Munro"
 4) "10"
 5) "Jason Roy"
 6) "10"
 7) "Rishabh Pant"
 8) "10"
 9) "Bob"
10) "15"
11) "Anderson"
12) "20"
13) "John"
14) "23"

redis:6379> ZREM users "Anderson" "John"
(integer) 2

redis:6379> ZCARD users
(integer) 5

redis:6379> ZRANGE users 0 50
1) "Maxwell"
2) "Colin Munro"
3) "Jason Roy"
4) "Rishabh Pant"
5) "Bob"

redis:6379> ZRANK users "Bob"
(integer) 4

redis:6379> ZRANK users "Jason Roy"
(integer) 2

redis:6379> ZREM users "Bob"
(integer) 1

redis:6379> ZADD users 15 "Iyer" 18 "Gambhir" 20 "Morris" 25 "Boult"
(integer) 4

redis:6379> ZRANGE users 0 50
1) "Maxwell"
2) "Colin Munro"
3) "Jason Roy"
4) "Rishabh Pant"
5) "Iyer"
6) "Gambhir"
7) "Morris"
8) "Boult"

redis:6379> ZRANGEBYSCORE users 10 20 WITHSCORES
 1) "Colin Munro"
 2) "10"
 3) "Jason Roy"
 4) "10"
 5) "Rishabh Pant"
 6) "10"
 7) "Iyer"
 8) "15"
 9) "Gambhir"
10) "18"
11) "Morris"
12) "20"

redis:6379> ZRANGEBYSCORE users (10 20 WITHSCORES
1) "Iyer"
2) "15"
3) "Gambhir"
4) "18"
5) "Morris"
6) "20"

redis:6379> ZRANGEBYSCORE users (10 (20 WITHSCORES
1) "Iyer"
2) "15"
3) "Gambhir"
4) "18"

redis:6379> ZCOUNT users 10 20
(integer) 6

redis:6379> ZCOUNT users (10 20
(integer) 3

redis:6379> ZCOUNT users (10 (20
(integer) 2

redis:6379> ZSCORE users "Iyer"
"15"

redis:6379> ZSCAN users 0 MATCH ?a*
1) "0"
2) 1) "Maxwell"
 2) "3"
 3) "Jason Roy"
 4) "10"
 5) "Gambhir"
 6) "18"

 

 

6. Hashes

 

6.1 HSET / HMSET

HSET key field value

Sets the value for the field for the hash specified by the key

HMSET key field value [field value ...]

Setting multiple values for multiple fields

 

6.2 HGET / HMGET / HGETALL

HGET key field

Returns the value associated with field in the hash

HMGET key field [field ...]

Returns values of multiple fields in the hash.

HGETALL key

Returns all fields and values of the hash stored at key.

 

6.3 HEXISTS

HEXISTS key field

Returns if field is an existing field in the hash stored at the specified key

 

6.4 HKEYS

HKEYS key

Returns all field names in the hash stored at key.

 

6.5 HDEL

HDEL key field [field ...]

Removes the specified fields from the hash stored at key

 

6.6 HSCAN

HSCAN key cursor [MATCH pattern] [COUNT count]

 

 

Examples

redis:6379> HSET user:1000 firstName "Sunit"
(integer) 1

redis:6379> HMSET user:1000 lastName "Chatterjee" gender "Male" country "India"
OK

redis:6379> HKEYS user:1000
1) "firstName"
2) "lastName"
3) "gender"
4) "country"

redis:6379> HGETALL user:1000
1) "firstName"
2) "Sunit"
3) "lastName"
4) "Chatterjee"
5) "gender"
6) "Male"
7) "country"
8) "India"

redis:6379> HGET user:1000 firstName
"Sunit"

redis:6379> HMGET user:1000 firstName lastName country
1) "Sunit"
2) "Chatterjee"
3) "India"

redis:6379> HEXISTS user:1000 gender
(integer) 1

redis:6379> HDEL user:1000 gender
(integer) 1

redis:6379> HEXISTS user:1000 gender
(integer) 0

redis:6379> HSCAN user:1000 0 COUNT 2
1) "0"
2) 1) "firstName"
 2) "Sunit"
 3) "lastName"
 4) "Chatterjee"
 5) "country"
 6) "India"

 

 

 

 

References

https://redis.io/commands

http://try.redis.io/

https://redislabs.com/community/ebook/