Slide 1

Slide 1 text

Peter Cooper presents.. A rapid tour of Redis, the super-fast data structure server!

Slide 2

Slide 2 text

who am i?

Slide 3

Slide 3 text

overview What is Redis? What can you use it for? Characteristics Installing and using Data types and commands Putting it all together Some extra stu!

Slide 4

Slide 4 text

WHAT IS REDIS?

Slide 5

Slide 5 text

remote dictionary server

Slide 6

Slide 6 text

Salvatore Sanfilippo a.k.a. antirez

Slide 7

Slide 7 text

remote dictionary server

Slide 8

Slide 8 text

remote hash server

Slide 9

Slide 9 text

data structure server!

Slide 10

Slide 10 text

data structure server! isn’t that a “database”?

Slide 11

Slide 11 text

in a way, but.. no “tables” no SQL no !xed relationships isn’t that a “database”?

Slide 12

Slide 12 text

think less.. and more.. name age user 1 jim 21 user 2 amy 44 user:1:name = Jim user:1:age = 21 user:2:name = Amy user:2:age = 44 just keys and values.. a la SQLite, MySQL, etc..

Slide 13

Slide 13 text

In Ruby: a = “Hello world” b = “This is a test” storing some strings

Slide 14

Slide 14 text

In Redis: SET A “Hello world” SET B “This is a test” storing some strings

Slide 15

Slide 15 text

And to retrieve: GET A GET B storing some strings

Slide 16

Slide 16 text

1 0 1 0 1 0 1 0 like an assembly language? mov rax, 0x0a68732f push rax xor rax, rax mov rsi, rsp ...

Slide 17

Slide 17 text

1 0 1 0 1 0 1 0 SURE! :-)

Slide 18

Slide 18 text

DSL for abstract data types and that’s from the Redis manifesto! google “redis manifesto”

Slide 19

Slide 19 text

# big ole’ hash in the sky?

Slide 20

Slide 20 text

open source (BSD) !rst public release in 2009 VMware hired Salvatore in 2010 (and Pieter Noordhuis!)

Slide 21

Slide 21 text

add more commands add persistence add more data types and more.. familiar with memcached?

Slide 22

Slide 22 text

USES FOR REDIS

Slide 23

Slide 23 text

Real time logging LLOOGGs! Just keep pushing stu" onto a list.. LLOOGG was/is a service run by Salvatore that Redis was born out of.

Slide 24

Slide 24 text

Scoreboarding Sony did/does this Sorted sets are ideal! Add entries to a sorted set and query for results later. SuperDude1 20 pts xx_idiot_xx 19 pts mrkewl 11 pts freddieM 2 pts

Slide 25

Slide 25 text

Inter Process Communication (IPC) Talking between programs Use lists or a pre-agreed set of keys

Slide 26

Slide 26 text

Job queues resque does this! Push jobs onto a queue and forget about them (sort of..!)

Slide 27

Slide 27 text

Session storage redis-store library can help with this for your Ruby webapps SET session:[session_key] “{ stuff: 123, blah: 234 }”

Slide 28

Slide 28 text

Cache redis-store library can help with this for your Ruby webapps

Slide 29

Slide 29 text

Rate limiting INCR hits:33.12.32.22 EXPIRE hits:33.12.32.22 3600 INCR hits:33.12.32.22 It’s a bit naive but.. you get the idea.

Slide 30

Slide 30 text

YouPorn.com Yep, really. Redis is their primary datastore.

Slide 31

Slide 31 text

YouPorn.com AND THEY’RE CANADIAN!

Slide 32

Slide 32 text

GroupOn, Stack Over#ow, Craigslist There’s a lot more than this but just to make the point.. Redis is not some fruit loop far out technology.

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

CHARACTERISTICS

Slide 35

Slide 35 text

Single threaded and event driven Think Node.js or a simple EventMachine loop (technically in Redis 2.4+ some threads are used for background IO tasks but not servicing requests)

Slide 36

Slide 36 text

Individual operations are atomic Nothing can interrupt or clash with a command you issue. Remember: Redis is single threaded processed just one at a time..

Slide 37

Slide 37 text

Data can be “expired” Want a key/value to die in 60 seconds? You got it.

Slide 38

Slide 38 text

Everything is stored in memory It has persistence but.. your entire dataset is in memory too. If it can’t !t, stu" will be pruned.

Slide 39

Slide 39 text

Everything is stored in memory THIS MAKES REDIS SUPER FAST!

Slide 40

Slide 40 text

Did I mention SUPER FAST? Think 50k+ GETs and SETs per second.

Slide 41

Slide 41 text

Lots of Redis clients C, C#, C++, Clojure, Lisp, Ruby, Python, Go, Erlang Scala, Objective C, Lua, Node, PHP, Smalltalk, Tcl Some languages even have ORMs supporting Redis. Like Ohm in Ruby. DataMapper has an adapter too.

Slide 42

Slide 42 text

5 Data Types Strings Lists Sets Sorted Sets Hashes

Slide 43

Slide 43 text

INSTALLING REDIS

Slide 44

Slide 44 text

brew install redis If you’re on OS X and have Homebrew installed

Slide 45

Slide 45 text

or just compile it from source it’s ridiculously quick and easy make ./redis-server

Slide 46

Slide 46 text

Windows ports are available but your mileage may vary

Slide 47

Slide 47 text

USING REDIS

Slide 48

Slide 48 text

From the command line.. redis-cli SET a “foo” redis-cli GET a or just redis-cli for a REPL/console.

Slide 49

Slide 49 text

From Ruby, say? require 'redis' redis = Redis.new # or.. redis = Redis.new(:host => '1.2.3.4', :port => 1234) redis.set "test", "hello world!" p redis.get “test” gem install redis

Slide 50

Slide 50 text

DATA TYPES

Slide 51

Slide 51 text

Those 5 Data Types (again) Strings Lists Sets Sorted Sets Hashes

Slide 52

Slide 52 text

STRINGS As simple as it gets..

Slide 53

Slide 53 text

SET mystring “hello world” ./redis-cli Redis command line client app GET mystring returns “hello world” ./redis-cli Key Value

Slide 54

Slide 54 text

GETSET MGET SETNX SETEX MSET MSETNX INCR INCRBY DECR DECRBY APPEND SUBSTR Works on strings that appear to be integers. Magic!

Slide 55

Slide 55 text

GET hits:33.12.32.22 #=> (nil) INCR hits:33.12.32.22 #=> 1 It even does the initializing..

Slide 56

Slide 56 text

GENERIC OPERATIONS Yes, a short break with those data types..

Slide 57

Slide 57 text

TYPE EXISTS RENAME DEL EXPIRE EXPIREAT TTL PERSIST

Slide 58

Slide 58 text

LISTS Lurvely linked lists with super-fast PUSH and POP

Slide 59

Slide 59 text

f e d c b a RPUSH LPOP LPUSH RPOP RPUSH my_q f e.g.

Slide 60

Slide 60 text

f e d c b a } LRANGE 2 3 LLEN == 6 LINDEX 5 X LREM 1 b

Slide 61

Slide 61 text

f e d c b a RPUSH LPOP RPUSH my_q a RPUSH my_q b LPOP my_q == “a” LPOP my_q == “b” LPOP my_q == (nil) Or BLPOP to block (wait) until something can be popped Queues Not a native data type. Just a list!

Slide 62

Slide 62 text

LTRIM myqueue 0 99 Want to keep a list just 100 items long?

Slide 63

Slide 63 text

SETS Think a list but with only unique elements.

Slide 64

Slide 64 text

SADD contains:aba abacus SADD contains:aba cabal SADD contains:aba baba # .. etc .. SISMEMBER contains:aba abacus #=> 1 (or true)

Slide 65

Slide 65 text

contains:aba contains:ase abacus cabal baba hello teabag base cabaret database vase vaseline baseline uncase unbased phase database tease SADD contains:ase suitcase SREM contains:aba hello SMOVE contains:aba contains:ase base

Slide 66

Slide 66 text

contains:aba contains:ase abacus cabal baba teabag cabaret database vase vaseline baseline unbased phase database suitcase SCARD contains:aba == 6 SISMEMBER contains:aba chips == 0 (meaning false) SRANDMEMBER contains:aba == “teabag” SMEMBERS contains:ase == vase, vaseline, baseline, unbased, phase, database, suitcase

Slide 67

Slide 67 text

contains:aba contains:ase abacus cabal baba teabag cabaret vase vaseline baseline unbased phase suitcase SINTER contains:aba contains:ase == database database This is only a simple example. SINTER can take any number of arguments! SUNION is another command that will join sets together.

Slide 68

Slide 68 text

contains:aba contains:ase abacus cabal baba teabag cabaret vase vaseline baseline unbased phase suitcase SINTERSTORE resultset contains:aba contains:ase database SUNIONSTORE does the same for set unions. database resultset

Slide 69

Slide 69 text

SORTED SETS a.k.a. “scored” sets Like the sets we saw before, but with a “score” for each member too.

Slide 70

Slide 70 text

ZADD scoreboard 10 “Johnny” ZADD scoreboard 8 “Fred” ZADD scoreboard 20 “Amy” ZREVRANGE scoreboard 0 1 A very short example.. .. gets us the top 2 scorers!

Slide 71

Slide 71 text

HASHES Like Ruby’s hashes but with string-only values.

Slide 72

Slide 72 text

Yep, you can only use strings in a hash. No super duper weird recursive data structures for you!

Slide 73

Slide 73 text

HSET user:1 name “John” HGET user:1 name Yet another very short example.. Other commands include HGETALL, HKEYS, HVALUES, HINCRBY, etc.. name John age 20 gender male user:1

Slide 74

Slide 74 text

OTHER STUFF!

Slide 75

Slide 75 text

SERVER COMMANDS

Slide 76

Slide 76 text

SAVE BGSAVE INFO MONITOR SHUTDOWN FLUSHDB QUIT and more..

Slide 77

Slide 77 text

TRANSACTIONS but not in the ACID sense

Slide 78

Slide 78 text

MULTI INCR a INCR b EXEC Careful though.. no rollbacks! This is when “stu!” actually happens.

Slide 79

Slide 79 text

WATCH somekey GET somekey MULTI SET someotherkey $x EXEC You can also ensure a transaction fails if a dependency is broken. Put result into $x This fails if somekey changed. (check and set)

Slide 80

Slide 80 text

PERSISTENCE

Slide 81

Slide 81 text

Dump of entire database (snapshots) AOF: Append Only File Persistence can be tweaked.. - save 60 1000 Dumps by default, AOF can be enabled.

Slide 82

Slide 82 text

REPLICATION

Slide 83

Slide 83 text

Very simple master/slave replication. Server 1 Server 2 12.34.56.78:6378 12.34.56.78:6379

Slide 84

Slide 84 text

Simple master/slave replication. Server 1 Server 2 12.34.56.78:6378 12.34.56.78:6379 SLAVEOF 12.34.56.78 6378

Slide 85

Slide 85 text

Clients can hammer server 1 while server 2 does persistence, perhaps? Server 1 Server 2 SAVE disk

Slide 86

Slide 86 text

Want to do a live replacement of Redis without a!ecting clients? REPLICATE AND SWITCH!

Slide 87

Slide 87 text

THE FUTURE

Slide 88

Slide 88 text

“Redis Cluster” Lua-based scripting (in 2.6) High resolution expiry times (in 2.6) More introspection - imagine pub/sub for internal events

Slide 89

Slide 89 text

IN CLOSING

Slide 90

Slide 90 text

search for.. “try redis” “redis documentation” “the little redis book”

Slide 91

Slide 91 text

coming mid 2012.. was out in 2011

Slide 92

Slide 92 text

bye! [email protected] @peterc