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

Redis for the Everyday Developer

Ross Tuck
August 11, 2012

Redis for the Everyday Developer

As presented at Northeast PHP. #nephp

More than some arcane NoSQL tool, Redis is a simple but powerful swiss army knife you can begin using today.

This talk introduces Redis and focuses on using it to cleanly solve common problems. Along the way, you'll learn how Redis can be used as an alternative to several common PHP tools.

Ross Tuck

August 11, 2012
Tweet

More Decks by Ross Tuck

Other Decks in Programming

Transcript

  1. Ross Tuck
    Redis For The
    Everyday Developer
    Northeast PHP
    August 11th, #nephp

    View full-size slide

  2. Team Lead at Ibuildings
    Codemonkey
    HTTP nut
    Hat guy

    View full-size slide

  3. • Created by Salvatore Sanfilippo ( @antirez )
    • Sponsored by VMware
    • 3 years old
    • redis.io

    View full-size slide

  4. “Redis is an open source, advanced
    key-value store.”

    View full-size slide

  5. “It is often referred to as
    a data structure server...”

    View full-size slide

  6. Defancypants

    View full-size slide

  7. Server A Server B
    Redis

    View full-size slide

  8. The Cool Stuff

    View full-size slide

  9. Oh wait, it's already done.

    View full-size slide

  10. 120,000~ ops per sec

    View full-size slide

  11. (optionally)
    Persistent.

    View full-size slide

  12. Replication
    Pub/Sub
    Scripting
    Transactions
    Slices
    Dices
    Makes julienne fries

    View full-size slide

  13. MySQL Redis
    +

    View full-size slide

  14. MySQL
    +
    Redis
    4ever

    View full-size slide

  15. apt-get install redis-server
    Easy but old

    View full-size slide

  16. http://redis.io/download

    View full-size slide

  17. PHP Libraries
    • 5.3+: Predis
    • 5.2: Predis or Rediska
    • C Extension: phpredis
    • redis-cli

    View full-size slide

  18. $client = new \Predis\Client();

    View full-size slide

  19. The Surprise Ending

    View full-size slide

  20. PHP
    MySQL
    Memcache

    View full-size slide

  21. PHP
    MySQL
    Memcache

    View full-size slide

  22. PHP
    MySQL
    Redis

    View full-size slide

  23. It's about 80% cases.

    View full-size slide

  24. Use Case #1: Caching

    View full-size slide

  25. Redis has commands.

    View full-size slide

  26. Commands have parameters.

    View full-size slide

  27. Think functions.

    View full-size slide

  28. $client->commandGoesHere($params, $go, $here);

    View full-size slide

  29. $client->set('ross:mood', 'nervous');
    // Later
    $client->get('ross:mood'); // returns “nervous”

    View full-size slide

  30. $client->set('ross:mood', 'nervous');
    $client->expire('ross:mood', 5);
    // 4 seconds later...
    $client->get('ross:mood'); // “nervous”
    // 5 seconds later...
    $client->get('ross:mood'); // null

    View full-size slide

  31. $client->set('ross:mood', 'nervous');
    $client->expire('ross:mood', 5);

    View full-size slide

  32. $client->setex('ross:mood', 5, 'nervous');

    View full-size slide

  33. Great for caching

    View full-size slide

  34. Use Case #2: Session Storage

    View full-size slide

  35. You already know it.

    View full-size slide

  36. Use Case #3: Hit Counters

    View full-size slide

  37. $client->incr('page:42:views');
    $client->incr('page:42:views');
    $client->incr('page:42:views');
    // 1
    // 2
    // 3

    View full-size slide

  38. Redis is hard ;)

    View full-size slide

  39. How is this better?

    View full-size slide

  40. redis-benchmark
    ====== INCR ======
    10000 requests completed in 0.08 seconds
    50 parallel clients
    3 bytes payload
    keep alive: 1
    100.00% <= 0 milliseconds
    119047.62 requests per second

    View full-size slide

  41. Fast enough.

    View full-size slide

  42. $client->incr('cookies:eaten');
    $client->incrBy('cookies:eaten', 2);
    $client->incrByFloat('cookies:eaten', 0.5); version 2.6+

    View full-size slide

  43. Use Case #4: Latest News

    View full-size slide

  44. “It is often referred to as
    a data structure server...”

    View full-size slide

  45. “...since keys can contain strings, hashes,
    lists, sets and sorted sets.”

    View full-size slide

  46. $redis = array();

    View full-size slide

  47. Generic
    Hash
    List
    Set
    Sorted Set

    View full-size slide

  48. Generic
    Hash
    List
    Set
    Sorted Set
    // strings and numbers
    $redis['ross:mood'] = "happy";
    $redis['foo'] = 9;

    View full-size slide

  49. Generic
    Hash
    List
    Set
    Sorted Set
    // associative array
    $redis['foo']['name'] = 'Bob';
    $redis['foo']['age'] = 31;
    Objects, forms, records

    View full-size slide

  50. Generic
    Hash
    List
    Set
    Sorted Set
    // not associative
    $redis['foo'][] = 'zip';
    $redis['foo'][] = 'zap';
    Lists, stacks, queues

    View full-size slide

  51. Generic
    Hash
    List
    Set
    Sorted Set
    // No dupes, no order
    shuffle(
    array_unique($redis['foo'])
    );
    Relations, stats, matching

    View full-size slide

  52. Generic
    Hash
    List
    Set
    Sorted Set
    // Ordered by *score*
    array_unique($redis['foo']);
    Curing cancer, world peace
    Sets but with order or scores

    View full-size slide

  53. Y U NO STOP
    Y U NO STOP
    LISTING THINGS
    LISTING THINGS

    View full-size slide

  54. Generic
    Hash
    List
    Set
    Sorted Set

    View full-size slide

  55. Generic
    Hash
    List
    Set
    Sorted Set

    View full-size slide

  56. // Code
    $client->lpush('news:latest', 'Aliens Attack!');
    // Redis
    ['Aliens Attack!']

    View full-size slide

  57. // Redis
    ['Takei 2012', 'Aliens Attack!']
    // 2 hours later...
    $client->lpush('news:latest', 'Takei 2012');

    View full-size slide

  58. // That evening...
    $client->lpush('news:latest', 'Eggs = Cancer!');
    // Redis
    ['Eggs = Cancer!', 'Takei 2012', 'Aliens Attack!']

    View full-size slide

  59. // Code
    $client->lpush('news:latest', 'Aliens Attack!');
    $client->lpush('news:latest', 'Takei 2012');
    $client->lpush('news:latest', 'Eggs = Cancer!');
    // Redis
    ['Eggs = Cancer!', 'Takei 2012', 'Aliens Attack!']

    View full-size slide

  60. Getting it back out?

    View full-size slide

  61. $client->lrange('news:latest', 0, 1);
    End Index
    Start Index

    View full-size slide

  62. var_dump( $client->lrange('news:latest', 0, 1) );
    array(2) {
    [0]=> string(14) "Eggs = Cancer!"
    [1]=> string(10) "Takei 2012"
    }

    View full-size slide

  63. That's it.
    Really.

    View full-size slide

  64. What about size?

    View full-size slide

  65. $client->lpush('news:latest', 'Free Jetpacks!');
    $client->lpush('news:latest', 'Elvis Found!');
    $client->lpush('news:latest', 'Takei Wins!');
    //...and so on...

    View full-size slide

  66. $client->ltrim('news:latest', 0, 2);
    // Only the three latest stories remain!

    View full-size slide

  67. or better...

    View full-size slide

  68. $client->lpush('news:latest', 'Cats Leave Euro');
    $client->ltrim('news:latest', 0, 2);

    View full-size slide

  69. Great option for notifications

    View full-size slide

  70. SELECT * FROM Articles
    INNER JOIN Authors ON (complicated joins)
    -- More joins
    WHERE (complicated logic)
    LIMIT 0, 20

    View full-size slide

  71. SELECT Articles.id FROM Articles
    INNER JOIN Authors ON (complicated joins)
    -- More joins
    WHERE (complicated logic)

    View full-size slide

  72. $client->lpush('search:a17f3', $ids);

    View full-size slide

  73. $client->lrange('search:a17f3', $limit, $offset);

    View full-size slide

  74. Use Case #5: Recently Viewed

    View full-size slide

  75. Generic
    Hash
    List
    Set
    Sorted Set

    View full-size slide

  76. Generic
    Hash
    List
    Set
    Sorted Set
    No duplicates

    View full-size slide

  77. Generic
    Hash
    List
    Set
    Sorted Set
    Needs to be ordered

    View full-size slide

  78. Goldilocks
    Just Right
    Generic
    Hash
    List
    Set
    Sorted Set

    View full-size slide

  79. $client->zadd('mykey', 1, 'mydata');
    Any integer
    or float

    View full-size slide

  80. $client->zadd('recent', 1, '/p/first');

    View full-size slide

  81. $client->zadd('recent', time(), '/p/first');

    View full-size slide

  82. $client->zadd('recent', 1338020901, '/p/first');
    $client->zadd('recent', 1338020902, '/p/second');
    $client->zadd('recent', 1338020903, '/p/third');
    $client->zadd('recent', 1338020904, '/p/fourth');

    View full-size slide

  83. Reading it back out?

    View full-size slide

  84. array(3) {
    [0]=> string(8) "/p/first"
    [1]=> string(9) "/p/second"
    [2]=> string(8) "/p/third"
    }
    $client->zrange('recent', 0, 2);

    View full-size slide

  85. $client->zrevrange('recent', 0, 2);
    Reverse
    array(3) {
    [0]=> string(9) "/p/fourth"
    [1]=> string(8) "/p/third"
    [2]=> string(9) "/p/second"
    }

    View full-size slide

  86. $client->zadd('recent', 1338020901, '/p/first');
    // later...
    $client->zadd('recent', 1338020928, '/p/first');

    View full-size slide

  87. array(3) {
    [0]=> string(8) "/p/first"
    [1]=> string(9) "/p/fourth"
    [2]=> string(8) "/p/third"
    }
    $client->zrevrange('recent', 0, 2);

    View full-size slide

  88. Other things we can do?

    View full-size slide

  89. $client->zrangebyscore('recent', $low, $high);

    View full-size slide

  90. $yesterday = time()-(60*60*24);
    $client->zrangebyscore('recent', $yesterday, '+inf');

    View full-size slide

  91. Intersections

    View full-size slide

  92. $client->zinterstore('omg', 2, 'recent', 'favorite');
    $client->zrange('omg', 0, 4);

    View full-size slide

  93. zremrangebyscore

    View full-size slide

  94. $yesterday = time()-(60*60*24);
    $client->zremrangebyscore(
    'recent', '-inf', $yesterday
    );

    View full-size slide

  95. We can do a lot.

    View full-size slide

  96. Scores can be anything.

    View full-size slide

  97. Use Case #6: Sharing Data

    View full-size slide

  98. Redis
    PHP
    Node.js Python

    View full-size slide

  99. • ActionScript
    • C
    • C#
    • C++
    • Clojure
    • Common Lisp
    • Erlang
    • Fancy
    • Go
    • Haskell
    • haXe
    • Io
    • Java
    • Lua
    • Node.js
    • Objective-C
    • Perl
    • PHP
    • Pure Data
    • Python
    • Ruby
    • Scala
    • Smalltalk
    • Tcl

    View full-size slide

  100. $client = new \Predis\Client();
    $client->set('foo', 'bar');

    View full-size slide

  101. var redis = require("redis");
    var client = redis.createClient();
    client.get("foo", redis.print);

    View full-size slide

  102. Step Further...

    View full-size slide

  103. $client->publish('broadcast', 'batkitty');

    View full-size slide

  104. client.on("message", function (channel, message) {
    console.log(channel + ": " + message);
    });
    client.subscribe("broadcast");
    // prints “broadcast: batkitty”

    View full-size slide

  105. Not everyday
    yet

    View full-size slide

  106. Use Case #7: Worker Queues

    View full-size slide

  107. $client->lpush('jobs:pending', 'clear_cache');

    View full-size slide

  108. $client->lpush('jobs:pending', '{"do":"email", …}');

    View full-size slide

  109. $client->lpush('jobs:pending', 'job:45');

    View full-size slide

  110. // worker
    $client = new \Predis\Client(array(
    'read_write_timeout' => -1
    ));
    do {
    $job = $client->brpop('jobs:pending', 0);
    doJob($job);
    } while(true);

    View full-size slide

  111. This will work.

    View full-size slide

  112. Things break.

    View full-size slide

  113. Things break.

    View full-size slide

  114. Clients break.

    View full-size slide

  115. Clients break.

    View full-size slide

  116. Clients crash.

    View full-size slide

  117. do {
    $job = $client->brpop('jobs:pending', 0);
    doJob($job);
    } while(true);

    View full-size slide

  118. Multiple keys is the redis way.

    View full-size slide

  119. 'jobs:pending'
    'jobs:working'

    View full-size slide

  120. What we need is: blocking
    rpop
    lpush

    View full-size slide

  121. brpoplpush
    No, really.

    View full-size slide

  122. do {
    $job = $client->brpoplpush(
    'jobs:pending', 'jobs:working', 0
    );
    doJob($job);
    } while(true);

    View full-size slide

  123. do {
    $job = $client->brpoplpush(
    'jobs:pending', 'jobs:working', 0
    );
    if(doJob($job)) {
    $client->lrem('jobs:working', 0, $job);
    }
    } while(true);

    View full-size slide

  124. Simple = Powerful

    View full-size slide

  125. Keep it in RAM.

    View full-size slide

  126. Redis 2.6 will be amazing.

    View full-size slide

  127. Great docs.
    Use them.

    View full-size slide

  128. Bad for the DB?
    Might be good for Redis.

    View full-size slide

  129. Bad for the DB?
    Might be good for Redis.

    View full-size slide

  130. @svdgraaf
    QuickMeme
    IAEA Kotaku

    View full-size slide

  131. http://joind.in/6829

    View full-size slide