Redis: Data Cheeseburgers 2

Redis: Data Cheeseburgers 2

Followup/redux of Redis: Data Cheesburgers!

Explaining Redis is tough, it’s easy to say “a data structures server” or “memcached on steroids” or something more jargon filled. It’s not exactly a key value store, it’s definitely not a relational or document-oriented database. Usually as programmers we have to bend our data into a table or document to save it, but with Redis we can persist data as we conceptually visualize it. You may have heard that Redis is super fast, is being used for everything from analytics to job queues, and even on large sites such as Craigslist. Learn about the basics of Redis and we'll cover some use cases so you can see how to integrate with your applications. Tasty!

Given at Scale9X. http://www.socallinuxexpo.org/scale9x/

Eb8975af8e49e19e3dd6b6b84a542e26?s=128

Nick Quaranto

January 12, 2012
Tweet

Transcript

  1. Redis: Data Cheeseburgers Nick Quaranto @qrush / nick@quaran.to

  2. thoughtbot.com i work at

  3. we use

  4. hoptoadapp.com on

  5. rubygems.org and

  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. Redis

  13. redis “an advanced key-value store”

  14. no •tables •relations •documents •graphs

  15. yes data structures

  16. a big hash _ => _

  17. really it’s just key => data structure

  18. run commands > GET foo “bar”

  19. memory •all keys & values in RAM •sync to disk

    when necessary •various levels of paranoia
  20. •data structures •getting started •use cases

  21. data structures

  22. strings •like memcached •chunk of data •binary aware

  23. > get fries (nil) > set fries over OK >

    get fries over fries over fries over fries
  24. > strlen fries 4 > getrange fries 0 1 ov

    > append fries over done over fries over fries overdone fries
  25. > rename fries chips OK > del chips 1 >

    exists chips 0 overdone chips chips chips
  26. counters •like strings, cast to an integer •atomic increment/ decrement

    •very, very fast
  27. > get burgers nil > incr burgers 1 > incrby

    burgers 41 42 burgers 1 42 burgers burgers
  28. lists •push, pop •random access •blocking actions

  29. RPUSH LPOP LPUSH RPOP thanks to peter cooper (@peterc) for

    the graphs! a b c d e
  30. hotdog fries burger > rpush order burger 1 > rpush

    order hotdog 2 > rpush order fries 3 order
  31. hotdog fries burger > lrange order 0 1 1. burger

    2. hotdog > lindex order 2 fries order
  32. hotdog fries burger > lrange order 0 -1 1. burger

    2. hotdog 3. fries order
  33. > lpop order burger hotdog fries burger order

  34. sets •unique elements •intersect, union, difference

  35. > sadd meat bacon 1 > sadd meat turkey 1

    > sadd toppings bacon 1 > sadd toppings bacon 0
  36. RPUSH meat thanks to peter cooper (@peterc) for the graphs!

    burger turkey ham capicola jerky bacon toppings relish bacon ketchup mustard pickles
  37. meat burger turkey ham capicola jerky toppings relish ketchup mustard

    pickles > sinter meat toppings 1. bacon bacon
  38. sorted sets •high score list •set algebra •ranges by score

    or rank
  39. > zadd menu 4.99 burger 1 > zadd menu 2.99

    shake 1 > zadd menu 1.99 fries 1
  40. menu burger fries shake key score 1.99 2.99 4.99

  41. menu burger fries shake key score 1.99 2.99 4.99 >

    zrange menu 0 -1 1. fries 2. shake 3. burger
  42. menu burger fries shake key score 1.99 2.99 4.99 >

    zrank menu fries 0 > zscore menu fries 1.99
  43. menu burger fries shake key score 1.99 2.99 4.99 >

    zrangebyscore menu 2 5 1. shake 2. burger
  44. menu burger fries shake key score 1.99 2.99 4.99 >

    zremrangebyscore menu 1.50 4.50 2 deleted!
  45. hashes •easier to get all keys, values •space saver

  46. orders:1 thanks to peter cooper (@peterc) for the graphs! nick

    burger john fries mike shake joe salad created_at 1298686121 > hset orders:1 nick burger 1 > hset orders:1 john fries 1
  47. orders:1 nick burger john fries mike shake joe salad created_at

    1298686121 > hget orders:1 mike shake > hlen orders:1 5 > hexists orders:1 brian 0
  48. orders:1 nick burger john fries mike shake joe salad created_at

    1298686121 > hvals orders 1. burger 2. fries 3. shake 4. salad 5. 1298686121
  49. getting started

  50. community BSD, on GitHub Awesome maintainer (antirez) IRC, wiki, mailing

    list Hosting providers: Redis TO GO, OpenRedis
  51. git clone git://github.com/antirez/redis make redis-server or... $YOUR_DISTRO’s package manager installing

  52. simple telnet interface no authorization/authentication by default AUTH password use

    appendonly & daily cron BGREWRITEAOF sysadmin
  53. depends on system and configuration ranges from 5,000 to 120,000

    ops/sec commands have O(n) complexity listed performance
  54. misc master/slave replication diskstore soon cluster support someday?

  55. use cases

  56. when? Whenever you want to store data fast that doesn't

    need to be 100% consistent. -Mathias Meyer http://www.paperplanes.de/2009/10/29/when_to_redis.html
  57. hit counter ideal with speed and INCR/INCRBY total counts =

    counters daily per URL = sorted set (ZINCRBY)
  58. on a URL hit... HTTP: GET /index.html Redis: INCR index.html

    ZINCRBY hits 1 index.html
  59. > get index.html 3910 > get pages/docs.html 2983 > get

    public/404.html 199 basic stats
  60. advanced > zrange hits 0 -1 withscores 1) "public/404.html" 2)

    "199" 3) "pages/docs.html" 4) "2983" 5) "index.html" 6) "3910"
  61. queue atomic pops = multiple workers start a job: RPUSH

    key '{"some":"data"}' worker daemon(s): BLPOP key
  62. Thanks! http://redis.io