introduction to redis

40
Redis – Memory is the new Disk •Deepak Mittal •IntelliGrape Software

Upload: intelligrape

Post on 06-May-2015

1.087 views

Category:

Technology


5 download

DESCRIPTION

Redis is an advanced in memory key-value store designed for a world where "Memory is the new disk and disk is the new tape". Redis has some unique properties -- like blazing read and write speed, rich atomic operations and asynchronous persistence -- which make it ideally suited for a number of situations.

TRANSCRIPT

Page 1: Introduction to Redis

Redis – Memory is the new Disk

•Deepak Mittal• IntelliGrape Software

Page 2: Introduction to Redis

2

Agenda• What is Redis• Available clients• Data types• Operations on data types• Performance• Persistence• Sweet spots• Design considerations / Best practices• Adopters

Page 3: Introduction to Redis

3

About me?• Deepak Mittal• CEO of IntelliGrape Software• 13 years of Software development

experience• Interests : Performance tuning of web-

applications and teams

Page 4: Introduction to Redis

4

Poll • Familiarity with NoSQL? • Memcache?• Redis?

Page 5: Introduction to Redis

5

What is Redis

Key/value storelike

Memcached on steroids

Page 6: Introduction to Redis

6

What is Redis

In-memory NoSQL databasebacked by disk

Page 7: Introduction to Redis

7

What is Redis

Collection of data structures exposed

over the network

Page 8: Introduction to Redis

8

What is Redis

Not just Strings!

Keys can containStrings, Hashes, Lists, Sets

andSorted Sets

Page 9: Introduction to Redis

9

What is Redis

Page 10: Introduction to Redis

10

A Brief History of Redis• Started in 2009 by Salvatore Sanfillipo• VMWare hires Salvatore & Pieter Noordhuis• Financially supported and promoted by

VMWare

Page 11: Introduction to Redis

11

Redis Characteristics• Is single-threaded• Has queryble key namespace• Is extremely fast (~100 K

operations/second)• Is easy to install (no dependencies)• Is simple and flexible• Stores everything in memory• Written in ANSI C and BSD licensed (free,

open)

Page 12: Introduction to Redis

12

Language Support

Ruby, Python, PHP, Erlang,Tcl, Perl, Lua, Java, Scala,

Clojure, C#, C/C++,JavaScript/Node.js, Haskell,

IO, Go

Page 13: Introduction to Redis

13

Data Types• Strings (Integers)• Lists• Hashes• Sets• Sorted Sets

Page 14: Introduction to Redis

14

Thinking in Redis• This is not an RDBMS• Its all about the KEYS, and NOT the values• There is no querying on values• There are no indexes• There are no schemas

Page 15: Introduction to Redis

Operations on Strings/Integers• SET key value• GET key• MGET / MSET • INCR / DECR• INCRBY DECRBY • APPEND key value• SETNX key value• GETSET key value

Page 16: Introduction to Redis

16

Installation

$ wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz

$ tar xzf redis-2.4.2.tar.gz

$ cd redis-2.4.2

$ make

$ ./src/redis-server

Page 17: Introduction to Redis

17

Atomicity of Commands• All of the built-in commands of Redis are

atomic• Commands can be chained together

(pipelined) to create complex atomic transactions

• Redis is single threaded and therefore, no locking is necessary

• In other words, commands like INCR won’t tread on each other’s toes coming from multiple clients simultaneously!

Page 18: Introduction to Redis

18

Key Expiration• When caching, we don’t want things to live

forever• Any item in Redis can be made to expire

after or at a certain time

EXPIRE my_key 60

EXPIREAT my_key 1293840000

Page 19: Introduction to Redis

Operations on Lists• LPUSH/RPUSH key value [value ...]

• LPOP/RPOP key

• LRANGE key start stop

• LSET key index value

• LLEN key

• LTRIM key start stop

• LINSERT key before|after pivot value

• BLPOP key [key ...] timeout

• LREM key count value19

Page 20: Introduction to Redis

Operations on Sets• SADD key member

• SPOP key

• SMEMBERS key

• SINTER key [key ...]

• SUNION key [key ...]

• SISMEMBER key member

• SCARD key

20

Page 21: Introduction to Redis

Operations on Sorted Sets• ZADD key score member [score] [member]

• ZCARD key

• ZCOUNT key min max

• ZINCRBY key increment member

• ZRANK key member

• ZRANGE key start stop [WITHSCORES]

• ZSCORE key member

21

Page 22: Introduction to Redis

Operations on Hashes• HSET key field value

• HGET key field

• HKEYS key

• HVALS key

• HLEN key

• HEXISTS key field

• HDEL key field [field ...]

22

Page 23: Introduction to Redis

Operations on Keys• TYPE key

• TTL key

• KEYS pattern

• EXPIRE key seconds

• EXPIREAT key timestamp

• EXISTS key

• DEL key

23

Page 24: Introduction to Redis

Redis Administration Commands• MONITOR• SAVE / BGSAVE• INFO• DBSIZE• FLUSHALL• CONFIG SET parameter value

24

Page 25: Introduction to Redis

25

Performance

We added two million items into a list in 1.28 seconds, with a networking layer between

us and the server

Salvatore Sanfilippo, Creator

Page 26: Introduction to Redis

26

Performance• Almost same for read and write, handles

writes faster than read• 100 K ops/second • Performance depends a lot on – Hardware– Persistence configuration – Operation complexity– Payload size– Batch size (pipelining)

Page 27: Introduction to Redis

27

Redis-benchmark

Page 28: Introduction to Redis

28

Demo• Redis CLI• Getting Help• Operations on Strings• Key Expiry• Operations on Lists, Set• Groovy program operating on Sorted

Set and Hash• MONITOR / INFO / DBSIZE / TYPE• Redis benchmark

Page 29: Introduction to Redis

29

Data Durability

Dump data to disk after certain conditions are met

OR

Manually

AND/OR

Append Only File (AOF)

Page 30: Introduction to Redis

30

Advantages of Persistence• On server restart, Redis re-populates its

cache from files stored on disk• No issue of “cache warm-up”• It is possible to back-up and replicate the

files/cache

Page 31: Introduction to Redis

31

Memory Requirements?• Depends upon the payload size, and the

data-type used • Instagram reports that they stored 300

million key-value pairs in about 5 GB of memory => 16 MB/million pairs (they also report that Memcache requires 52 MB for the same million keys)

Page 32: Introduction to Redis

32

Sweet Spots / Use-cases• Cache Server• Stats collection• Tag Cloud• Most read / shared articles• Share state between processes / Session

Tokens• Auto-completion• API rate limiting• Activity Feeds• Job Queue (LPUSH & RPOP)• URL Shortener mapping

Page 33: Introduction to Redis

33

Case Studies• Video Marketing Platform– Real-time analytics– Stats for usage reports to clients

• Content Publishing application– Tag Cloud – Top Read Articles– URL Shortening information

• Social Media Marketing application– Daily, Monthly and overall stats collection– List of online users

Page 34: Introduction to Redis

34

Design Considerations / Best Practices• Avoid excessively long keys• Use consistent key namespace

nomenclature• Have human readable keys• Since there is no querying on values, keys

should be thought-out before hand• Store data based on use-cases. Ad-hoc

queries can-be very expensive • Multiple databases are useful for

segmenting key namespaces, especially where key queries are needed

Page 35: Introduction to Redis

35

Design Considerations / Best Practices• All data must fit in memory• Data duplication/redundancy is OK• Persistence strategy should be chosen

based on the kind of data that you are storing and performance trade-off must be understood

• Good for write-heavy, frequently changing and non-relational data

• Polyglot-persistence is a smart choice• Start by using Redis to augment, and then

replace

Page 36: Introduction to Redis

36

Some Adopters• Stackoverflow• Craiglist• Github• Digg• Instagram• Blizzard Entertainment

Page 37: Introduction to Redis

37

Learning Resources• http://redis.io/commands• http://try.redis-db.org• Redis CLI

Page 38: Introduction to Redis

38

Advanced Features• Master-slave replication• Pub / Sub• Transactions• Using multiple databases• Commands pipelining• Clustering / Sharding• LUA scripting

Page 39: Introduction to Redis

39

What is Redis• Simple & Easy to use (Key / Value, data-

types map to programming languages)• Flexible (Supports commonly used data-

types• Fast (100 Kops / second)• Powerful (data-types, operations on data-

types)• Safe (persistence)• Easy to use (various client libraries)• Widely adopted (Digg, Craiglist, Github …)

Page 40: Introduction to Redis

40

Discussion

Now that you understand Redis and its capabilities, what use-cases do come to

your mind?

What are your concerns around usage of Redis?