introduction to redis

21
Introduction to Redis Saeid Zebardast @saeidzeb zebardast.com April 2015 1

Upload: saeid-zebardast

Post on 15-Jul-2015

419 views

Category:

Technology


6 download

TRANSCRIPT

Page 2: Introduction to Redis

Agenda● What is Redis?● Features● Persistence● Data types● Commands● Publish / Subscribe● Sort● Transactions● Replication● Cluster 2

Page 3: Introduction to Redis

What is Redis?● Data structure server● In-memory Key-Value store

○ it is also persistent!● Open source, BSD licensed

3

Page 4: Introduction to Redis

Features● Abstract data types● Atomic operations● Service-side operations● Transactions● Pub/Sub● Lua scripting● TTL (Time-to-Live)● Master-Slave asynchronous replication● Atomic Failover

4

Page 5: Introduction to Redis

Persistence● Can be optionally disabled● RDB file

○ Synchronized to disk (eventually or immediately)○ Use SAVE or BGSAVE command to create a snapshot manually.

● Data is dumped or written as a append-only change-log (AOF)● AOF mode supports transactional disk write● Default config:

○ Save after 900 sec (15 min) if at least 1 key changed.○ Save after 300 sec (5 min) if at least 10 keys changed.○ Save after 60 sec if at least 10000 keys changed.

5

Page 6: Introduction to Redis

Data Types● String (binary safe)● Lists (linked lists)● Sets● Sorted sets (with scores)● Hashes (<String, String>)● Bitmaps (Bit arrays)

Note: The type of a value determines what operations (called commands) are available for the value itself.

6

Page 7: Introduction to Redis

Redis keys● Binary safe● Empty string is valid key● Very long key is a bad idea● Very short key is [often] a bad idea

○ u1000flw● Stick with a schema

○ user:1000:followers● Max allowed key size is 512 MB.

7

Page 8: Introduction to Redis

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

○ MSET key value [key value ….]● GET key

○ MGET key [key ….]● INCR/DECR key● INCRBY/DECRBY key increment● DEL key● EXISTS key● EXPIRE key seconds● TTL/PTTL key● TYPE key● MONITOR● HELP command

8

Page 9: Introduction to Redis

Atomic Operations● GETSET key value

○ Puts a new value, retrieving old one.● SETNX key value

○ Sets a value only if it does not exist.

9

Page 10: Introduction to Redis

Strings● Simplest type of value in Redis● redis-cli> SET mykey value

OK

● redis-cli> GET mykey“value”

● redis-cli> SET foo 110OK

● redis-cli> INCR bar(integer) 1

● redis-cli> INCRBY foo 50(integer) 160

● Max allowed value size is 512 MB.

10

Page 11: Introduction to Redis

● Implemented by Linked List● Push and pop at both sides● BLPOP: Blocking POP

LPOPBLPOP

Lists

A B C DLPUSH RPUSH

RPOPBRPOP

LRANGE

LINDEXLSETLREM...

11

Page 12: Introduction to Redis

Sets

A, B, CSADD

B, C, DSREMSPOP

SISMEMBER

B, CSINTER

A, B, C, DSUNION

ASDIFF D

12

Page 13: Introduction to Redis

● Same as sets, but with score per element

Sorted Sets

foo | 12.1 bar | 14.3 baz | 17.87 qux | 30.9876ZADD key score memeber [score memeber

...]

ZRANGE key start stop [withscores]ZREVRANGE key start stop [WITHSCORES]ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

ZSCORE key memberZRANK key member

13

Page 14: Introduction to Redis

Hashes● redis-cli> HSET foo bar 1

(integer) 1

● redis-cli> HSET foo baz 2(integer) 1

● redis-cli> SET foo foo foo(integer) 1

● redis-cli> HGETALL foo1) "bar"

2) "1"

3) "baz"

4) "1"

5) "foo”

6) "foo"

● redis-cli> HINCRBY foo bar 1(integer) 2

● redis-cli> HGET foo bar“2”

● redis-cli> HKEYS foo1) "bar"

2) "baz"

3) "foo"

● redis-cli> HSCAN foo 0 MATCH *ba*1) "0"

2)

1) "bar"

2) "3"

3) "baz"

4) "1" 14

Page 15: Introduction to Redis

Publish / Subscribe (PubSub)● Clients can subscribe to channels or patterns.● Subscribing is O(1), Posting message is O(N)● Chats, Comet Apps, Real time analytics and etc.

On Client 1:

redis-cli> subscribe feed:foo feed:bar feed:baz

Reading messages...

On Client 2:

redis-cli> publish feed:foo “hello world!”

(integer) 1

On Client 1:

Reading messages...

1) "message"

2) "feed:foo"

3) "hello world!"

15

Page 16: Introduction to Redis

Sort● SORT key

○ [BY pattern]○ [LIMIT offset count]○ [GET pattern [GET pattern ...]]○ [ASC|DESC]○ [ALPHA]○ [STORE destination]

● By default, sorting is numeric and elements compared as double.● Use the ALPHA modifier to sort string list lexicographically (UTF-8 aware).● Result can be stored as a list.

16

Page 17: Introduction to Redis

Transactions● MULTI, [commands], EXEC● All commands are executed after EXEC.● redis-cli> MULTI

OK

● redis-cli> SET foo barQUEUED

● redis-cli> INCRBY num 1 QUEUED

● redis-cli> EXEC1) OK

2) (integer) 1

● DISCARD for discarding a transaction● WATCH for locking keys

17

Page 18: Introduction to Redis

Replication● Asynchronous, Non-Blocking Replication● Turn on persistence in the Master or avoid restarting automatically.● 64GB of memory i.e., Only 64GB of data

18

Redis Master Server

Redis Slave ServerRedis Slave ServerRedis Slave Server

Page 19: Introduction to Redis

Cluster● Version 3.0 or higher. (Current stable version is 2.8)● Automatically split your dataset among multiple nodes.● Continue operations when a subset of the nodes are experiencing failures.● 10 clustered computers with each 64GB of RAM, store 640GB of data.

19

Page 20: Introduction to Redis

Where Can I Learn More?● redis.io● redis.io/topics/quickstart● try.redis.io

20