Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Redis Steady Go

Redis Steady Go

Slides for a talk I gave to Ottawa Ruby about Redis, the open source data structure server by Salvatore Sanfilippo. Soon to become a screencast, too.

Peter Cooper

March 03, 2012

More Decks by Peter Cooper

Other Decks in Programming


  1. 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!
  2. in a way, but.. no “tables” no SQL no !xed

    relationships isn’t that a “database”?
  3. 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..
  4. In Ruby: a = “Hello world” b = “This is

    a test” storing some strings
  5. 1 0 1 0 1 0 1 0 like an

    assembly language? mov rax, 0x0a68732f push rax xor rax, rax mov rsi, rsp ...
  6. DSL for abstract data types and that’s from the Redis

    manifesto! google “redis manifesto”
  7. open source (BSD) !rst public release in 2009 VMware hired

    Salvatore in 2010 (and Pieter Noordhuis!)
  8. 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.
  9. 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
  10. Job queues resque does this! Push jobs onto a queue

    and forget about them (sort of..!)
  11. Session storage redis-store library can help with this for your

    Ruby webapps SET session:[session_key] “{ stuff: 123, blah: 234 }”
  12. 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.
  13. 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)
  14. 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..
  15. 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.
  16. 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.
  17. From the command line.. redis-cli SET a “foo” redis-cli GET

    a or just redis-cli for a REPL/console.
  18. From Ruby, say? require 'redis' redis = Redis.new # or..

    redis = Redis.new(:host => '', :port => 1234) redis.set "test", "hello world!" p redis.get “test” gem install redis
  19. SET mystring “hello world” ./redis-cli Redis command line client app

    GET mystring returns “hello world” ./redis-cli Key Value

    APPEND SUBSTR Works on strings that appear to be integers. Magic!
  21. f e d c b a } LRANGE 2 3

    LLEN == 6 LINDEX 5 X LREM 1 b
  22. 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!
  23. SADD contains:aba abacus SADD contains:aba cabal SADD contains:aba baba #

    .. etc .. SISMEMBER contains:aba abacus #=> 1 (or true)
  24. 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
  25. 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
  26. 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.
  27. 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
  28. SORTED SETS a.k.a. “scored” sets Like the sets we saw

    before, but with a “score” for each member too.
  29. 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!
  30. Yep, you can only use strings in a hash. No

    super duper weird recursive data structures for you!
  31. 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
  32. MULTI INCR a INCR b EXEC Careful though.. no rollbacks!

    This is when “stu!” actually happens.
  33. 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)
  34. Dump of entire database (snapshots) AOF: Append Only File Persistence

    can be tweaked.. - save 60 1000 Dumps by default, AOF can be enabled.
  35. “Redis Cluster” Lua-based scripting (in 2.6) High resolution expiry times

    (in 2.6) More introspection - imagine pub/sub for internal events