Redis Tutorial
Redis Tutorial
This tutorial provides good understanding on Redis concepts, needed to create and deploy
a highly scalable and performance-oriented system.
Audience
This tutorial is designed for Software Professionals who are willing to learn Redis in simple
and easy steps. After completing this tutorial, you will be at an intermediate level of
expertise from where you can take yourself to a higher level of expertise.
Prerequisites
Before proceeding with this tutorial, you should have basic knowledge of Data Structures.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected].
i
Redis
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Disclaimer & Copyright ..................................................................................................................................... i
Table of Contents ............................................................................................................................................ ii
7. Redis Strings......................................................................................................................................... 28
Redis Strings Commands ............................................................................................................................... 28
String Set Command ...................................................................................................................................... 30
ii
Redis
iii
Redis
iv
Redis
vi
Redis
Redis - Basics
1
1. Redis Overview Redis
Redis is an open source, advanced key-value store and an apt solution for building high-
performance, scalable web applications.
Redis holds its database entirely in the memory, using the disk only for persistence.
Redis has a relatively rich set of data types when compared to many key-value
data stores.
Redis Advantages
Following are certain advantages of Redis.
Exceptionally fast: Redis is very fast and can perform about 110000 SETs per
second, about 81000 GETs per second.
Supports rich data types: Redis natively supports most of the datatypes that
developers already know such as list, set, sorted set, and hashes. This makes it
easy to solve a variety of problems as we know which problem can be handled
better by which data type.
Operations are atomic: All Redis operations are atomic, which ensures that if
two clients concurrently access, Redis server will receive the updated value.
Multi-utility tool: Redis is a multi-utility tool and can be used in a number of use
cases such as caching, messaging-queues (Redis natively supports
Publish/Subscribe), any short-lived data in your application, such as web
application sessions, web page hit counts, etc.
2
2. Redis Environment Redis
In this chapter, you will learn about the environmental setup for Redis.
Start Redis
$redis-server
redis 127.0.0.1:6379>
In the above prompt, 127.0.0.1 is your machine's IP address and 6379 is the port on
which Redis server is running. Now type the following PING command.
Redis desktop manager will give you UI to manage your Redis keys and data.
3
3. Redis Configuration Redis
In Redis, there is a configuration file (redis.conf) available at the root directory of Redis.
Although you can get and set all Redis configurations by Redis CONFIG command.
Syntax
Following is the basic syntax of Redis CONFIG command.
Example
redis 127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "notice"
Example
redis 127.0.0.1:6379> CONFIG GET *
1) "dbfilename"
2) "dump.rdb"
3) "requirepass"
4) ""
5) "masterauth"
6) ""
7) "unixsocket"
8) ""
9) "logfile"
10) ""
11) "pidfile"
12) "/var/run/redis.pid"
13) "maxmemory"
14) "0"
4
Redis
15) "maxmemory-samples"
16) "3"
17) "timeout"
18) "0"
19) "tcp-keepalive"
20) "0"
21) "auto-aof-rewrite-percentage"
22) "100"
23) "auto-aof-rewrite-min-size"
24) "67108864"
25) "hash-max-ziplist-entries"
26) "512"
27) "hash-max-ziplist-value"
28) "64"
29) "list-max-ziplist-entries"
30) "512"
31) "list-max-ziplist-value"
32) "64"
33) "set-max-intset-entries"
34) "512"
35) "zset-max-ziplist-entries"
36) "128"
37) "zset-max-ziplist-value"
38) "64"
39) "hll-sparse-max-bytes"
40) "3000"
41) "lua-time-limit"
42) "5000"
43) "slowlog-log-slower-than"
44) "10000"
45) "latency-monitor-threshold"
46) "0"
47) "slowlog-max-len"
48) "128"
49) "port"
50) "6379"
5
Redis
51) "tcp-backlog"
52) "511"
53) "databases"
54) "16"
55) "repl-ping-slave-period"
56) "10"
57) "repl-timeout"
58) "60"
59) "repl-backlog-size"
60) "1048576"
61) "repl-backlog-ttl"
62) "3600"
63) "maxclients"
64) "4064"
65) "watchdog-period"
66) "0"
67) "slave-priority"
68) "100"
69) "min-slaves-to-write"
70) "0"
71) "min-slaves-max-lag"
72) "10"
73) "hz"
74) "10"
75) "no-appendfsync-on-rewrite"
76) "no"
77) "slave-serve-stale-data"
78) "yes"
79) "slave-read-only"
80) "yes"
81) "stop-writes-on-bgsave-error"
82) "yes"
83) "daemonize"
84) "no"
85) "rdbcompression"
86) "yes"
6
Redis
87) "rdbchecksum"
88) "yes"
89) "activerehashing"
90) "yes"
91) "repl-disable-tcp-nodelay"
92) "no"
93) "aof-rewrite-incremental-fsync"
94) "yes"
95) "appendonly"
96) "no"
97) "dir"
98) "/home/deepak/Downloads/redis-2.8.13/src"
99) "maxmemory-policy"
100) "volatile-lru"
101) "appendfsync"
102) "everysec"
103) "save"
104) "3600 1 300 100 60 10000"
105) "loglevel"
106) "notice"
107) "client-output-buffer-limit"
108) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
109) "unixsocketperm"
110) "0"
111) "slaveof"
112) ""
113) "notify-keyspace-events"
114) ""
115) "bind"
116) ""
7
Redis
Edit Configuration
To update configuration, you can edit redis.conf file directly or you can update
configurations via CONFIG set command.
Syntax
Following is the basic syntax of CONFIG SET command.
Example
redis 127.0.0.1:6379> CONFIG SET loglevel "notice"
OK
redis 127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "notice"
8
4. Redis Data Types Redis
Strings
Redis string is a sequence of bytes. Strings in Redis are binary safe, meaning they have a
known length not determined by any special terminating characters. Thus, you can store
anything up to 512 megabytes in one string.
Example
redis 127.0.0.1:6379> SET name "tutorialspoint"
OK
redis 127.0.0.1:6379> GET name
"tutorialspoint"
In the above example, SET and GET are Redis commands, name is the key used in Redis
and tutorialspoint is the string value that is stored in Redis.
Hashes
A Redis hash is a collection of key value pairs. Redis Hashes are maps between string
fields and string values. Hence, they are used to represent objects.
Example
redis 127.0.0.1:6379> HMSET user:1 username tutorialspoint password
tutorialspoint points 200
OK
redis 127.0.0.1:6379> HGETALL user:1
1) "username"
2) "tutorialspoint"
3) "password"
4) "tutorialspoint"
5) "points"
6) "200"
9
Redis
In the above example, hash data type is used to store the user's object which contains
basic information of the user. Here HMSET, HGETALL are commands for Redis,
while user:1 is the key.
Every hash can store up to 232 - 1 field-value pairs (more than 4 billion).
Lists
Redis Lists are simply lists of strings, sorted by insertion order. You can add elements to
a Redis List on the head or on the tail.
Example
redis 127.0.0.1:6379> lpush tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> lpush tutoriallist mongodb
(integer) 2
redis 127.0.0.1:6379> lpush tutoriallist rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange tutoriallist 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
The max length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements
per list).
Sets
Redis Sets are an unordered collection of strings. In Redis, you can add, remove, and test
for the existence of members in O(1) time complexity.
Example
redis 127.0.0.1:6379> sadd tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist mongodb
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 0
10
Redis
1) "rabitmq"
2) "mongodb"
3) "redis"
Note: In the above example, rabitmq is added twice, however due to unique property of
the set, it is added only once.
The max number of members in a set is 232 - 1 (4294967295, more than 4 billion of
members per set).
Sorted Sets
Redis Sorted Sets are similar to Redis Sets, non-repeating collections of Strings. The
difference is, every member of a Sorted Set is associated with a score, that is used in
order to take the sorted set ordered, from the smallest to the greatest score. While
members are unique, the scores may be repeated.
Example
redis 127.0.0.1:6379> zadd tutoriallist 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 1000
1) "redis"
2) "mongodb"
3) "rabitmq"
11
Redis
Redis Commands
12
5. Redis Commands Redis
To run commands on Redis server, you need a Redis client. Redis client is available in
Redis package, which we have installed earlier.
Syntax
Following is the basic syntax of Redis client.
$redis-cli
Example
Following example explains how we can start Redis client.
To start Redis client, open the terminal and type the command redis-cli. This will connect
to your local server and now you can run any command.
$redis-cli
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> PING
PONG
In the above example, we connect to Redis server running on the local machine and
execute a command PING, that checks whether the server is running or not.
Syntax
$ redis-cli -h host -p port -a password
13
Redis
Example
Following example shows how to connect to Redis remote server, running on host
127.0.0.1, port 6379 and has password mypass.
PONG
14
6. Redis Keys Redis
Redis keys commands are used for managing keys in Redis. Following is the syntax for
using redis keys commands.
Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME
Example
redis 127.0.0.1:6379> SET tutorialspoint redis
OK
redis 127.0.0.1:6379> DEL tutorialspoint
(integer) 1
In the above example, DEL is the command, while tutorialspoint is the key. If the key
is deleted, then the output of the command will be (integer) 1, otherwise it will be (integer)
0.
Sr.
Command & Description
No.
DEL key
1
This command deletes the key, if it exists
DUMP key
2
This command returns a serialized version of the value stored at the specified key
EXISTS key
3
This command checks whether the key exists or not
15
Redis
KEYS pattern
8
Finds all keys matching the specified pattern
MOVE key db
9
Moves a key to another database
PERSIST key
10
Removes the expiration from the key
PTTL key
11
Gets the remaining time in keys expiry in milliseconds
TTL key
12
Gets the remaining time in keys expiry
RANDOMKEY
13
Returns a random key from Redis
TYPE key
16
Returns the data type of the value stored in the key
Return Value
Number of keys that were removed.
Syntax
Following is the basic syntax of Redis DEL command.
16
Redis
Example
First, create a key in Redis and set some value in it.
Return Value
Serialized value (String)
Syntax
Following is the basic syntax of Redis DUMP command.
Example
First, create a key in Redis and set some value in it.
Return Value
Integer value
17
Redis
Syntax
Following is the basic syntax of Redis EXISTS command.
Example
redis 127.0.0.1:6379> EXISTS tutorialspoint-new-key
(integer) 0
Now, create a key with the name tutorialspoint-new-key and check for its existence.
Return Value
Integer value 1 or 0
Syntax
Following is the basic syntax of Redis Expire command.
Example
First, create a key in Redis and set some value in it.
In the above example, 1 minute (or 60 seconds) time is set for the key tutorialspoint.
After 1 minute, the key will expire automatically.
18
Redis
Return Value
Integer value 1 or 0
Syntax
Following is the basic syntax of Redis Expireat command.
Example
First, create a key in Redis and set some value in it.
Return Value
Integer value 1 or 0
Syntax
Following is the basic syntax of Redis Expire command.
19
Redis
Example
First, create a key in Redis and set some value in it.
In the above example, 5 seconds time is set for the key tutorialspoint. After 5 seconds,
the key will expire automatically.
Return Value
Integer value 1 or 0
Syntax
Following is the basic syntax of Redis Pexpireat command.
Example
First, create a key in Redis and set some value in it.
Keys Command
Redis KEYS command is used to search keys with a matching pattern.
20
Redis
Return Value
List of keys with a matching pattern (Array).
Syntax
Following is the basic syntax of Redis KEYS command.
Example
First, create a key in Redis and set some value in it.
Now, search Redis with keys starting from the keyword tutorial.
Return Value
Integer value 1 or 0
21
Redis
Syntax
Following is the basic syntax of Redis MOVE command.
Example
First, create a key in Redis and set some value in it.
In Redis, by default 0th database is selected, so now we are moving the generated key in
the second database.
Return Value
Integer value 1 or 0
Syntax
Following is the basic syntax of Redis PERSIST command.
Example
First, create a key in Redis and set some value in it.
Now, set the expiry of the key, and later remove the expiry.
22
Redis
1) (integer) 60
redis 127.0.0.1:6379> PERSIST tutorial1
1) (integer) 1
redis 127.0.0.1:6379> TTL tutorial1
1) (integer) -1
Return Value
Integer value TTL in milliseconds, or a negative value.
TTL in milliseconds.
-1, if the key does not have an expiry timeout.
-2, if the key does not exist.
Syntax
Following is the basic syntax of Redis PTTL command.
Example
First, create a key in Redis and set some value in it.
Now set the expiry of the key, and later check the remaining expiry time.
Return Value
Integer value TTL in milliseconds, or a negative value.
TTL in milliseconds.
23
Redis
Syntax
Following is the basic syntax of Redis TTL command.
Example
First, create a key in Redis and set some value in it.
Now, set the expiry of the key, and later check the remaining expiry time.
Return Value
String, a random key or nil, if the database is empty.
Syntax
Following is the basic syntax of Redis RANDOMKEY command.
Example
First, create some keys in Redis and set some values in it.
24
Redis
OK
Return Value
String reply OK or error.
It returns an error if the old key and the new key names are equal, or when the key does
not exist. If the new key already exists, then it overwrites the existing key.
Syntax
Following is the basic syntax of Redis RENAME command.
Example
First, create some keys in Redis and set some values in it.
Return Value
Integer reply 1 or 0.
25
Redis
Syntax
Following is the basic syntax of Redis RENAMENX command.
Example
First, create some keys in Redis and set some values in it.
Return Value
String reply, data type of the value stored in the key or none.
Syntax
Following is the basic syntax of Redis TYPE command.
Example
First, create some keys in Redis and set some values in it.
27
7. Redis Strings Redis
Redis strings commands are used for managing string values in Redis. Following is the
syntax for using Redis string commands.
Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME
Example
redis 127.0.0.1:6379> SET tutorialspoint redis
OK
redis 127.0.0.1:6379> GET tutorialspoint
"redis"
In the above example, SET and GET are the commands, while tutorialspoint is the key.
Sr.
Command & Description
No.
GET key
2
Gets the value of a key
28
Redis
STRLEN key
11
Gets the length of the value stored in a key
INCR key
15
Increments the integer value of a key by one
DECR key
18
Decrements the integer value of a key by one
29
Redis
Return Value
Simple string reply. OK, if the value is set in the key. Null, if the value does not set.
Syntax
Following is the basic syntax of Redis SET command.
Example
redis 127.0.0.1:6379> SET tutorialspoint redis
OK
Options
In SET command, there are many options available, that modify the behavior of
command. Following is the basic syntax of SET command with available options.
redis 127.0.0.1:6379> SET KEY VALUE [EX seconds] [PX milliseconds] [NX|XX]
Example
redis 127.0.0.1:6379> SET tutorialspoint redis EX 60 NX
OK
The above example will set the key tutorialspoint, with expiry of 60 seconds, if the key
does not exist.
Return Value
Simple string reply. Value or key or nil.
30
Redis
Syntax
Following is the basic syntax of Redis GET command.
Example
First, set a key in Redis and then get it.
The function handles out of range requests by limiting the resulting range to the actual
length of the string.
Return Value
Simple string reply.
Syntax
Following is the basic syntax of Redis GETRANGE command.
Example
First, set a key in Redis and then get some part of it.
31
Redis
Return Value
Simple string reply, old value of the key. If the key does not exist, then nil is returned.
Syntax
Following is the basic syntax of Redis GETSET command.
Example
redis 127.0.0.1:6379> GETSET mynewkey "This is my test key"
(nil)
redis 127.0.0.1:6379> GETSET mynewkey "This is my new value to test getset"
"This is my test key"
Return Value
Integer, the bit value stored at the offset.
Syntax
Following is the basic syntax of Redis GETBIT command.
Example
redis 127.0.0.1:6379> SETBIT mykey 7 1
(integer) 0
redis 127.0.0.1:6379> GETBIT mykey 0
(integer) 0
redis 127.0.0.1:6379> GETBIT mykey 7
(integer) 1
redis 127.0.0.1:6379> GETBIT mykey 100
32
Redis
(integer) 0
Return Value
Array, list of values at the specified keys.
Syntax
Following is the basic syntax of Redis MGET command.
Example
redis 127.0.0.1:6379> SET key1 "hello"
OK
redis 127.0.0.1:6379> SET key2 "world"
OK
redis 127.0.0.1:6379> MGET key1 key2 someOtherKey
1) "Hello"
2) "World"
3) (nil)
Return Value
Integer, the bit value stored at the offset.
Syntax
Following is the basic syntax of Redis GETBIT command.
33
Redis
Example
redis 127.0.0.1:6379> SETBIT mykey 7 1
(integer) 0
redis 127.0.0.1:6379> GETBIT mykey 0
(integer) 0
redis 127.0.0.1:6379> GETBIT mykey 7
(integer) 1
redis 127.0.0.1:6379> GETBIT mykey 100
(integer) 0
Return Value
Simple string reply. OK, if the value is set in key. Null, if the value is not set.
Syntax
Following is the basic syntax of Redis SETEX command.
Example
redis 127.0.0.1:6379> SETEX mykey 60 redis
OK
redis 127.0.0.1:6379> TTL mykey
60
redis 127.0.0.1:6379> GET mykey
"redis
Return Value
34
Redis
Integer reply 1 or 0
Syntax
Following is the basic syntax of Redis SETNX command.
Example
redis 127.0.0.1:6379> SETNX mykey redis
(integer) 1
redis 127.0.0.1:6379> SETNX mykey mongodb
(integer) 0
redis 127.0.0.1:6379> GET mykey
"redis"
Return Value
Integer reply, the length of the string after it was modified by the command.
Syntax
Following is the basic syntax of Redis SETRANGE command.
Example
redis 127.0.0.1:6379> SET key1 "Hello World"
OK
redis 127.0.0.1:6379> SETRANGE key1 6 "Redis"
(integer) 11
redis 127.0.0.1:6379> GET key1
"Hello Redis"
35
Redis
Return Value
Integer reply, the length of the string at the key, or 0 when the key does not exist.
Syntax
Following is the basic syntax of Redis SETRANGE command.
Example
redis 127.0.0.1:6379> SET key1 "Hello World"
OK
redis 127.0.0.1:6379> STRLEN key1
(integer) 11
redis 127.0.0.1:6379> STRLEN key2
(integer) 0
Return Value
Simple string reply OK.
Syntax
Following is the basic syntax of Redis MSET command.
Example
redis 127.0.0.1:6379> MSET key1 "Hello" key2 "World"
OK
redis 127.0.0.1:6379> GET key1
"Hello"
redis 127.0.0.1:6379> GET key2
1) "World"
36
Redis
Return Value
Integer reply 1 or 0
Syntax
Following is the basic syntax of Redis MSETNX command.
Example
redis 127.0.0.1:6379> MSETNX key1 "Hello" key2 "world"
(integer) 1
redis 127.0.0.1:6379> MSETNX key2 "worlds" key3 "third key"
(integer) 0
redis 127.0.0.1:6379> MGET key1 key2 key3
1) "Hello"
2) "world"
3) (nil)
Return Value
Simple string reply OK.
Syntax
Following is the basic syntax of Redis PSETEX command.
Example
37
Redis
Return Value
Integer reply, the value of the key after the increment.
Syntax
Following is the basic syntax of Redis INCR command.
Example
redis 127.0.0.1:6379> SET visitors 1000
OK
redis 127.0.0.1:6379> INCR visitors
(integer) 1001
redis 127.0.0.1:6379> GET visitors
(integer) 1001
Return Value
Integer reply, the value of key after the increment.
38
Redis
Syntax
Following is the basic syntax of Redis INCRBY command.
Example
redis 127.0.0.1:6379> SET visitors 1000
OK
redis 127.0.0.1:6379> INCRBY visitors 5
(integer) 1005
redis 127.0.0.1:6379> GET visitors
(integer) 1005
Return Value
String reply, the value of the key after the increment.
Syntax
Following is the basic syntax of Redis INCRBYFLOAT command.
Example
redis 127.0.0.1:6379> SET visitors 1000.20
OK
redis 127.0.0.1:6379> INCRBYFLOAT visitors .50
1000.70
redis 127.0.0.1:6379> GET visitors
1000.70
key contains a value of the wrong type or contains a string that cannot be represented as
an integer. This operation is limited to 64 bit signed integers.
Return Value
Integer reply, the value of the key after the increment.
Syntax
Following is the basic syntax of Redis DECR command.
Example
redis 127.0.0.1:6379> SET visitors 1000
OK
redis 127.0.0.1:6379> DECR visitors
(integer) 999
redis 127.0.0.1:6379> SET visitors "13131312312312312312312rgergerg"
Ok
redis 127.0.0.1:6379> DECR visitors
ERR value is not an integer or out of range
Return Value
Integer reply, the value of the key after the increment.
Syntax
Following is the basic syntax of Redis DECRBY command.
Example
redis 127.0.0.1:6379> SET visitors 1000
OK
40
Redis
Return Value
Integer reply, the length of the string after the append operation.
Syntax
Following is the basic syntax of Redis APPEND command.
Example
redis 127.0.0.1:6379> SET mykey "hello"
OK
redis 127.0.0.1:6379> APPEND mykey " tutorialspoint"
(integer) 20
redis 127.0.0.1:6379> GET mykey
"hello tutorialspoint"
41
8. Redis Hashes Redis
Redis Hashes are maps between the string fields and the string values. Hence, they are
the perfect data type to represent objects.
In Redis, every hash can store up to more than 4 billion field-value pairs.
Example
redis 127.0.0.1:6379> HMSET tutorialspoint name "redis tutorial" description
"redis basic commands for caching" likes 20 visitors 23000
OK
redis 127.0.0.1:6379> HGETALL tutorialspoint
1) "name"
2) "redis tutorial"
3) "description"
4) "redis basic commands for caching"
5) "likes"
6) "20"
7) "visitors"
8) "23000"
In the above example, we have set Redis tutorials detail (name, description, likes, visitors)
in hash named tutorialspoint.
Sr.
Command & Description
No.
42
Redis
HGETALL key
4
Gets all the fields and values stored in a hash at the specified key
HKEYS key
7
Gets all the fields in a hash
HLEN key
8
Gets the number of fields in a hash
HVALS key
13
Gets all the values in a hash
Return Value
Integer reply, the number of fields that were removed from the hash, not including
specified but non-existing fields.
43
Redis
Syntax
Following is the basic syntax of Redis HDEL command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HDEL myhash field1
(integer) 1
redis 127.0.0.1:6379> HDEL myhash field2
(integer) 1
Return Value
Integer reply, 1 or 0.
Syntax
Following is the basic syntax of Redis HEXISTS command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HEXISTS myhash field1
(integer) 1
redis 127.0.0.1:6379> HEXISTS myhash field2
(integer) 0
44
Redis
Return Value
String reply, the value associated with the field. Nil, when the field is not present in the
hash or the key does not exist.
Syntax
Following is the basic syntax of Redis HGET command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HGET myhash field1
"foo"
redis 127.0.0.1:6379> HEXISTS myhash field2
(nil)
Return Value
Array reply, list of fields and their values stored in the hash, or an empty list when the key
does not exist.
Syntax
Following is the basic syntax of Redis HGETALL command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HSET myhash field2 "bar"
45
Redis
(integer) 1
redis 127.0.0.1:6379> HGETALL myhash
1) "field1"
2) "Hello"
3) "field2"
4) "World"
Return Value
Integer reply, the value at the field after the increment operation.
Syntax
Following is the basic syntax of Redis HINCRBY command.
Example
redis 127.0.0.1:6379> HSET myhash field1 20
(integer) 1
redis 127.0.0.1:6379> HINCRBY myhash field 1
(integer) 21
redis 127.0.0.1:6379> HINCRBY myhash field -1
(integer) 20
Return Value
String reply, the value of the field after the increment.
Syntax
46
Redis
Example
redis 127.0.0.1:6379> HSET myhash field 20.50
(integer) 1
redis 127.0.0.1:6379> HINCRBYFLOAT mykey field 0.1
"20.60"
Return Value
Array reply, list of fields in the hash, or an empty list when the key does not exist.
Syntax
Following is the basic syntax of Redis HKEYS command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HSET myhash field2 "bar"
(integer) 1
redis 127.0.0.1:6379> HKEYS myhash
1) "field1"
2) "field2"
Return Value
Integer reply, number of fields in the hash, or 0 when the key does not exist.
Syntax
47
Redis
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HSET myhash field2 "bar"
(integer) 1
redis 127.0.0.1:6379> HLEN myhash
(integer) 2
Return Value
Array reply, list of values associated with the given fields, in the same order as they are
requested.
Syntax
Following is the basic syntax of Redis HMGET command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HSET myhash field2 "bar"
(integer) 1
redis 127.0.0.1:6379> HMGET myhash field1 field2 nofield
1) "foo"
2) "bar"
3) (nil)
48
Redis
Return Value
Simple string reply.
Syntax
Following is the basic syntax of Redis HMSET command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo" field2 "bar"
OK
redis 127.0.0.1:6379> HGET myhash field1
"foo"
redis 127.0.0.1:6379> HMGET myhash field2
"bar"
Return Value
Integer reply
1 if fthe ield is a new field in the hash and value was set.
0 if the field already exists in the hash and the value was updated.
Syntax
Following is the basic syntax of Redis HSET command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
OK
49
Redis
Return Value
Integer reply
1 if the field is a new field in the hash and value was set.
0 if the field already exists in the hash and no operation was performed.
Syntax
Following is the basic syntax of Redis HSETNX command.
Example
redis 127.0.0.1:6379> HSETNX myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HSETNX myhash field1 "bar"
(integer) 0
redis 127.0.0.1:6379> HGET myhash field1
"foo"
Return Value
Array reply, list of values in the hash, or an empty list when the key does not exist.
Syntax
Following is the basic syntax of Redis HVALS command.
50
Redis
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
(integer) 1
redis 127.0.0.1:6379> HSET myhash field2 "bar"
(integer) 1
redis 127.0.0.1:6379> HVALS myhash
1) "foo"
2) "bar"
Return Value
Integer reply
1, if the field is a new field in the hash and the value was set.
0, if the field already exists in the hash and the value was updated.
Syntax
Following is the basic syntax of Redis HSET command.
Example
redis 127.0.0.1:6379> HSET myhash field1 "foo"
OK
redis 127.0.0.1:6379> HGET myhash field1
"foo"
51
9. Redis Lists Redis
Redis Lists are simply lists of strings, sorted by insertion order. You can add elements in
Redis lists in the head or the tail of the list.
Maximum length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements
per list).
Example
redis 127.0.0.1:6379> LPUSH tutorials redis
(integer) 1
redis 127.0.0.1:6379> LPUSH tutorials mongodb
(integer) 2
redis 127.0.0.1:6379> LPUSH tutorials mysql
(integer) 3
redis 127.0.0.1:6379> LRANGE tutorials 0 10
1) "mysql"
2) "mongodb"
3) "redis"
In the above example, three values are inserted in Redis list named tutorials by the
command LPUSH.
Sr.
Command & Description
No.
52
Redis
LLEN key
6
Gets the length of a list
LPOP key
7
Removes and gets the first element in a list
RPOP key
14
Removes and gets the last element in a list
53
Redis
Return Value
String reply, the value of element stored at the key or nil.
Syntax
Following is the basic syntax of Redis BLPOP command.
Example
redis 127.0.0.1:6379> BLPOP list1 100
The above example will block the client for 100 seconds to execute any command. If any
data comes in the specified key list1, then it returns; otherwise after 100 seconds nil value
is returned.
(nil)
(100.06s)
Return Value
String reply, the value of element stored at the key or nil.
Syntax
Following is the basic syntax of Redis BRPOP command.
Example
redis 127.0.0.1:6379> BRPOP list1 100
The above example will block the client for 100 seconds to execute any command. If any
data comes in the specified key list1, then it returns; otherwise after 100 seconds nil value
is returned.
54
Redis
(nil)
(100.06s)
Return Value
String reply, the value of element stored at the key or nil.
Syntax
Following is the basic syntax of Redis BRPOPLPUSH command.
Example
redis 127.0.0.1:6379> BRPOPLPUSH list1 list2 100
The above example will block the client for 100 seconds to execute any command. If any
data comes in the specified key list1, then it will pop data and push it into another list;
otherwise after 100 seconds nil value is returned.
(nil)
(100.06s)
Return Value
String reply, the requested element, or nil when the index is out of range.
Syntax
Following is the basic syntax of Redis LINDEX command.
55
Redis
Example
redis 127.0.0.1:6379> LPUSH list1 "foo"
(integer) 1
redis 127.0.0.1:6379> LPUSH list1 "bar"
(integer) 2
redis 127.0.0.1:6379> LINDEX list1 0
"foo"
redis 127.0.0.1:6379> LINDEX list1 -1
"bar"
redis 127.0.0.1:6379> LINDEX list1 5
nil
Return Value
Integer reply, the length of the list after the insert operation, or -1 when the value pivot
was not found.
Syntax
Following is the basic syntax of Redis LINSERT command.
Example
redis 127.0.0.1:6379> RPUSH list1 "foo"
(integer) 1
redis 127.0.0.1:6379> RPUSH list1 "bar"
(integer) 2
redis 127.0.0.1:6379> LINSERT list1 BEFORE "bar" "Yes"
(integer) 3
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) "foo"
2) "Yes"
3) "bar"
56
Redis
Return Value
Integer reply, the length of the list at the key.
Syntax
Following is the basic syntax of Redis LLEN command.
Example
redis 127.0.0.1:6379> RPUSH list1 "foo"
(integer) 1
redis 127.0.0.1:6379> RPUSH list1 "bar"
(integer) 2
redis 127.0.0.1:6379> LLEN list1
(integer) 2
Return Value
String reply, the value of the first element, or nil when the key does not exist.
Syntax
Following is the basic syntax of Redis LPOP command.
Example
redis 127.0.0.1:6379> RPUSH list1 "foo"
(integer) 1
redis 127.0.0.1:6379> RPUSH list1 "bar"
(integer) 2
redis 127.0.0.1:6379> LPOP list1
57
Redis
"foo"
Return Value
Integer reply, the length of the list after the push operations.
Syntax
Following is the basic syntax of Redis LPUSH command.
Example
redis 127.0.0.1:6379> LPUSH list1 "foo"
(integer) 1
redis 127.0.0.1:6379> LPUSH list1 "bar"
(integer) 2
redis 127.0.0.1:6379> LRANGE list1 0 -1
1) "foo"
2) "bar"
Return Value
Integer reply, the length of the list after the push operations.
Syntax
Following is the basic syntax of Redis LPUSHX command.
Example
redis 127.0.0.1:6379> LPUSH list1 "foo"
58
Redis
(integer) 1
redis 127.0.0.1:6379> LPUSHX list1 "bar"
(integer) 2
redis 127.0.0.1:6379> LPUSHX list2 "bar"
(integer) 0
redis 127.0.0.1:6379> LRANGE list1 0 -1
1) "foo"
2) "bar"
Return Value
Array reply, list of elements in the specified range.
Syntax
Following is the basic syntax of Redis LRANGE command.
Example
redis 127.0.0.1:6379> LPUSH list1 "foo"
(integer) 1
redis 127.0.0.1:6379> LPUSH list1 "bar"
(integer) 2
redis 127.0.0.1:6379> LPUSHX list1 "bar"
(integer) 0
redis 127.0.0.1:6379> LRANGE list1 0 -1
1) "foo"
2) "bar"
3) "bar"
59
Redis
count > 0: Removes the elements equal to the value moving from the head to tail.
count < 0: Removes the elements equal to the value moving from the tail to head.
count = 0: Removes all elements equal to value.
Return Value
Integer reply, the number of removed elements.
Syntax
Following is the basic syntax of Redis LREM command.
Example
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 2
redis 127.0.0.1:6379> RPUSH mylist "foo"
(integer) 3
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 4
redis 127.0.0.1:6379> LREM mylist -2 "hello"
(integer) 2
Return Value
String reply, OK.
Syntax
Following is the basic syntax of Redis LSET command.
60
Redis
Example
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 2
redis 127.0.0.1:6379> RPUSH mylist "foo"
(integer) 3
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 4
redis 127.0.0.1:6379> LSET mylist 0 "bar"
OK
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1: "bar"
2) "hello"
3) "foo"
4) "hello"
Return Value
String reply, OK.
Syntax
Following is the basic syntax of redis LTRIM command.
61
Redis
Example
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 2
redis 127.0.0.1:6379> RPUSH mylist "foo"
(integer) 3
redis 127.0.0.1:6379> RPUSH mylist "bar"
(integer) 4
redis 127.0.0.1:6379> LTRIM mylist 1 -1
OK
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "foo"
3) "bar"
Return Value
String reply, the value of the last element, or nil when the key does not exist.
Syntax
Following is the basic syntax of Redis RPOP command.
Example
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 2
redis 127.0.0.1:6379> RPUSH mylist "foo"
62
Redis
(integer) 3
(integer) 4
redis 127.0.0.1:6379> RPOP mylist
OK
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "hello"
3) "foo"
Return Value
String reply, the element being popped and pushed.
Syntax
Following is the basic syntax of Redis RPOPLPUSH command.
Example
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
redis 127.0.0.1:6379> RPUSH mylist "foo"
(integer) 2
redis 127.0.0.1:6379> RPUSH mylist "bar"
(integer) 3
redis 127.0.0.1:6379> RPOPLPUSH mylist myotherlist
"bar"
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "foo"
63
Redis
Return Value
Integer reply, the length of the list after the push operation.
Syntax
Following is the basic syntax of Redis RPUSH command.
Example
redis 127.0.0.1:6379> RPUSH mylist "hello"
(integer) 1
redis 127.0.0.1:6379> RPUSH mylist "foo"
(integer) 2
redis 127.0.0.1:6379> RPUSH mylist "bar"
(integer) 3
redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "foo"
3) "bar"
Return Value
Integer reply, the length of the list after the push operation.
Syntax
Following is the basic syntax of Redis RPUSHX command.
Example
64
Redis
65
10. Redis Sets Redis
Redis Sets are an unordered collection of unique strings. Unique means sets does not allow
repition of data in a key.
In Redis set add, remove, and test for the existence of members in O(1) (constant time
regardless of the number of elements contained inside the Set). The maximum length of
a list is 232 - 1 elements (4294967295, more than 4 billion of elements per set).
Example
redis 127.0.0.1:6379> SADD tutorials redis
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mongodb
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mysql
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mysql
(integer) 0
redis 127.0.0.1:6379> SMEMBERS tutorials
1) "mysql"
2) "mongodb"
3) "redis"
In the above example, three values are inserted in Redis set named tutorials by the
command SADD.
Sr.
Command & Description
No.
SCARD key
2
Gets the number of members in a set
66
Redis
SMEMBERS key
8
Gets all the members in a set
SPOP key
10
Removes and returns a random member from a set
Return Value
Integer reply, the number of elements that were added to the set, not including all the
elements already present in the set.
67
Redis
Syntax
Following is the basic syntax of Redis SADD command.
Example
redis 127.0.0.1:6379> SADD myset "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset "foo"
(integer) 1
redis 127.0.0.1:6379> SADD myset "hello"
(integer) 0
redis 127.0.0.1:6379> SMEMBERS myset
1) "hello"
2) "foo"
Return Value
Integer reply, the cardinality (number of elements) of the set, or 0 if the key does not
exist.
Syntax
Following is the basic syntax of Redis SCARD command.
Example
redis 127.0.0.1:6379> SADD myset "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset "foo"
(integer) 1
redis 127.0.0.1:6379> SADD myset "hello"
(integer) 0
redis 127.0.0.1:6379> SCARD myset
(integer) 2
68
Redis
Return Value
Array reply, list with members of the resulting set.
Syntax
Following is the basic syntax of Redis SDIFF command.
Example
redis 127.0.0.1:6379> SADD myset "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset "foo"
(integer) 1
redis 127.0.0.1:6379> SADD myset "bar"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "world"
(integer) 1
redis 127.0.0.1:6379> SDIFF myset myset2
1) "foo"
2) "bar"
Return Value
Integer reply, the number of elements in the resulting set.
Syntax
Following is the basic syntax of Redis SDIFFSTORE command.
69
Redis
Example
redis 127.0.0.1:6379> SADD myset "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset "foo"
(integer) 1
redis 127.0.0.1:6379> SADD myset "bar"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "world"
(integer) 1
redis 127.0.0.1:6379> SDIFFSTORE destset myset myset2
(integer) 2
redis 127.0.0.1:6379> SMEMBERS destset
1) "foo"
2) "bar"
Return Value
Array reply, list with members of the resulting set.
Syntax
Following is the basic syntax of Redis SINTER command.
Example
redis 127.0.0.1:6379> SADD myset "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset "foo"
70
Redis
(integer) 1
redis 127.0.0.1:6379> SADD myset "bar"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "world"
(integer) 1
redis 127.0.0.1:6379> SINTER myset myset2
1) "hello"
Return Value
Integer reply, the number of elements in the resulting set.
Syntax
Following is the basic syntax of Redis SINTERSTORE command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "foo"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "world"
(integer) 1
redis 127.0.0.1:6379> SINTERSTORE myset myset1 myset2
(integer) 1
redis 127.0.0.1:6379> SMEMBERS myset
71
Redis
1) "hello"
Return Value
Integer reply
Syntax
Following is the basic syntax of Redis SISMEMBER command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SISMEMBER myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SISMEMBER myset1 "world"
(integer) 0
Return Value
Integer reply
Syntax
Following is the basic syntax of Redis SISMEMBER command.
72
Redis
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SISMEMBER myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SISMEMBER myset1 "world"
(integer) 0
Return Value
Integer reply.
Syntax
Following is the basic syntax of Redis SMOVE command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "world"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "foo"
(integer) 1
73
Redis
Return Value
String reply, the removed element, or nil when the key does not exist.
Syntax
Following is the basic syntax of Redis SPOP command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "world"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
redis 127.0.0.1:6379> SPOP myset1
"bar"
redis 127.0.0.1:6379> SMEMBERS myset1
1) "Hello"
2) "world"
74
Redis
Return Value
String reply, without the additional count argument. The command returns a Bulk Reply
with the randomly selected element, or nil when the key does not exist. Array reply, when
the additional count argument is passed the command returns an array of elements, or an
empty array when the key does not exist.
Syntax
Following is the basic syntax of Redis SRANDMEMBER command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "world"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
redis 127.0.0.1:6379> SRANDMEMBER myset1
"bar"
redis 127.0.0.1:6379> SRANDMEMBER myset1 2
1) "Hello"
2) "world"
Return Value
Integer reply, the number of members that were removed from the set, not including non-
existing members.
75
Redis
Syntax
Following is the basic syntax of Redis SREM command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "world"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
redis 127.0.0.1:6379> SREM myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SREM myset1 "foo"
(integer) 0
redis 127.0.0.1:6379> SMEMBERS myset1
1) "bar"
2) "world"
Return Value
Array reply, list with members of the resulting set.
Syntax
Following is the basic syntax of Redis SUNION command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "world"
(integer) 1
76
Redis
Return Value
Integer reply, the number of elements in the resulting set.
Syntax
Following is the basic syntax of Redis SUNIONSTORE command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "world"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset2 "bar"
(integer) 1
redis 127.0.0.1:6379> SUNIONSTORE myset myset1 myset2
(integer) 1
77
Redis
Return Value
Array reply.
Syntax
Following is the basic syntax of Redis SSCAN command.
Example
redis 127.0.0.1:6379> SADD myset1 "hello"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "hi"
(integer) 1
redis 127.0.0.1:6379> SADD myset1 "bar"
(integer) 1
redis 127.0.0.1:6379> sscan myset1 0 match h*
1) "0"
2) 1) "hello"
2) "h1"
78
11. Redis Sorted Sets Redis
Redis Sorted Sets are similar to Redis Sets with the unique feature of values stored in a
set. The difference is, every member of a Sorted Set is associated with a score, that is
used in order to take the sorted set ordered, from the smallest to the greatest score.
In Redis sorted set, add, remove, and test for the existence of members in O(1) (constant
time regardless of the number of elements contained inside the set). Maximum length of
a list is 232 - 1 elements (4294967295, more than 4 billion of elements per set).
Example
redis 127.0.0.1:6379> ZADD tutorials 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 3 mysql
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 3 mysql
(integer) 0
redis 127.0.0.1:6379> ZADD tutorials 4 mysql
(integer) 0
redis 127.0.0.1:6379> ZRANGE tutorials 0 10 WITHSCORES
1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"
In the above example, three values are inserted with its score in Redis sorted set named
tutorials by the command ZADD.
79
Redis
Sr.
Command & Description
No.
ZCARD key
2
Gets the number of members in a sorted set
80
Redis
Return Value
Integer reply. The number of elements added to the sorted sets, not including elements
already existing for which the score was updated.
Syntax
Following is the basic syntax of Redis ZADD command.
Example
redis 127.0.0.1:6379> ZADD myset 1 "hello"
(integer) 1
81
Redis
Return Value
Integer reply, the cardinality (number of elements) of the sorted set, or 0 if the key does
not exist.
Syntax
Following is the basic syntax of Redis ZCARD command.
Example
redis 127.0.0.1:6379> ZADD myset 1 "hello"
(integer) 1
redis 127.0.0.1:6379> ZADD myset 1 "foo"
(integer) 1
redis 127.0.0.1:6379> ZADD myset 2 "world" 3 "bar"
(integer) 2
redis 127.0.0.1:6379> ZCARD myzset
(integer) 4
82
Redis
Return Value
Integer reply, the number of elements in the specified score range.
Syntax
Following is the basic syntax of Redis ZCOUNT command.
Example
redis 127.0.0.1:6379> ZADD myset 1 "hello"
(integer) 1
redis 127.0.0.1:6379> ZADD myset 1 "foo"
(integer) 1
redis 127.0.0.1:6379> ZADD myset 2 "world" 3 "bar"
(integer) 2
redis 127.0.0.1:6379> ZCOUNT myzset (1 3
(integer) 2
Return Value
String reply, the new score of the member (a double precision floating point number),
represented as string.
Syntax
Following is the basic syntax of Redis ZINCRBY command.
Example
redis 127.0.0.1:6379> ZADD myset 1 "hello"
83
Redis
(integer) 1
redis 127.0.0.1:6379> ZADD myset 1 "foo"
(integer) 1
redis 127.0.0.1:6379> ZINCRBY myzset 2 "hello"
(integer) 3
redis 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
1) "foo"
2) "2"
3) "hello"
4) "3"
Return Value
Integer reply, the number of elements in the resulting sorted set at the destination.
Syntax
Following is the basic syntax of Redis ZINTERSTORE command.
Example
redis 127.0.0.1:6379> ZADD myset 1 "hello"
(integer) 1
redis 127.0.0.1:6379> ZADD myset 2 "world"
(integer) 1
redis 127.0.0.1:6379> ZADD myset2 1 "hello"
(integer) 1
redis 127.0.0.1:6379> ZADD myset2 2 "world"
(integer) 1
redis 127.0.0.1:6379> ZADD myset2 3 "foo"
(integer) 1
redis 127.0.0.1:6379> ZINTERSTORE out 2 myset1 myset2 WEIGHTS 2 3"
(integer) 3
84
Redis
Return Value
Integer reply, the number of elements in the specified score range.
Syntax
Following is the basic syntax of Redis ZLEXCOUNT command.
Example
redis 127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e
(integer) 5
redis 127.0.0.1:6379> ZADD myzset 0 f 0 g
(integer) 2
redis 127.0.0.1:6379> ZLEXCOUNT myzset - +
(integer) 7
redis 127.0.0.1:6379> ZLEXCOUNT myzset [b [f
(integer) 5
Return Value
Array reply, list of elements in the specified range (optionally with their scores).
85
Redis
Syntax
Following is the basic syntax of Redis ZRANGE command.
Example
redis 127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e
(integer) 5
redis 127.0.0.1:6379> ZADD myzset 0 f 0 g
(integer) 2
redis 127.0.0.1:6379> ZRANGE myzset 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
7) "g"
redis 127.0.0.1:6379> ZLEXCOUNT myzset [b [f
(integer) 5
Return Value
Array reply, list of elements in the specified score range.
Syntax
Following is the basic syntax of Redis ZRANGEBYLEX command.
86
Redis
Example
redis 127.0.0.1:6379> ZADD myzset 0 a 0 b 0 c 0 d 0 e
(integer) 5
redis 127.0.0.1:6379> ZADD myzset 0 f 0 g
(integer) 2
redis 127.0.0.1:6379> ZRANGEBYLEX myzset - [c
1) "a"
2) "b"
3) "c"
redis 127.0.0.1:6379> ZRANGEBYLEX myzset - (c
1) "a"
2) "b"
Return Value
Array reply, list of elements in the specified score range (optionally with their scores).
Syntax
Following is the basic syntax of Redis ZRANGEBYSCORE command.
Example
redis 127.0.0.1:6379> ZADD myzset 0 a 1 b 2 c 3 d 4 e
(integer) 5
redis 127.0.0.1:6379> ZADD myzset 5 f 6 g
(integer) 2
redis 127.0.0.1:6379> ZRANGEBYSCORE myzset 1 2
1) "b"
2) "c"
redis 127.0.0.1:6379> ZRANGEBYSCORE myzset (1 2
87
Redis
1) "b"
Return Value
If the member exists in the sorted set, Integer reply: the rank of member.
If the member does not exist in the sorted set or the key does not exist, Bulk string
reply: nil.
Syntax
Following is the basic syntax of Redis ZRANK command.
Example
redis 127.0.0.1:6379> ZADD myzset 0 a 1 b 2 c 3 d 4 e
(integer) 5
redis 127.0.0.1:6379> ZADD myzset 5 f 6 g
(integer) 2
redis 127.0.0.1:6379> ZRANK myzset b
(integer) 1
redis 127.0.0.1:6379> ZRANK myzset t
nil
Return Value
Integer, number of members removed from the sorted set, not including non-existing
members.
Syntax
Following is the basic syntax of Redis ZREM command.
88
Redis
Example
redis 127.0.0.1:6379> ZADD myzset 0 a 1 b 2 c 3 d 4 e
(integer) 5
redis 127.0.0.1:6379> ZREM myzset b
(integer) 1
redis 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
1) "a"
2) "0"
3) "c"
4) "2"
5) "d"
6) "3"
7) "e"
8) "4"
Return Value
Integer reply, the number of elements removed.
Syntax
Following is the basic syntax of Redis ZREMRANGEBYLEX command.
Example
redis 127.0.0.1:6379> ZADD myzset 0 a 1 b 2 c 3 d 4 e
(integer) 5
redis 127.0.0.1:6379> ZREMRANGEBYLEX myzset 0 -1
(integer) 1
redis 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
89
Redis
Return Value
Integer reply, the number of elements removed.
Syntax
Following is the basic syntax of Redis ZREMRANGEBYRANK command.
Example
redis 127.0.0.1:6379> ZADD myzset 1 b 2 c 3 d 4 e
(integer) 4
redis 127.0.0.1:6379> ZREMRANGEBYRANK myzset 0 3
(integer) 3
redis 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
1) "e"
2) "4"
Return Value
Integer reply, the number of elements removed.
Syntax
Following is the basic syntax of Redis ZREMRANGEBYSCORE command.
90
Redis
Example
redis 127.0.0.1:6379> ZADD myzset 1 b 2 c 3 d 4 e
(integer) 4
redis 127.0.0.1:6379> ZREMRANGEBYSCORE myzset -inf (2
(integer) 1
redis 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
1) "b"
2) "2"
3) "c"
4) "3"
5) "d"
6) "4"
7) "e"
8) "5"
Return Value
Array reply, list of elements in the specified range (optionally with their scores).
Syntax
Following is the basic syntax of Redis ZREVRANGE command.
Example
redis 127.0.0.1:6379> ZADD myzset 1 b 2 c 3 d 4 e
(integer) 4
redis 127.0.0.1:6379> ZREVRANGE myzset 0 -1
1) "e"
2) "d"
3) "c"
4) "b"
91
Redis
Return Value
Array reply, list of elements in the specified score range (optionally with their scores).
Syntax
Following is the basic syntax of Redis ZREVRANGEBYSCORE command.
Example
redis 127.0.0.1:6379> ZADD myzset 1 b 2 c 3 d 4 e
(integer) 4
redis 127.0.0.1:6379> ZREVRANGEBYSCORE myzset +inf -inf
1) "e"
2) "d"
3) "c"
4) "b"
redis 127.0.0.1:6379> ZREVRANGEBYSCORE myzset 2 1
1) "c"
2) "b"
Return Value
If the member exists in the sorted set, Integer reply: the rank of member.
If the member does not exist in the sorted set or the key does not exist, Bulk string
reply: nil.
Syntax
Following is the basic syntax of Redis ZREVRANK command.
92
Redis
Example
redis 127.0.0.1:6379> ZADD myzset 1 b 2 c 3 d 4 e
(integer) 4
redis 127.0.0.1:6379> ZREVRANK myzset "c"
(integer) 3
redis 127.0.0.1:6379> ZREVRANK myzset "y"
(nil)
Return Value
Bulk string reply, the score of member (a double precision floating point number),
represented as string.
Syntax
Following is the basic syntax of Redis ZSCORE command.
Example
redis 127.0.0.1:6379> ZADD myzset 1 b 2 c 3 d 4 e
(integer) 4
redis 127.0.0.1:6379> ZSCORE myzset "c"
(integer) 3
redis 127.0.0.1:6379> ZSCORE myzset "y"
(nil)
93
Redis
Return Value
Integer reply, the number of elements in the resulting sorted set at the destination.
Syntax
Following is the basic syntax of Redis ZUNIONSTORE command.
Example
redis 127.0.0.1:6379> ZADD myzset1 1 b 2 c
(integer) 2
redis 127.0.0.1:6379> ZADD myzset2 1 b 2 c 3 d
(integer) 3
redis 127.0.0.1:6379> ZUNIONSTORE out 2 myzset1 myzset2 WEIGHTS 2 3
(integer) 3
redis 127.0.0.1:6379> ZRANGE out 0 -1 WITHSCORES
1) "b"
2) "5"
3) "c"
4) "9"
5) "d"
6) "10"
Return Value
Array reply.
Syntax
Following is the basic syntax of Redis ZSCAN command.
94
12. Redis HyperLogLog Redis
HyperLogLog provides a very good approximation of the cardinality of a set even using a
very small amount of memory around 12 kbytes per key with a standard error of 0.81%.
There is no limit to the number of items you can count, unless you approach 2 64 items.
Example
Following example explains how Redis HyperLogLog works.
1) (integer) 1
1) (integer) 1
1) (integer) 1
(integer) 3
Sr.
Command & Description
No.
95
Redis
Return Value
Integer reply, 1 or 0.
Syntax
Following is the basic syntax of Redis PFADD command.
Example
redis 127.0.0.1:6379> PFADD mykey a b c d e f g h i j
(integer) 1
redis 127.0.0.1:6379> PFCOUNT mykey
(integer) 10
Return Value
Integer reply, approximated number of unique elements.
When PFCOUNT command is used with multiple keys, then it returns approximated
cardinality of the union of the HyperLogLogs.
Syntax
Following is the basic syntax of Redis PFCOUNT command.
Example
redis 127.0.0.1:6379> PFADD mykey a b c d e f g h i j
96
Redis
(integer) 1
redis 127.0.0.1:6379> PFCOUNT mykey
(integer) 10
redis 127.0.0.1:6379> PFCOUNT mykey
(integer) 10
redis 127.0.0.1:6379> PFCOUNT mykey mynewkey
(integer) 10
Return Value
Simple string reply OK.
Syntax
Following is the basic syntax of Redis PFMERGE command.
Example
redis 127.0.0.1:6379> PFADD hll1 foo bar zap a
(integer) 1
redis 127.0.0.1:6379> PFADD hll2 a b c foo
(integer) 1
redis 127.0.0.1:6379> PFMERGE hll3 hll1 hll2
OK
redis 127.0.0.1:6379> PFCOUNT hll3
(integer) 6
97
13. Redis Publish Subscribe Redis
Redis Pub/Sub implements the messaging system where the senders (in redis terminology
called publishers) sends the messages while the receivers (subscribers) receive them. The
link by which the messages are transferred is called channel.
Example
Following example explains how publish subscriber concept works. In the following
example, one client subscribes a channel named redisChat.
Now, two clients are publishing the messages on the same channel named redisChat and
the above subscribed client is receiving messages.
(integer) 1
(integer) 1
1) "message"
2) "redisChat"
3) "Redis is a great caching technique"
1) "message"
2) "redisChat"
3) "Learn redis by tutorials point"
98
Redis
Sr.
Command & Description
No.
Syntax
Following is the basic syntax of Redis PSUBSCRIBE command.
Example
redis 127.0.0.1:6379> PSUBSCRIBE mychannel
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
99
Redis
2) "mychannel"
3) (integer) 1
Syntax
Following is the basic syntax of Redis PUBSUB command.
Return Value
Array reply, a list of active channels.
Example
redis 127.0.0.1:6379> PUBSUB CHANNELS
(empty list or set)
Syntax
Following is the basic syntax of Redis PUBLISH command.
Return Value
Integer reply: the number of clients that received the message.
Example
redis 127.0.0.1:6379> PUBLISH mychannel "hello, i m here"
(integer) 1
Syntax
Following is the basic syntax of Redis PUNSUBSCRIBE command.
Return Value
Array reply.
Example
redis 127.0.0.1:6379> PUNSUBSCRIBE mychannel
1) "punsubscribe"
2) "a"
3) (integer) 1
Syntax
Following is the basic syntax of Redis SUBSCRIBE command.
Return Value
Array reply.
Example
redis 127.0.0.1:6379> SUBSCRIBE mychannel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mychannel"
3) (integer) 1
1) "message"
2) "mychannel"
3) "a"
101
Redis
Syntax
Following is the basic syntax of Redis UNSUBSCRIBE command.
Return Value
Array reply.
Example
redis 127.0.0.1:6379> UNSUBSCRIBE mychannel
1) "unsubscribe"
2) "a"
3) (integer) 0
102
14. Redis Transactions Redis
Redis transactions allow the execution of a group of commands in a single step. Following
are the two properties of Transactions.
Redis transaction is also atomic. Atomic means either all of the commands or none
are processed.
Sample
Redis transaction is initiated by command MULTI and then you need to pass a list of
commands that should be executed in the transaction, after which the entire transaction
is executed by EXEC command.
Example
Following example explains how Redis transaction can be initiated and executed.
1) OK
2) "redis"
3) (integer) 1
103
Redis
Sr.
Command & Description
No.
DISCARD
1
Discards all commands issued after MULTI
EXEC
2
Executes all commands issued after MULTI
MULTI
3
Marks the start of a transaction block
UNWATCH
4
Forgets about all watched keys
Return Value
Array reply, each element being the reply to each of the commands in the atomic
transaction.
Syntax
Following is the basic syntax of Redis EXEC command.
104
Redis
Return Value
Simple string reply: always OK.
Syntax
Following is the basic syntax of Redis MULTI command.
Return Value
Simple string reply: always OK.
Syntax
Following is the basic syntax of Redis UNWATCH command.
Return Value
Simple string reply: always OK.
Syntax
Following is the basic syntax of Redis WATCH command.
105
15. Redis Scripting Redis
Redis scripting is used to evaluate scripts using the Lua interpreter. It is built into Redis
starting from version 2.6.0. The command used for scripting is EVALcommand.
Syntax
Following is the basic syntax of EVAL command.
redis 127.0.0.1:6379> EVAL script numkeys key [key ...] arg [arg ...]
Example
Following example explains how Redis scripting works.
1) "key1"
2) "key2"
3) "first"
4) "second"
Sr.
Command & Description
No.
SCRIPT FLUSH
4
Removes all the scripts from the script cache.
106
Redis
SCRIPT KILL
5
Kills the script currently in execution.
Syntax
Following is the basic syntax of Redis EVAL command.
redis 127.0.0.1:6379> EVAL script numkeys key [key ...] arg [arg ...]
Example
redis 127.0.0.1:6379> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1
key2 first second
1) "key1"
2) "key2"
3) "first"
4) "second"
Syntax
Following is the basic syntax of Redis EVALSHA command.
redis 127.0.0.1:6379> EVALSHA sha1 numkeys key [key ...] arg [arg ...]
Example
107
Redis
Syntax
Following is the basic syntax of Redis SCRIPT EXISTS command.
Return Value
Array reply - The command returns an array of integers that corresponds to the specified
SHA1 digest arguments. For every corresponding SHA1 digest of a script that actually
exists in the script cache, 1 is returned, otherwise 0 is returned.
Example
redis 127.0.0.1:6379> SCRIPT LOAD "return 1"
ERR Unknown or disabled command 'SCRIPT'
redis 127.0.0.1:6379> SCRIPT EXISTS
ff9d4800c877a703b823dsdsfsffewfwefwefweac0578ff8db
ERR Unknown or disabled command 'SCRIPT'
Syntax
Following is the basic syntax of Redis SCRIPT FLUSH command.
108
Redis
Return Value
Simple string reply.
Example
redis 127.0.0.1:6379> SCRIPT FLUSH
OK
Syntax
Following is the basic syntax of Redis SCRIPT KILL command.
Return Value
Simple string reply.
Example
redis 127.0.0.1:6379> SCRIPT KILL
OK
Syntax
Following is the basic syntax of Redis SCRIPT LOAD command.
109
Redis
Return Value
Bulk string reply - This command returns the SHA1 digest of the script added into the
script cache.
Example
redis 127.0.0.1:6379> SCRIPT LOAD "return 1"
"e0e1f9fabfc9d4800c877a703b823ac0578ff8db"
110
16. Redis Connections Redis
Redis connection commands are basically used to manage client connections with Redis
server.
Example
Following example explains how a client authenticates itself to Redis server and checks
whether the server is running or not.
Sr.
Command & Description
No.
AUTH password
1
Authenticates to the server with the given password
ECHO message
2
Prints the given string
PING
3
Checks whether the server is running or not
QUIT
4
Closes the current connection
SELECT index
5
Changes the selected database for the current connection
111
Redis
Return Value
String reply.
Syntax
Following is the basic syntax of Redis AUTH command.
Example
redis 127.0.0.1:6379> AUTH PASSWORD
(error) ERR Client sent AUTH, but no password is set
redis 127.0.0.1:6379> CONFIG SET requirepass "mypass"
OK
redis 127.0.0.1:6379> AUTH mypass
Ok
Return Value
String reply.
Syntax
Following is the basic syntax of Redis ECHO command.
Example
redis 127.0.0.1:6379> ECHO "Hello World"
"Hello World"
Return Value
String reply.
112
Redis
Syntax
Following is the basic syntax of Redis PING command.
Example
redis 127.0.0.1:6379> PING
PONG
Return Value
String reply OK.
Syntax
Following is the basic syntax of Redis QUIT command.
Example
redis 127.0.0.1:6379> QUIT
OK
Return Value
String reply.
Syntax
Following is the basic syntax of Redis SELECT command.
113
Redis
Example
redis 127.0.0.1:6379> SELECT 1
OK
redis 127.0.0.1:6379[1]>
114
17. Redis Server Redis
Example
Following example explains how we can get all statistics and information about the server.
# Server
redis_version:2.8.13
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:c2238b38b1edb0e2
redis_mode:standalone
os:Linux 3.5.0-48-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.7.2
process_id:3856
run_id:0e61abd297771de3fe812a3c21027732ac9f41fe
tcp_port:6379
uptime_in_seconds:11554
uptime_in_days:0
hz:10
lru_clock:16651447
config_file:
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
115
Redis
used_memory:589016
used_memory_human:575.21K
used_memory_rss:2461696
used_memory_peak:667312
used_memory_peak_human:651.67K
used_memory_lua:33792
mem_fragmentation_ratio:4.18
mem_allocator:jemalloc-3.6.0
# Persistence
loading:0
rdb_changes_since_last_save:3
rdb_bgsave_in_progress:0
rdb_last_save_time:1409158561
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
# Stats
total_connections_received:24
total_commands_processed:294
instantaneous_ops_per_sec:0
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:41
116
Redis
keyspace_misses:82
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:264
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:10.49
used_cpu_user:4.96
used_cpu_sys_children:0.00
used_cpu_user_children:0.01
# Keyspace
db0:keys=94,expires=1,avg_ttl=41638810
db1:keys=1,expires=0,avg_ttl=0
db3:keys=1,expires=0,avg_ttl=0
117
Redis
Sr.
Command & Description
No.
BGREWRITEAOF
1
Asynchronously rewrites the append-only file
BGSAVE
2
Asynchronously saves the dataset to the disk
CLIENT LIST
4
Gets the list of client connections to the server
CLIENT GETNAME
5
Gets the name of the current connection
CLUSTER SLOTS
8
Gets an array of Cluster slot to node mappings
COMMAND
9
Gets an array of Redis command details
COMMAND COUNT
10
Gets total number of Redis commands
COMMAND GETKEYS
11
Extracts the keys given a full Redis command
BGSAVE
12
Asynchronously saves the dataset to the disk
118
Redis
CONFIG REWRITE
15
Rewrites the configuration file with the in-memory configuration
CONFIG RESETSTAT
17
Resets the stats returned by INFO
DBSIZE
18
Returns the number of keys in the selected database
DEBUG SEGFAULT
20
Makes the server crash
FLUSHALL
21
Removes all the keys from all databases
FLUSHDB
22
Removes all the keys from the current database
INFO [section]
23
Gets information and statistics about the server
LASTSAVE
24
Gets the UNIX time stamp of the last successful save to the disk
MONITOR
25
Listens for all the requests received by the server in real time
ROLE
26
Returns the role of the instance in the context of replication
SAVE
27
Synchronously saves the dataset to the disk
119
Redis
SYNC
31
Command used for replication
TIME
32
Returns the current server time
Return Value
Simple string reply: always OK.
Syntax
Following is the basic syntax of Redis BGREWRITEAOF command.
Return Value
Simple string reply.
Syntax
Following is the basic syntax of Redis BGSAVE command.
120
Redis
Return Value
Simple string reply: OK if the connection exists and has been closed.
Syntax
Following is the basic syntax of Redis CLIENT KILL command.
With Redis 2.8.12 or greater, the command can be run with multiple options as shown
below:
CLIENT KILL ADDR ip:port. This is exactly the same as the old three-arguments
behavior.
CLIENT KILL ID client-id. Allows to kill a client by its unique ID field, which was
introduced in the CLIENT LIST command starting from Redis 2.8.12.
CLIENT KILL TYPE type, where type is one of normal, slave, pubsub. This closes
the connections of all the clients in the specified class. Note: Clients blocked into
the MONITOR command are considered to belong to the normal class.
CLIENT KILL SKIPME yes/no. By default, this option is set to yes, that is, the client
calling the command will not get killed. However, setting this option to no will have
the effect of also killing the client calling the command.
Return Value
Bulk string reply, a unique string.
Syntax
Following is the basic syntax of Redis CLIENT LIST command.
Description of Fields
id: Unique 64-bit client ID (introduced in Redis 2.8.12)
addr: Address/port of the client
fd: File descriptor corresponding to the socket
121
Redis
Return Value
Bulk string reply: The connection name, or a null bulk reply, if no name is set.
Syntax
Following is the basic syntax of Redis CLIENT GETNAME command.
It stops processing all the pending commands from normal and pub/sub clients.
However, interactions with slaves will continue normally.
It returns OK to the caller ASAP, so the CLIENT PAUSE command execution is not
paused by itself.
When the specified amount of time has elapsed, all the clients are unblocked: this
will trigger the processing of all the commands accumulated in the query buffer of
every client during the pause.
122
Redis
Return Value
Simple string reply: The command returns OK or an error if the timeout is invalid.
Syntax
Following is the basic syntax of Redis CLIENT PAUSE command.
Return Value
Simple string reply, OK if the connection name was successfully set.
Syntax
Following is the basic syntax of Redis CLIENT SETNAME command.
Example
redis 127.0.0.1:6379> CLIENT SETNAME "my connection"
OK
Return Value
Array reply - Nested list of slot ranges with IP/Port mappings.
Syntax
Following is the basic syntax of Redis CLUSTER SLOTS command.
Sample Reply
redis 127.0.0.1:6379> CLIENT SETNAME "my connection"
1) 1) (integer) 0
123
Redis
2) (integer) 4095
3) 1) "127.0.0.1"
2) (integer) 7000
4) 1) "127.0.0.1"
2) (integer) 7004
2) 1) (integer) 12288
2) (integer) 16383
3) 1) "127.0.0.1"
2) (integer) 7003
4) 1) "127.0.0.1"
2) (integer) 7007
3) 1) (integer) 4096
2) (integer) 8191
3) 1) "127.0.0.1"
2) (integer) 7001
4) 1) "127.0.0.1"
2) (integer) 7005
4) 1) (integer) 8192
2) (integer) 12287
3) 1) "127.0.0.1"
2) (integer) 7002
4) 1) "127.0.0.1"
2) (integer) 7006
Server Command
Redis COMMAND array replies the details about all Redis commands.
Return Value
Array reply - Nested list of command details. Commands are returned in a random order.
Syntax
Following is the basic syntax of Redis COMMAND command.
124
Redis
Example
redis 127.0.0.1:6379> COMMAND
1) 1) "pfcount"
2) (integer) -2
3) 1) write
4) (integer) 1
5) (integer) 1
6) (integer) 1
2) 1) "command"
2) (integer) 0
3) 1) readonly
2) loading
3) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
3) 1) "zscan"
2) (integer) -3
3) 1) readonly
2) random
4) (integer) 1
5) (integer) 1
6) (integer) 1
4) 1) "echo"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
5) 1) "select"
2) (integer) 2
3) 1) readonly
2) loading
3) fast
4) (integer) 0
125
Redis
5) (integer) 0
6) (integer) 0
6) 1) "zcount"
2) (integer) 4
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
7) 1) "substr"
2) (integer) 4
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
8) 1) "pttl"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
9) 1) "hincrbyfloat"
2) (integer) 4
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
10) 1) "hlen"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
126
Redis
6) (integer) 1
11) 1) "incrby"
2) (integer) 3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
12) 1) "setex"
2) (integer) 4
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
13) 1) "persist"
2) (integer) 2
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
14) 1) "setbit"
2) (integer) 4
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
15) 1) "info"
2) (integer) -1
3) 1) readonly
2) loading
3) stale
4) (integer) 0
127
Redis
5) (integer) 0
6) (integer) 0
16) 1) "scard"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
17) 1) "srandmember"
2) (integer) -2
3) 1) readonly
2) random
4) (integer) 1
5) (integer) 1
6) (integer) 1
18) 1) "lrem"
2) (integer) 4
3) 1) write
4) (integer) 1
5) (integer) 1
6) (integer) 1
19) 1) "append"
2) (integer) 3
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
20) 1) "hgetall"
2) (integer) 2
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
21) 1) "zincrby"
128
Redis
2) (integer) 4
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
22) 1) "rpop"
2) (integer) 2
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
23) 1) "cluster"
2) (integer) -2
3) 1) readonly
2) admin
4) (integer) 0
5) (integer) 0
6) (integer) 0
24) 1) "ltrim"
2) (integer) 4
3) 1) write
4) (integer) 1
5) (integer) 1
6) (integer) 1
25) 1) "flushdb"
2) (integer) 1
3) 1) write
4) (integer) 0
5) (integer) 0
6) (integer) 0
26) 1) "rpoplpush"
2) (integer) 3
3) 1) write
129
Redis
2) denyoom
4) (integer) 1
5) (integer) 2
6) (integer) 1
27) 1) "expire"
2) (integer) 3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
28) 1) "psync"
2) (integer) 3
3) 1) readonly
2) admin
3) noscript
4) (integer) 0
5) (integer) 0
6) (integer) 0
29) 1) "zremrangebylex"
2) (integer) 4
3) 1) write
4) (integer) 1
5) (integer) 1
6) (integer) 1
30) 1) "pubsub"
2) (integer) -2
3) 1) readonly
2) pubsub
3) random
4) loading
5) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
130
Redis
31) 1) "setnx"
2) (integer) 3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
32) 1) "pexpireat"
2) (integer) 3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
33) 1) "psubscribe"
2) (integer) -2
3) 1) readonly
2) pubsub
3) noscript
4) loading
5) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
34) 1) "zrevrange"
2) (integer) -4
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
35) 1) "hmget"
2) (integer) -3
3) 1) readonly
4) (integer) 1
5) (integer) 1
131
Redis
6) (integer) 1
36) 1) "object"
2) (integer) -2
3) 1) readonly
4) (integer) 2
5) (integer) 2
6) (integer) 2
37) 1) "watch"
2) (integer) -2
3) 1) readonly
2) noscript
3) fast
4) (integer) 1
5) (integer) -1
6) (integer) 1
38) 1) "setrange"
2) (integer) 4
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
39) 1) "sdiffstore"
2) (integer) -3
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) -1
6) (integer) 1
40) 1) "flushall"
2) (integer) 1
3) 1) write
4) (integer) 0
5) (integer) 0
6) (integer) 0
41) 1) "sadd"
132
Redis
2) (integer) -3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
42) 1) "renamenx"
2) (integer) 3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 2
6) (integer) 1
43) 1) "zrangebyscore"
2) (integer) -4
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
44) 1) "bitop"
2) (integer) -4
3) 1) write
2) denyoom
4) (integer) 2
5) (integer) -1
6) (integer) 1
45) 1) "get"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
46) 1) "hmset"
2) (integer) -4
133
Redis
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
47) 1) "type"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
48) 1) "evalsha"
2) (integer) -3
3) 1) noscript
2) movablekeys
4) (integer) 0
5) (integer) 0
6) (integer) 0
49) 1) "zrevrangebyscore"
2) (integer) -4
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
50) 1) "set"
2) (integer) -3
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
51) 1) "getset"
2) (integer) 3
3) 1) write
2) denyoom
134
Redis
4) (integer) 1
5) (integer) 1
6) (integer) 1
52) 1) "punsubscribe"
2) (integer) -1
3) 1) readonly
2) pubsub
3) noscript
4) loading
5) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
53) 1) "publish"
2) (integer) 3
3) 1) readonly
2) pubsub
3) loading
4) stale
5) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
54) 1) "lset"
2) (integer) 4
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
55) 1) "rename"
2) (integer) 3
3) 1) write
4) (integer) 1
5) (integer) 2
135
Redis
6) (integer) 1
56) 1) "bgsave"
2) (integer) 1
3) 1) readonly
2) admin
4) (integer) 0
5) (integer) 0
6) (integer) 0
57) 1) "decrby"
2) (integer) 3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
58) 1) "sunion"
2) (integer) -2
3) 1) readonly
2) sort_for_script
4) (integer) 1
5) (integer) -1
6) (integer) 1
59) 1) "blpop"
2) (integer) -3
3) 1) write
2) noscript
4) (integer) 1
5) (integer) -2
6) (integer) 1
60) 1) "zrem"
2) (integer) -3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
136
Redis
6) (integer) 1
61) 1) "readonly"
2) (integer) 1
3) 1) readonly
2) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
62) 1) "exists"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
63) 1) "linsert"
2) (integer) 5
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
64) 1) "lindex"
2) (integer) 3
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
65) 1) "scan"
2) (integer) -2
3) 1) readonly
2) random
4) (integer) 0
5) (integer) 0
6) (integer) 0
66) 1) "migrate"
137
Redis
2) (integer) -6
3) 1) write
2) admin
4) (integer) 0
5) (integer) 0
6) (integer) 0
67) 1) "ping"
2) (integer) 1
3) 1) readonly
2) stale
3) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
68) 1) "zunionstore"
2) (integer) -4
3) 1) write
2) denyoom
3) movablekeys
4) (integer) 0
5) (integer) 0
6) (integer) 0
69) 1) "latency"
2) (integer) -2
3) 1) readonly
2) admin
3) noscript
4) loading
5) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
70) 1) "role"
2) (integer) 1
3) 1) admin
2) noscript
138
Redis
3) loading
4) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
71) 1) "ttl"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
72) 1) "del"
2) (integer) -2
3) 1) write
4) (integer) 1
5) (integer) -1
6) (integer) 1
73) 1) "wait"
2) (integer) 3
3) 1) readonly
2) noscript
4) (integer) 0
5) (integer) 0
6) (integer) 0
74) 1) "zscore"
2) (integer) 3
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
75) 1) "zrevrangebylex"
2) (integer) -4
3) 1) readonly
4) (integer) 1
139
Redis
5) (integer) 1
6) (integer) 1
76) 1) "sscan"
2) (integer) -3
3) 1) readonly
2) random
4) (integer) 1
5) (integer) 1
6) (integer) 1
140
Redis
77) 1) "incrbyfloat"
2) (integer) 3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
78) 1) "decr"
2) (integer) 2
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
79) 1) "getbit"
2) (integer) 3
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
80) 1) "spop"
2) (integer) 2
3) 1) write
2) noscript
3) random
4) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
141
Redis
81) 1) "hkeys"
2) (integer) 2
3) 1) readonly
2) sort_for_script
4) (integer) 1
5) (integer) 1
6) (integer) 1
82) 1) "pfmerge"
2) (integer) -2
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) -1
6) (integer) 1
83) 1) "zrange"
2) (integer) -4
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
84) 1) "monitor"
2) (integer) 1
3) 1) readonly
2) admin
3) noscript
4) (integer) 0
5) (integer) 0
6) (integer) 0
85) 1) "zinterstore"
2) (integer) -4
3) 1) write
2) denyoom
3) movablekeys
142
Redis
4) (integer) 0
5) (integer) 0
6) (integer) 0
86) 1) "rpushx"
2) (integer) 3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
87) 1) "llen"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
88) 1) "hincrby"
2) (integer) 4
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
89) 1) "save"
2) (integer) 1
3) 1) readonly
2) admin
3) noscript
4) (integer) 0
5) (integer) 0
6) (integer) 0
143
Redis
90) 1) "zremrangebyrank"
2) (integer) 4
3) 1) write
4) (integer) 1
5) (integer) 1
6) (integer) 1
91) 1) "auth"
2) (integer) 2
3) 1) readonly
2) noscript
3) loading
4) stale
5) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
92) 1) "zcard"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
93) 1) "psetex"
2) (integer) 4
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) 1
6) (integer) 1
144
Redis
94) 1) "shutdown"
2) (integer) -1
3) 1) readonly
2) admin
3) loading
4) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
95) 1) "sync"
2) (integer) 1
3) 1) readonly
2) admin
3) noscript
4) (integer) 0
5) (integer) 0
6) (integer) 0
96) 1) "dbsize"
2) (integer) 1
3) 1) readonly
2) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
97) 1) "expireat"
2) (integer) 3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
145
Redis
98) 1) "subscribe"
2) (integer) -2
3) 1) readonly
2) pubsub
3) noscript
4) loading
5) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
99) 1) "brpop"
2) (integer) -3
3) 1) write
2) noscript
4) (integer) 1
5) (integer) 1
6) (integer) 1
100) 1) "sort"
2) (integer) -2
3) 1) write
2) denyoom
3) movablekeys
4) (integer) 1
5) (integer) 1
6) (integer) 1
101) 1) "sunionstore"
2) (integer) -3
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) -1
6) (integer) 1
146
Redis
102) 1) "zrangebylex"
2) (integer) -4
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
103) 1) "zlexcount"
2) (integer) 4
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
104) 1) "lpush"
2) (integer) -3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
105) 1) "incr"
2) (integer) 2
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
106) 1) "mget"
2) (integer) -2
3) 1) readonly
4) (integer) 1
5) (integer) -1
147
Redis
6) (integer) 1
107) 1) "getrange"
2) (integer) 4
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
108) 1) "slaveof"
2) (integer) 3
3) 1) admin
2) noscript
3) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
109) 1) "bitpos"
2) (integer) -3
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
110) 1) "rpush"
2) (integer) -3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
148
Redis
111) 1) "config"
2) (integer) -2
3) 1) readonly
2) admin
3) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
112) 1) "srem"
2) (integer) -3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
113) 1) "mset"
2) (integer) -3
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) -1
6) (integer) 2
114) 1) "lrange"
2) (integer) 4
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
115) 1) "replconf"
2) (integer) -1
3) 1) readonly
2) admin
3) noscript
4) loading
5) stale
149
Redis
4) (integer) 0
5) (integer) 0
6) (integer) 0
116) 1) "hsetnx"
2) (integer) 4
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
117) 1) "discard"
2) (integer) 1
3) 1) readonly
2) noscript
3) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
118) 1) "pexpire"
2) (integer) 3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
119) 1) "pfdebug"
2) (integer) -3
3) 1) write
4) (integer) 0
5) (integer) 0
6) (integer) 0
120) 1) "asking"
2) (integer) 1
3) 1) readonly
4) (integer) 0
150
Redis
5) (integer) 0
6) (integer) 0
121) 1) "client"
2) (integer) -2
3) 1) readonly
2) admin
4) (integer) 0
5) (integer) 0
6) (integer) 0
122) 1) "pfselftest"
2) (integer) 1
3) 1) readonly
4) (integer) 0
5) (integer) 0
6) (integer) 0
123) 1) "bgrewriteaof"
2) (integer) 1
3) 1) readonly
2) admin
4) (integer) 0
5) (integer) 0
6) (integer) 0
124) 1) "zremrangebyscore"
2) (integer) 4
3) 1) write
4) (integer) 1
5) (integer) 1
6) (integer) 1
125) 1) "sinterstore"
2) (integer) -3
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) -1
6) (integer) 1
126) 1) "lpushx"
151
Redis
2) (integer) 3
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
127) 1) "restore"
2) (integer) -4
3) 1) write
2) denyoom
3) admin
4) (integer) 1
5) (integer) 1
6) (integer) 1
128) 1) "unsubscribe"
2) (integer) -1
3) 1) readonly
2) pubsub
3) noscript
4) loading
5) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
152
Redis
129) 1) "zrank"
2) (integer) 3
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
130) 1) "readwrite"
2) (integer) 1
3) 1) readonly
2) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
131) 1) "hget"
2) (integer) 3
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
132) 1) "bitcount"
2) (integer) -2
3) 1) readonly
4) (integer) 1
5) (integer) 1
6) (integer) 1
133) 1) "randomkey"
2) (integer) 1
3) 1) readonly
2) random
4) (integer) 0
5) (integer) 0
6) (integer) 0
153
Redis
134) 1) "restore-asking"
2) (integer) -4
3) 1) write
2) denyoom
3) admin
4) asking
4) (integer) 1
5) (integer) 1
6) (integer) 1
135) 1) "time"
2) (integer) 1
3) 1) readonly
2) random
3) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
136) 1) "zrevrank"
2) (integer) 3
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
137) 1) "hset"
2) (integer) 4
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
138) 1) "sinter"
2) (integer) -2
3) 1) readonly
2) sort_for_script
154
Redis
4) (integer) 1
5) (integer) -1
6) (integer) 1
139) 1) "dump"
2) (integer) 2
3) 1) readonly
2) admin
4) (integer) 1
5) (integer) 1
6) (integer) 1
140) 1) "move"
2) (integer) 3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
141) 1) "strlen"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
142) 1) "unwatch"
2) (integer) 1
3) 1) readonly
2) noscript
3) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
143) 1) "lpop"
2) (integer) 2
3) 1) write
2) fast
155
Redis
4) (integer) 1
5) (integer) 1
6) (integer) 1
144) 1) "smembers"
2) (integer) 2
3) 1) readonly
2) sort_for_script
4) (integer) 1
5) (integer) 1
6) (integer) 1
145) 1) "msetnx"
2) (integer) -3
3) 1) write
2) denyoom
4) (integer) 1
5) (integer) -1
6) (integer) 2
146) 1) "pfadd"
2) (integer) -2
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
147) 1) "zadd"
2) (integer) -4
3) 1) write
2) denyoom
3) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
148) 1) "lastsave"
2) (integer) 1
3) 1) readonly
156
Redis
2) random
3) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
149) 1) "exec"
2) (integer) 1
3) 1) noscript
2) skip_monitor
4) (integer) 0
5) (integer) 0
6) (integer) 0
150) 1) "sismember"
2) (integer) 3
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
151) 1) "debug"
2) (integer) -2
3) 1) admin
2) noscript
4) (integer) 0
5) (integer) 0
6) (integer) 0
152) 1) "slowlog"
2) (integer) -2
3) 1) readonly
4) (integer) 0
5) (integer) 0
6) (integer) 0
153) 1) "hexists"
2) (integer) 3
3) 1) readonly
2) fast
157
Redis
4) (integer) 1
5) (integer) 1
6) (integer) 1
154) 1) "eval"
2) (integer) -3
3) 1) noscript
2) movablekeys
4) (integer) 0
5) (integer) 0
6) (integer) 0
155) 1) "smove"
2) (integer) 4
3) 1) write
2) fast
4) (integer) 1
5) (integer) 2
6) (integer) 1
156) 1) "multi"
2) (integer) 1
3) 1) readonly
2) noscript
3) fast
4) (integer) 0
5) (integer) 0
6) (integer) 0
157) 1) "sdiff"
2) (integer) -2
3) 1) readonly
2) sort_for_script
4) (integer) 1
5) (integer) -1
6) (integer) 1
158) 1) "hscan"
2) (integer) -3
3) 1) readonly
2) random
158
Redis
4) (integer) 1
5) (integer) 1
6) (integer) 1
159) 1) "brpoplpush"
2) (integer) 4
3) 1) write
2) denyoom
3) noscript
4) (integer) 1
5) (integer) 2
6) (integer) 1
160) 1) "script"
2) (integer) -2
3) 1) readonly
2) admin
3) noscript
4) (integer) 0
5) (integer) 0
6) (integer) 0
161) 1) "keys"
2) (integer) 2
3) 1) readonly
2) sort_for_script
4) (integer) 0
5) (integer) 0
6) (integer) 0
162) 1) "hdel"
2) (integer) -3
3) 1) write
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
163) 1) "hvals"
2) (integer) 2
3) 1) readonly
159
Redis
2) sort_for_script
4) (integer) 1
5) (integer) 1
6) (integer) 1
Return Value
Integer reply The number of commands returned by COMMAND.
Syntax
Following is the basic syntax of Redis COMMAND COUNT command.
Example
redis 127.0.0.1:6379> COMMAND COUNT
(integer) 163
Return Value
Array reply The list of keys from your command.
Syntax
Following is the basic syntax of Redis COMMAND GETKEYS command.
Example
redis 127.0.0.1:6379> COMMAND GETKEYS MSET a b c d e f
1) "a"
2) "c"
3) "e"
160
Redis
Return Value
Simple string reply.
Syntax
Following is the basic syntax of Redis BGSAVE command.
Return Value
Array reply The nested list of command details.
Syntax
Following is the basic syntax of Redis COMMAND INFO command.
Example
redis 127.0.0.1:6379> COMMAND INFO get set eval
1) 1) "get"
2) (integer) 2
3) 1) readonly
2) fast
4) (integer) 1
5) (integer) 1
6) (integer) 1
2) 1) "set"
2) (integer) -3
3) 1) write
2) denyoom
4) (integer) 1
161
Redis
5) (integer) 1
6) (integer) 1
162
Redis
3) 1) "eval"
2) (integer) -3
3) 1) noscript
2) movablekeys
4) (integer) 0
5) (integer) 0
6) (integer) 0
redis> COMMAND INFO foo evalsha config bar
1) (nil)
2) 1) "evalsha"
2) (integer) -3
3) 1) noscript
2) movablekeys
4) (integer) 0
5) (integer) 0
6) (integer) 0
3) 1) "config"
2) (integer) -2
3) 1) readonly
2) admin
3) stale
4) (integer) 0
5) (integer) 0
6) (integer) 0
4) (nil)
Return Value
The return type of the command is a Bulk string reply.
Syntax
Following is the basic syntax of Redis CONFIG GET command.
163
Redis
Example
redis 127.0.0.1:6379> config get *max-*-entries*
1) "hash-max-zipmap-entries"
2) "512"
3) "list-max-ziplist-entries"
4) "512"
5) "set-max-intset-entries"
6) "512"
Return Value
String reply OK, when the configuration is rewritten properly. Otherwise, an error is
returned.
Syntax
Following is the basic syntax of Redis CONFIG REWRITE command.
Return Value
String reply OK, when the configuration is set properly. Otherwise, an error is returned.
Syntax
Following is the basic syntax of Redis CONFIG Set command.
164
Redis
Example
redis 127.0.0.1:6379> CONFIG Get "requirePass"
""
redis 127.0.0.1:6379> CONFIG Set "requirePass" "pass1"
OK
Keyspace hits
Keyspace misses
Number of commands processed
Number of connections received
Number of expired keys
Number of rejected connections
Latest fork(2) time
The aof_delayed_fsync counter
Return Value
String reply - Always OK.
Syntax
Following is the basic syntax of Redis CONFIG RESETSTAT command.
Return Value
Integer reply.
Syntax
Following is the basic syntax of Redis DBSIZE command.
165
Redis
Example
redis 127.0.0.1:6379> DBSIZE
(integer) 147
Return Value
String reply.
Syntax
Following is the basic syntax of Redis DEBUG OBJECT command.
Example
redis 127.0.0.1:6379> SET a a
OK
redis 127.0.0.1:6379> DEBUG OBJECT a
Value at:0x7f68f7886df0 refcount:1 encoding:raw serializedlength:2 lru:1566733
lru_s
Return Value
String reply.
Syntax
Following is the basic syntax of Redis DEBUG SEGFAULT command.
Example
redis 127.0.0.1:6379> DEBUG SEGFAULT
Could not connect to Redis at 127.0.0.1:6379: Connection refused
166
Redis
not connected>
Return Value
String reply.
Syntax
Following is the basic syntax of Redis FLUSHALL command.
Example
redis 127.0.0.1:6379> FLUSHALL
OK
Return Value
String reply.
Syntax
Following is the basic syntax of Redis FLUSHDB command.
Example
redis 127.0.0.1:6379> FLUSHDB
OK
167
Redis
Return Value
String reply - As a collection of text lines.
Syntax
Following is the basic syntax of Redis INFO command.
Example
redis 127.0.0.1:6379> INFO
# Server
redis_version:2.8.13
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:c2238b38b1edb0e2
redis_mode:standalone
os:Linux 3.5.0-48-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.7.2
process_id:3856
run_id:0e61abd297771de3fe812a3c21027732ac9f41fe
tcp_port:6379
uptime_in_seconds:11554
uptime_in_days:0
168
Redis
hz:10
lru_clock:16651447
config_file:
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:589016
used_memory_human:575.21K
used_memory_rss:2461696
used_memory_peak:667312
used_memory_peak_human:651.67K
used_memory_lua:33792
mem_fragmentation_ratio:4.18
mem_allocator:jemalloc-3.6.0
# Persistence
loading:0
rdb_changes_since_last_save:3
rdb_bgsave_in_progress:0
rdb_last_save_time:1409158561
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
169
Redis
aof_last_write_status:ok
# Stats
total_connections_received:24
total_commands_processed:294
instantaneous_ops_per_sec:0
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:41
keyspace_misses:82
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:264
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:10.49
used_cpu_user:4.96
used_cpu_sys_children:0.00
used_cpu_user_children:0.01
# Keyspace
db0:keys=94,expires=1,avg_ttl=41638810
db1:keys=1,expires=0,avg_ttl=0
170
Redis
db3:keys=1,expires=0,avg_ttl=0
Return Value
Integer reply, a UNIX time stamp.
Syntax
Following is the basic syntax of Redis LASTSAVE command.
Example
redis 127.0.0.1:6379> LASTSAVE
(integer) 1410853592
Syntax
Following is the basic syntax of Redis MONITOR command.
Example
redis 127.0.0.1:6379> MONITOR
OK
1410855382.370791 [0 127.0.0.1:60581] "info"
1410855404.062722 [0 127.0.0.1:60581] "get" "a"
171
Redis
Syntax
Following is the basic syntax of Redis ROLE command.
Return Value
The command returns an array of elements. The first element is the role of the instance,
as one of the following three strings:
master
slave
sentinel
Example
redis 127.0.0.1:6379> ROLE
1) "master"
2) (integer) 3129659
3) 1) 1) "127.0.0.1"
2) "9001"
3) "3129242"
2) 1) "127.0.0.1"
2) "9002"
3) "3129543"
Return Value
String reply - The commands returns OK on success.
Syntax
Following is the basic syntax of Redis SAVE command.
172
Redis
Example
redis 127.0.0.1:6379> SAVE
OK
Return Value
Simple string reply on error. On success nothing is returned, since the server quits and
the connection is closed.
Syntax
Following is the basic syntax of Redis SHUTDOWN command.
Example
redis 127.0.0.1:6379> SHUTDOWN
Return Value
Simple string reply.
Syntax
Following is the basic syntax of Redis SLAVEOF command.
173
Redis
Note, that a negative number disables the slow log, while a value of zero forces the logging
of every command. slowlog-max-len is the length of the slow log. The minimum value is
zero. When a new command is logged and the slow log is already at its maximum length,
the oldest one is removed from the queue of logged commands in order to make space.
The configuration can be done by editing redis.conf or while the server is running using
the CONFIG GET and CONFIG SET commands.
Return Value
Simple string reply.
Syntax
Following is the basic syntax of Redis SLOWLOG command.
Sample Output
redis 127.0.0.1:6379> slowlog get 2
1) 1) (integer) 14
2) (integer) 1309448221
3) (integer) 15
4) 1) "ping"
2) 1) (integer) 13
2) (integer) 1309448128
3) (integer) 30
4) 1) "slowlog"
2) "get"
3) "100"
174
Redis
Return Value
Simple string reply.
Syntax
Following is the basic syntax of Redis SYNC command.
Example
redis 127.0.0.1:6379> SYNC
Entering slave output mode... (press Ctrl-C to quit)
SYNC with master, discarding 18 bytes of bulk transfer...
SYNC done. Logging commands from master.
"PING"
"PING"
"PING"
"PING"
Return Value
A multi bulk reply containing two elements:
Syntax
Following is the basic syntax of Redis TIME command.
Example
redis 127.0.0.1:6379> TIME
175
Redis
1) "1410856598"
2) "928370"
176
Redis
Redis Advanced
177
18. Redis Backup Redis
Redis SAVE command is used to create a backup of the current Redis database.
Syntax
Following is the basic syntax of redis SAVE command.
127.0.0.1:6379> SAVE
Example
Following example creates a backup of the current database.
127.0.0.1:6379> SAVE
OK
1) "dir"
2) "/user/tutorialspoint/redis-2.8.13/src"
Bgsave
To create Redis backup, an alternate command BGSAVE is also available. This command
will start the backup process and run this in the background.
Example
127.0.0.1:6379> BGSAVE
178
19. Redis Security Redis
Redis database can be secured, such that any client making a connection needs to
authenticate before executing a command. To secure Redis, you need to set the password
in the config file.
Example
Following example shows the steps to secure your Redis instance.
By default, this property is blank, which means no password is set for this instance. You
can change this property by executing the following command.
After setting the password, if any client runs the command without authentication,
then (error) NOAUTH Authentication required. error will return. Hence, the client
needs to use AUTH command to authenticate himself.
Syntax
Following is the basic syntax of AUTH command.
Example
127.0.0.1:6379> AUTH "tutorialspoint"
OK
127.0.0.1:6379> SET mykey "Test value"
OK
127.0.0.1:6379> GET mykey
"Test value"
179
20. Redis Benchmarks Redis
Redis benchmark is the utility to check the performance of Redis by running n commands
simultaneously.
Syntax
Following is the basic syntax of Redis benchmark.
Example
Following example checks Redis by calling 100000 commands.
redis-benchmark -n 100000
Sr.
Options Description Default Value
No.
180
Redis
Example
Following example shows the multiple usage options in Redis benchmark utility.
181
21. Redis Client Connection Redis
Redis accepts clients connections on the configured listening TCP port and on the Unix
socket, if enabled. When a new client connection is accepted, the following operations are
performed:
The client socket is put in non-blocking state since Redis uses multiplexing and
non-blocking I/O.
The TCP_NODELAY option is set in order to ensure that we don't have delays in our
connection.
A readable file event is created so that Redis is able to collect the client queries as
soon as new data is available to be read on the socket.
1) "maxclients"
2) "10000"
By default, this property is set to 10000 (depending upon the maximum number of file
descriptors limit of OS), although you can change this property.
Example
In the following example, we have set the maximum number of clients to 100000, while
starting the server.
Client Commands
Sr.
Command Description
No.
182
Redis
183
22. Redis Pipelining Redis
The client sends a query to the server, and reads from the socket, usually in a
blocking way, for the server response.
The server processes the command and sends the response back to the client.
Meaning of Pipelining
The basic meaning of pipelining is, the client can send multiple requests to the server
without waiting for the replies at all, and finally reads the replies in a single step.
Example
To check the Redis pipelining, just start the Redis instance and type the following command
in the terminal.
+PONG
+OK
redis
:1
:2
:3
In the above example, we will check Redis connection by using PING command. We have
set a string named tutorial with value redis. Later, we get that keys value and increment
the visitor number three times. In the result, we can see that all commands are submitted
to Redis once, and Redis provides the output of all commands in a single step.
Benefits of Pipelining
The benefit of this technique is a drastically improved protocol performance. The speedup
gained by pipelining ranges from a factor of five for connections to localhost up to a factor
of at least one hundred over slower internet connections.
184
23. Redis Partitioning Redis
Partitioning is the process of splitting your data into multiple Redis instances, so that every
instance will only contain a subset of your keys.
Benefits of Partitioning
It allows for much larger databases, using the sum of the memory of many
computers. Without partitioning you are limited to the amount of memory that a
single computer can support.
Disadvantages of Partitioning
Operations involving multiple keys are usually not supported. For instance, you
can't perform the intersection between two sets if they are stored in the keys that
are mapped to different Redis instances.
The partitioning granuliary is the key, so it is not possible to shard a dataset with
a single huge key like a very big sorted set.
When partitioning is used, data handling is more complex. For instance, you have
to handle multiple RDB/AOF files, and to get a backup of your data you need to
aggregate the persistence files from multiple instances and hosts.
Adding and removing the capacity can be complex. For instance, Redis Cluster
supports mostly transparent rebalancing of data with the ability to add and remove
nodes at runtime. However, other systems like client-side partitioning and proxies
don't support this feature. A technique called Presharding helps in this regard.
Types of Partitioning
There are two types of partitioning available in Redis. Suppose we have four Redis
instances, R0, R1, R2, R3 and many keys representing users like user:1, user:2, ... and
so forth.
Range Partitioning
Range partitioning is accomplished by mapping ranges of objects into specific Redis
instances. Suppose in our example, the users from ID 0 to ID 10000 will go into instance
R0, while the users from ID 10001 to ID 20000 will go into instance R1 and so forth.
185
Redis
Hash Partitioning
In this type of partitioning, a hash function (eg. modulus function) is used to convert the
key into a number and then the data is stored in different-different Redis instances.
186
24. Redis Java Redis
Before you start using Redis in your Java programs, you need to make sure that you have
Redis Java driver and Java set up on the machine. You can check our Java tutorial for Java
installation on your machine.
Installation
Now, let us see how to set up Redis Java driver.
You need to download the jar from the path Download jedis.jar. Make sure to
download the latest release of it.
Now, let's compile and run the above program to test the connection to Redis server. You
can change your path as per your requirement. We are assuming the current version of
jedis.jar is available in the current path.
$javac RedisJava.java
$java RedisJava
Connection to server sucessfully
Server is running: PONG
187
Redis
$javac RedisStringJava.java
$java RedisStringJava
Connection to server sucessfully
Stored string in redis:: Redis tutorial
188
Redis
$javac RedisListJava.java
$java RedisListJava
Connection to server sucessfully
Stored string in redis:: Redis
Stored string in redis:: Mongodb
Stored string in redis:: Mysql
$javac RedisKeyJava.java
$java RedisKeyJava
Connection to server sucessfully
List of stored keys:: tutorial-name
List of stored keys:: tutorial-list
189
25. Redis PHP Redis
Before you start using Redis in your PHP programs, you need to make sure that you have
Redis PHP driver and PHP set up on the machine. You can check PHP tutorial for PHP
installation on your machine.
Installation
Now, let us check how to set up Redis PHP driver.
cd phpredis
sudo phpize
sudo ./configure
sudo make
sudo make install
Now, copy and paste the content of modules folder to the PHP extension directory and
add the following lines in php.ini.
extension = redis.so
190
Redis
When the above program is executed, it will produce the following result.
When the above program is executed, it will produce the following result.
191
Redis
Redis
Mongodb
Mysql
192