2. What’s Redis?
• Redis is an open source, advanced key-value store. It
is often referred to as a data structure server since
keys can contain strings, hashes, lists, sets and sorted
sets.
• Redis is written in ANSI C and works in most POSIX
systems like Linux, *BSD, OS X and Solaris without
external dependencies.
• Redis can be used from most programming
languages, such as C, C#, C++, Java, Lua, Node.js, Perl,
Objective-C, PHP, Python, Ruby, Smalltalk, Scala,
Erlang, Common Lisp …
3. How to use Redis?
• We can download the source code of Redis from
http://redis.io/download and install it just by make
&& make install.
• By default, both the server and client of Redis can be
found in the directory /usr/local/bin after installation.
• Some common commands:
- set/get/mset/mget
- lpush/rpush/lpop/rpop
- hset/hget/hmset/hmget
- sadd/smembers/zadd/zrange
4. C Client
• Hiredis is a official C client library for the Redis
database.
• We can download Hiredis from
https://github.com/antirez/hiredis/downloads.
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c->err) {
printf("Error: %sn", c->errstr);
}
reply = redisCommand(redisContext , "SET foo bar");
5. Java Client
• Jedis is a blazingly small and sane Redis java client.
• We can download Jedis package from
https://github.com/xetorthio/jedis/downloads.
Jedis jedis = new Jedis("localhost");
jedis.connect();
jedis.set("foo", "bar");
String value = jedis.get("foo");
6. Redis Persistence
• Redis provides a different range of persistence
options: RDB and AOF.
• The RDB persistence performs point-in-time
snapshots of your dataset at specified intervals.
- #save 900 1 or #save 300 10
• The AOF persistence logs every write operation
received by the server, that will be played again at
server startup, reconstructing the original dataset.
- no fsync at all or fsync every second or fsync at
every query
7. Redis Replication
• Redis master-slave replication allows slave Redis
servers to be exact copies of master.
- A master can have multiple
slaves slave
-Slaves can accept other
slaves connections
master
- Non-blocking at the master
side
slave
- Replication can be used for
scalability
- Replication can avoid saving
process at master side
8. Redis Cluster
• Redis nodes are connected and functionally
equivalent, but actually there are two kinds of nodes:
master nodes and slave nodes.
1
0 4
2 3
1
3 5
4 master 0
slave 2
0 5
2
1
5 4
3