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

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/

Nick Quaranto

January 12, 2012
Tweet

More Decks by Nick Quaranto

Other Decks in Programming

Transcript

  1. Redis: Data
    Cheeseburgers
    Nick Quaranto
    @qrush / [email protected]

    View Slide

  2. thoughtbot.com
    i work at

    View Slide

  3. we use

    View Slide

  4. hoptoadapp.com
    on

    View Slide

  5. rubygems.org
    and

    View Slide

  6. View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. Redis

    View Slide

  13. redis
    “an
    advanced
    key-value
    store”

    View Slide

  14. no
    •tables
    •relations
    •documents
    •graphs

    View Slide

  15. yes
    data structures

    View Slide

  16. a big hash
    _ => _

    View Slide

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

    View Slide

  18. run commands
    > GET foo
    “bar”

    View Slide

  19. memory
    •all keys & values in RAM
    •sync to disk when
    necessary
    •various levels of paranoia

    View Slide

  20. •data structures
    •getting started
    •use cases

    View Slide

  21. data structures

    View Slide

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

    View Slide

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

    View Slide

  24. > strlen fries
    4
    > getrange fries 0 1
    ov
    > append fries over
    done
    over
    fries
    over
    fries
    overdone
    fries

    View Slide

  25. > rename fries chips
    OK
    > del chips
    1
    > exists chips
    0
    overdone
    chips
    chips
    chips

    View Slide

  26. counters
    •like strings, cast to
    an integer
    •atomic increment/
    decrement
    •very, very fast

    View Slide

  27. > get burgers
    nil
    > incr burgers
    1
    > incrby burgers 41
    42
    burgers
    1
    42
    burgers
    burgers

    View Slide

  28. lists
    •push, pop
    •random access
    •blocking actions

    View Slide

  29. RPUSH
    LPOP
    LPUSH
    RPOP
    thanks to peter cooper (@peterc) for the graphs!
    a b c d e

    View Slide

  30. hotdog fries
    burger
    > rpush order burger
    1
    > rpush order hotdog
    2
    > rpush order fries
    3
    order

    View Slide

  31. hotdog fries
    burger
    > lrange order 0 1
    1. burger
    2. hotdog
    > lindex order 2
    fries
    order

    View Slide

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

    View Slide

  33. > lpop order
    burger
    hotdog fries
    burger
    order

    View Slide

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

    View Slide

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

    View Slide

  36. RPUSH
    meat
    thanks to peter cooper (@peterc) for the graphs!
    burger turkey ham
    capicola jerky bacon
    toppings relish bacon ketchup
    mustard pickles

    View Slide

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

    View Slide

  38. sorted sets
    •high score list
    •set algebra
    •ranges by score
    or rank

    View Slide

  39. > zadd menu 4.99 burger
    1
    > zadd menu 2.99 shake
    1
    > zadd menu 1.99 fries
    1

    View Slide

  40. menu
    burger
    fries
    shake
    key score
    1.99
    2.99
    4.99

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  44. menu
    burger
    fries
    shake
    key score
    1.99
    2.99
    4.99
    > zremrangebyscore
    menu 1.50 4.50
    2
    deleted!

    View Slide

  45. hashes
    •easier to get all
    keys, values
    •space saver

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  49. getting started

    View Slide

  50. community
    BSD, on GitHub
    Awesome maintainer (antirez)
    IRC, wiki, mailing list
    Hosting providers:
    Redis TO GO, OpenRedis

    View Slide

  51. git clone git://github.com/antirez/redis
    make
    redis-server
    or... $YOUR_DISTRO’s package manager
    installing

    View Slide

  52. simple telnet interface
    no authorization/authentication by default
    AUTH password
    use appendonly & daily cron BGREWRITEAOF
    sysadmin

    View Slide

  53. depends on system and configuration
    ranges from 5,000 to 120,000 ops/sec
    commands have O(n) complexity listed
    performance

    View Slide

  54. misc
    master/slave replication
    diskstore soon
    cluster support someday?

    View Slide

  55. use cases

    View Slide

  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

    View Slide

  57. hit counter
    ideal with speed and INCR/INCRBY
    total counts = counters
    daily per URL = sorted set (ZINCRBY)

    View Slide

  58. on a URL hit...
    HTTP:
    GET /index.html
    Redis:
    INCR index.html
    ZINCRBY hits 1 index.html

    View Slide

  59. > get index.html
    3910
    > get pages/docs.html
    2983
    > get public/404.html
    199
    basic stats

    View Slide

  60. advanced
    > zrange hits 0 -1 withscores
    1) "public/404.html"
    2) "199"
    3) "pages/docs.html"
    4) "2983"
    5) "index.html"
    6) "3910"

    View Slide

  61. queue
    atomic pops = multiple workers
    start a job:
    RPUSH key '{"some":"data"}'
    worker daemon(s):
    BLPOP key

    View Slide

  62. Thanks!
    http://redis.io

    View Slide