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 contact@tutorialspoint.com.
i
Redis
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Disclaimer & Copyright ..................................................................................................................................... i
Table of Contents ............................................................................................................................................ ii
7. Redis ─ Strings......................................................................................................................................... 29
Redis Strings Commands ............................................................................................................................... 29
String Set Command ...................................................................................................................................... 32
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
10
Redis
(integer) 0
redis 127.0.0.1:6379> smembers tutoriallist
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.
17
Redis
Return Value
Integer value
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.
18
Redis
In the above example, 1 minute (or 60 seconds) time is set for the key tutorialspoint.
After 1 minute, the key will expire automatically.
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
19
Redis
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, 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.
OK
21
Redis
22