Rediscover Speed with Redis(and PHP)

101132fd9b22a12e8eb7ce947cf50a87?s=47 Errazudin Ishak
July 09, 2012
190

Rediscover Speed with Redis(and PHP)

Talk presented at Malaysian Open Source Conference 2012 (09-07-12) :
Using the right tools for the right job. Yeah right! So what is your problem? Your farmville-like online games empire gaining momentum as what you never dream of? Your data read and write is close to maximum which make options like write through cache or other common scaling agendas less fun and not really effective? You’re getting tired of traditional RDBMS and all the bru-ha-ha over oldSQL, NoSQL and NewSQL? Redis might address (some) of your problems. It works with an in-memory dataset as an advanced key-value store often referred as data structure server that can runs multiple atomic operations. This session will uncover the beauty and practicality of Redis and how it works with most languages especially PHP.

101132fd9b22a12e8eb7ce947cf50a87?s=128

Errazudin Ishak

July 09, 2012
Tweet

Transcript

  1. Errazudin Ishak Rediscover Speed With Redis (and PHP)

  2. Agenda About Me What on earth For what reason So

    how to do that Can I use it with Ok, now what Summary
  3. ABOUT ME

  4. Day job Staff Engineer @ Mimos Bhd Malaysia Focuses on

    web application development, deployment, performance, security and stability.
  5. I was here.. 2009 foss.my , MyGOSSCON 2010 Entp. PHP

    Techtalk, BarcampKL, PHP Meetup, MOSC2010, PHP Northwest UK, MyGOSSCON 2011 INTAN Tech Update, Wordpress Conf. Asia, Joomla! Day, MOSC, OWASP Day 2012 OWASP Appsec Asia Pacific, Australia
  6. None
  7. WHAT ON EARTH

  8. NoSQL Family Key value stores : Redis, Voldemort, Cassandra

  9. NoSQL Family Column oriented : cass, hbase

  10. NoSQL Family Doc collection db : CouchDB, MongoDB

  11. NoSQL Family Graph DB : Neo4j, AllegroGraph

  12. Redis REmote Dictionary Server Advanced Key-value store Disk backed In-memory

    database (with virt mem) High write throughput (30-150k ops/sec) Data structures (list, hashes, sets, atomic ops)
  13. Redis, unique? Simple, lightweight In memory (RAM).. Fast, fast, fast

    (very!) Disk-backed, background writes Master-slave config. Multi language support
  14. FOR WHAT REASON

  15. Source : http://www.infoq.com/news/2008/06/ram-is-disk “Memory is the new disk. Disk is

    the new tape.” - Jim Gray Source : http://goo.gl/CM7wq
  16. Issue : Write heavy workload Scaling reads : easy Scaling

    write : headache
  17. Advanced key-value store Persistence Replication Transaction Pipelining Publish/Subscribe

  18. Use cases Realtime analytics Caching server (memcached on steroid) Queue

    (scheduler, take time to process) Clicks (eg. Reddit, digg)
  19. Who “We also use Redis extensively; it powers our main

    feed, our activity feed, our sessions system…”
  20. Who

  21. Who • flickr

  22. Who

  23. Who

  24. Who

  25. SO HOW TO DO THAT

  26. Get, Set.. Go! Installation Download, extract and compile http://redis.io/download …and

    redis-server and redis-cli, ready to rumble
  27. Get, Set.. Go! $ wget http://redis.googlecode.com/files/redis- 2.4.15.tar.gz $ tar xzf

    redis-2.4.15.tar.gz $ cd redis-2.4.15 $ make
  28. Data Structure server Strings SET, GET, SETRANGE, INCR, DECR Lists

    LPUSH, RPUSH, RPOP, LRANGE… Sets SADD, SINTER, SMEMBER, ZADD … Hashes HSET, HMSET, HGETALL
  29. Strings Basic 512MB As atomic counters Append to Strings Random

    access Encode lots with little space
  30. Lists List of Strings Max length of a list is

    232 - 1 elements (4294967295, more than 4 billion of elements per list).
  31. Sets Collection of Strings (unordered) Support a number of server

    side commands to compute sets Max number of members in a set is 232 - 1 (4294967295, more than 4 billion of members per set).
  32. Hashes Maps between string fields and string values

  33. CAN I USE IT WITH …

  34. PHP : Strings $src/redis-cli redis> set hello earth OK redis>

    get hello “earth” <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->set(„hello',„earth'); $stored = $redis->get(„hello'); echo $stored;
  35. PHP : Strings $src/redis-cli redis>set mosc"{\"a\":\"1\",\"b\":\"2\"}“ OK redis>get mosc "{\"a\":\"1\",\"b\":\"2\"}“

    <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $data = array('a'=>'1','b'=>'2'); $json = json_encode($data); $redis->set(„mosc',$json); $stored = $redis->get(„mosc'); echo $stored;
  36. PHP : Lists redis>LPUSH mylist “earth"(integer)1 redis>LPUSH mylist "hello"(integer)2 //[„earth','hello']

    redis>LRANGE mylist 0-1 1)"hello“ 2)“earth"
  37. PHP : Lists <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->delete('key1');

    $redis->rpush('key1','A');//returns 1 $redis->rpush('key1','B');//returns 2 $redis->rpush('key1','C');//returns 3 $redis->rpush('key1','A');//returns 4 /*key1nowpointstothefollowinglist:['A','B','C','A']*/
  38. PHP : Sets <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->delete('key2');

    $redis->sadd('key2','A');//returns 1 $redis->sadd('key2','B');//returns 2 $redis->sadd('key2','C');//returns 3 $redis->sadd('key2','A');//returns false /*key2nowpointstothefollowinglist:['A','B','C']*/
  39. PHP : Hashes redis>hmset firsthash a "1“ b “2“ c

    "3“ d "4“ OK redis>hget firsthash c "3“ redis>hset firsthash e "5“ (integer) 1 redis>hget firsthash e "5“ redis>hget firsthash d "4"
  40. PHP : Hashes <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->delete('user:1');

    $redis->hmset('user:1', array('name'=>„Zack','salary'=>3000)); //Give Zack a $200 Raise: $redis->hincrby('user:1','salary',200);
  41. PHP : Hashes <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->hmset('user:2',

    array('name'=>„Ali','salary'=>5000)); $redis->hmset('user:3', array('name'=>„Jonah','salary'=>6000)); $uid=3; $user=$redis->hgetall('user:'.$uid) //{name:„Jonah',salary:6000} echo $user['salary'];//6000
  42. PHP : Pub/Sub redis>subscribe chn801 redis>subscribe chn611

  43. PHP : Pub/Sub redis>publish chn801 “Kelantan 6-0 Perak” redis>subscribe chn801

    Reading messages… 1) “subscribe” 2) “chn801” 3) (integer) 1 1) “message” 2) “chn801” 3) “Kelantan 6-0 Perak”
  44. PHP Clients Predis https://github.com/nrk/predis Phpredis https://github.com/nicolasff/phpredis

  45. OK, NOW WHAT

  46. “Redis is more than a key-value store, it’s a lifestyle”

    – Mathias Meyer Source : http://goo.gl/sPZQ6
  47. Redis Cluster? Target : Redis 2.6 Unstable branch – Basis/fundamental

    parts Release when rock solid and useful End of 2012?
  48. SUMMARY

  49. “Different technologies excel at different things” – Weixi Yen

  50. Resources Redis commands http://redis.io/commands Redis Manifesto http://antirez.com/post/redis- manifesto.html

  51. Resources Redis Cookbook

  52. Resources http://simonwillison.net/static/2010/redis- tutorial/

  53. Diving deeper? Peter Cooper’s http://www.scribd.com/doc/33531219/Redi s-Presentation Pre-order Redis: The Definitive

    Guide (Data modeling, caching, and messaging)
  54. Diving deeper? Scaling Redis http://petrohi.me/post/6323289515/scalin g-redis Instagram Engineering blog http://instagram-engineering.tumblr.com/

  55. None