+ All Categories
Home > Technology > redis überall

redis überall

Date post: 08-Jul-2015
Category:
Upload: zucaritask
View: 225 times
Download: 0 times
Share this document with a friend
Description:
Rug-b Nov. 2014 lightning talk
Popular Tags:
40
überall redis Mariano Vallés @zucaritask
Transcript
Page 1: redis überall

überallredis

Mariano Vallés!@zucaritask

Page 2: redis überall

redis

@zucaritask

/rədɪs/

Page 3: redis überall

redis

@zucaritask

in memory key-value cache and store

/rədɪs/

Page 4: redis überall

redis

@zucaritask

in memory key-value cache and store

/rədɪs/

Considered a data structure server since keys can contain strings, hashes, lists, sets …

Page 5: redis überall

[ ] a hash has values [ ] those values can be hashes

In redis: !

First: Nested hashes

Page 6: redis überall

[ ] a hash has values [ ] those values can be hashes

In redis: ! ✓

First: Nested hashes

Page 7: redis überall

[ ] a hash has values [ ] those values can be hashes

In redis: !

First: Nested hashes

Page 8: redis überall

:001  >  $redis.hgetall(“levels:1234").  !{      easy_10:  “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}",        easy_20:  "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}"  }

First: Nested hashes

Page 9: redis überall

:001  >  $redis.hgetall(“levels:1234").  !{      easy_10:  “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}",        easy_20:  "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}"  }

:002  >  $redis.hgetall(“levels:1234").inject({})  do  |result,  (key,  value)|                    result[key]  =  JSON.parse(value);                    result              end    =>  {              easy_10:  {progress:  0,  goal:  10,  started_at:  1415278026,  finished_at:  0}              easy_20:  {progress:  0,  :goal:  20,  started_at:  1415278050,  finished_at:  0}          }  

First: Nested hashes

Page 10: redis überall

:001  >  $redis.hgetall(“levels:1234").  !{      easy_10:  “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}",        easy_20:  "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}"  }

Redis hash

:002  >$redis.hgetall(“levels:1234").inject({})  do  |result,  (key,  value)|                    result[key]  =  JSON.parse(value);                    result              end    =>  {              easy_10:  {:progress  =>  0,  :goal  =>  10,  started_at:  1415278026,  finished_at:  0}              easy_20:  {:progress  =>  0,  :goal  =>  20,  started_at:  1415278050,  finished_at:  0}          }  

Page 11: redis überall

:001  >  $redis.hgetall(“levels:1234").  !{      easy_10:  “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}",        easy_20:  "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}"  }

Redis hash

:002  >$redis.hgetall(“levels:1234").inject({})  do  |result,  (key,  value)|                    result[key]  =  JSON.parse(value);                    result              end    =>  {              easy_10:  {:progress  =>  0,  :goal  =>  10,  started_at:  1415278026,  finished_at:  0}              easy_20:  {:progress  =>  0,  :goal  =>  20,  started_at:  1415278050,  finished_at:  0}          }  

!

le crowddeal with

Page 12: redis überall

{      easy_10:  “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}",        easy_20:  "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}"  }

From:

First: Nested hashes

Page 13: redis überall

{      easy_10:  “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}",        easy_20:  "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}"  }

From:

To:

First: Nested hashes

Page 14: redis überall

{      easy_10:  “{\"progress\":0,\"goal\":10,\"started_at\":1415278026,\"finished_at\":0}",        easy_20:  "{\"progress\":0,\"goal\":20,\"started_at\":1415278050,\"finished_at\":0}"  }

{      easy_10:  "0,10,1415278026,0",      easy_20:  "4,20,1415278050,0"  }

From:

To:

First: Nested hashes

Page 15: redis überall

{      easy_10:  "0,10,1415278026,0",      easy_20:  "4,20,1415278050,0"  }

To:

First: Nested hashes

Page 16: redis überall

{      easy_10:  "0,10,1415278026,0",      easy_20:  "4,20,1415278050,0"  }

To:

First: Nested hashes

progress

goal

started_at

finished at

Page 17: redis überall

[ ] hashes are memory efficient [ ] millions of hashes are memory efficient

In redis: !

Last: Avoiding hashes

Page 18: redis überall

[ ] hashes are memory efficient [ ] millions of hashes are memory efficient

In redis: ! ✓

Last: Avoiding hashes

Page 19: redis überall

[ ] hashes are memory efficient [ ] millions of hashes are memory efficient

In redis: !

Last: Avoiding hashes

Page 20: redis überall

Last: Avoiding hashes

Page 21: redis überall

:01  >  $redis.hgetall(“counter:1234”)

Last: Avoiding hashes

Page 22: redis überall

:01  >  $redis.hgetall(“counter:1234”)=>  {            popup_shown:  1,            extra_life_given:  0        }

Last: Avoiding hashes

Page 23: redis überall
Page 24: redis überall

the crowdhas no mercy

Page 25: redis überall

the crowdhas no mercy

deal with it

Page 26: redis überall

From:

To:

:01  >  $redis.hgetall(“counter:1234”)  =>  {              popup_shown:  1,              extra_life_given:  0          }

Last: Avoiding hashes

Page 27: redis überall

From:

To:

:01  >  $redis.hgetall(“counter:1234”)  =>  {              popup_shown:  1,              extra_life_given:  0          }

:01  >  $redis.get(“counter:1234”)

 =>  "\u0000\u0001"

Last: Avoiding hashes

Page 28: redis überall

From:

To:

:01  >  $redis.hgetall(“counter:1234”)  =>  {              popup_shown:  1,              extra_life_given:  0          }

:01  >  $redis.get(“counter:1234”)

 =>  "\u0000\u0001"

A sequence of binary data

Last: Avoiding hashes

Page 29: redis überall

From:

To:

:01  >  $redis.hgetall(“counter:1234”)  =>  {              popup_shown:  1,              extra_life_given:  0          }

:01  >  $redis.get(“counter:1234”)

 =>  "\u0000\u0001"

A sequence of binary data

Less memory usage FTW!

Last: Avoiding hashes

Page 30: redis überall

class  Counter  !    COUNTERS  =  %w[  VERSION  popup_shown  extra_life_given  ]        !    def  set(key,  value)          $redis.setrange(              user_id,              COUNTERS.index(key),              [value].pack("C*")          )      end  !    def  [](key)          char  =  $redis.getrange(user_id,  index,  index)          char.unpack("C*").first.to_i      end  end

Last: Avoiding hashes

Page 31: redis überall

class  Counter  !    COUNTERS  =  %w[  VERSION  popup_shown  extra_life_given  ]        !    def  set(key,  value)          $redis.setrange(              user_id,              COUNTERS.index(key),              [value].pack("C*")          )      end  !    def  [](key)          char  =  $redis.getrange(user_id,  index,  index)          char.unpack("C*").first.to_i      end  end

Last: Avoiding hashes

Page 32: redis überall

class  Counter  !    COUNTERS  =  %w[  VERSION  popup_shown  extra_life_given  ]        !    def  set(key,  value)          $redis.setrange(              user_id,              COUNTERS.index(key),              [value].pack("C*")          )      end  !    def  [](key)          char  =  $redis.getrange(user_id,  index,  index)          char.unpack("C*").first.to_i      end  end

Last: Avoiding hashes

Page 33: redis überall

class  Counter  !    COUNTERS  =  %w[  VERSION  popup_shown  extra_life_given  ]        !    def  set(key,  value)          $redis.setrange(              user_id,              COUNTERS.index(key),              [value].pack("C*")          )      end  !    def  [](key)          char  =  $redis.getrange(user_id,  index,  index)          char.unpack("C*").first.to_i      end  end

set the value for a part of a string

Last: Avoiding hashes

Page 34: redis überall

class  Counter  !    COUNTERS  =  %w[  VERSION  popup_shown  extra_life_given  ]        !    def  set(key,  value)          $redis.setrange(              user_id,              COUNTERS.index(key),              [value].pack("C*")          )      end  !    def  [](key)          char  =  $redis.getrange(user_id,  index,  index)          char.unpack("C*").first.to_i      end  end

set the value for a part of a string

starting in this position

Last: Avoiding hashes

Page 35: redis überall

class  Counter  !    COUNTERS  =  %w[  VERSION  popup_shown  extra_life_given  ]        !    def  set(key,  value)          $redis.setrange(              user_id,              COUNTERS.index(key),              [value].pack("C*")          )      end  !    def  [](key)          char  =  $redis.getrange(user_id,  index,  index)          char.unpack("C*").first.to_i      end  end

set the value for a part of a string

starting in this position

turn an integer into an 8-bit unsigned char

Last: Avoiding hashes

Page 36: redis überall

Last: Avoiding hashes

Page 37: redis überall

:01  >  [0].pack(“C*”)  =>  "\x00"

Last: Avoiding hashes

Page 38: redis überall

:01  >  [0].pack(“C*”)  =>  "\x00"

:02  >  [256].pack(“C*”)  =>  "\x00"

Last: Avoiding hashes

Page 39: redis überall

:01  >  [0].pack(“C*”)  =>  "\x00"

:02  >  [256].pack(“C*”)  =>  "\x00"

Achtung, 8 bits !

Last: Avoiding hashes

Page 40: redis überall

butüberallredis

@zucaritask

use

beware of the crowds


Recommended