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. PHP Libraries • 5.3+: Predis • 5.2: Predis or Rediska

    • C Extension: phpredis • redis-cli
  2. SET

  3. 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
  4. Generic Hash List Set Sorted Set // strings and numbers

    $redis['ross:mood'] = "happy"; $redis['foo'] = 9;
  5. Generic Hash List Set Sorted Set // associative array $redis['foo']['name']

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

    = 'zip'; $redis['foo'][] = 'zap'; Lists, stacks, queues
  7. Generic Hash List Set Sorted Set // No dupes, no

    order shuffle( array_unique($redis['foo']) ); Relations, stats, matching
  8. Generic Hash List Set Sorted Set // Ordered by *score*

    array_unique($redis['foo']); Curing cancer, world peace Sets but with order or scores
  9. // Redis ['Takei 2012', 'Aliens Attack!'] // 2 hours later...

    $client->lpush('news:latest', 'Takei 2012');
  10. SELECT * FROM Articles INNER JOIN Authors ON (complicated joins)

    -- More joins WHERE (complicated logic) LIMIT 0, 20
  11. • 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
  12. client.on("message", function (channel, message) { console.log(channel + ": " +

    message); }); client.subscribe("broadcast"); // prints “broadcast: batkitty”
  13. // worker $client = new \Predis\Client(array( 'read_write_timeout' => -1 ));

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

    { $client->lrem('jobs:working', 0, $job); } } while(true);