Rediscover Speed with Redis(and PHP)

14,515 views
14,243 views

Published on

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.

Published in: Technology
1 Comment
12 Likes
Statistics
Notes
No Downloads
Views
Total views
14,515
On SlideShare
0
From Embeds
0
Number of Embeds
77
Actions
Shares
0
Downloads
180
Comments
1
Likes
12
Embeds 0
No embeds

No notes for slide

Rediscover Speed with Redis(and PHP)

  1. 1. Rediscover Speed WithRedis (and PHP)Errazudin Ishak
  2. 2. AgendaAbout MeWhat on earthFor what reasonSo how to do thatCan I use it withOk, now whatSummary
  3. 3. ABOUT ME
  4. 4. Day jobStaff Engineer @ Mimos Bhd MalaysiaFocuses on web application development, deployment, performance, security and stability.
  5. 5. I was here..2009foss.my , MyGOSSCON2010Entp. PHP Techtalk, BarcampKL, PHP Meetup, MOSC2010, PHP Northwest UK, MyGOSSCON2011INTAN Tech Update, Wordpress Conf. Asia, Joomla! Day, MOSC, OWASP Day2012OWASP Appsec Asia Pacific, Australia
  6. 6. WHAT ON EARTH
  7. 7. NoSQL FamilyKey value stores : Redis, Voldemort, Cassandra
  8. 8. NoSQL FamilyColumn oriented : cass, hbase
  9. 9. NoSQL FamilyDoc collection db : CouchDB, MongoDB
  10. 10. NoSQL FamilyGraph DB : Neo4j, AllegroGraph
  11. 11. RedisREmote Dictionary ServerAdvanced Key-value storeDisk backed In-memory database (with virt mem)High write throughput (30-150k ops/sec)Data structures (list, hashes, sets, atomic ops)
  12. 12. Redis, unique?Simple, lightweightIn memory (RAM).. Fast, fast, fast (very!)Disk-backed, background writesMaster-slave config.Multi language support
  13. 13. FOR WHAT REASON
  14. 14. Source : http://goo.gl/CM7wq “Memory is the new disk. Disk is the new tape.” - Jim GraySource : http://www.infoq.com/news/2008/06/ram-is-disk
  15. 15. Issue : Write heavy workloadScaling reads : easyScaling write : headache
  16. 16. Advanced key-value storePersistenceReplicationTransactionPipeliningPublish/Subscribe
  17. 17. Use casesRealtime analyticsCaching server (memcached on steroid)Queue (scheduler, take time to process)Clicks (eg. Reddit, digg)
  18. 18. Who “We also use Redis extensively; itpowers our main feed, our activity feed, our sessions system…”
  19. 19. Who
  20. 20. Who• flickr
  21. 21. Who
  22. 22. Who
  23. 23. Who
  24. 24. SO HOW TO DO THAT
  25. 25. Get, Set.. Go!InstallationDownload, extract and compilehttp://redis.io/download…and redis-server and redis-cli, ready to rumble
  26. 26. 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
  27. 27. Data Structure server Strings SET, GET, SETRANGE, INCR, DECR Lists LPUSH, RPUSH, RPOP, LRANGE… Sets SADD, SINTER, SMEMBER, ZADD … Hashes HSET, HMSET, HGETALL
  28. 28. StringsBasic512MBAs atomic countersAppend to StringsRandom accessEncode lots with little space
  29. 29. ListsList of StringsMax length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list).
  30. 30. SetsCollection of Strings (unordered)Support a number of server side commands to compute setsMax number of members in a set is 232 - 1 (4294967295, more than 4 billion of members per set).
  31. 31. HashesMaps between string fields and string values
  32. 32. CAN I USE IT WITH …
  33. 33. PHP : Strings$src/redis-cliredis> set hello earthOKredis> get hello“earth”<?php$redis = new Redis();$redis->connect(127.0.0.1,6379);$redis->set(„hello,„earth);$stored = $redis->get(„hello);echo $stored;
  34. 34. PHP : Strings$src/redis-cliredis>set mosc"{"a":"1","b":"2"}“OKredis>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;
  35. 35. PHP : Listsredis>LPUSH mylist “earth"(integer)1redis>LPUSH mylist "hello"(integer)2//[„earth,hello]redis>LRANGE mylist 0-11)"hello“2)“earth"
  36. 36. 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]*/
  37. 37. 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]*/
  38. 38. PHP : Hashesredis>hmset firsthash a "1“ b “2“ c "3“ d "4“OKredis>hget firsthash c"3“redis>hset firsthash e "5“(integer) 1redis>hget firsthash e"5“redis>hget firsthash d"4"
  39. 39. 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);
  40. 40. 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
  41. 41. PHP : Pub/Subredis>subscribe chn801redis>subscribe chn611
  42. 42. PHP : Pub/Subredis>publish chn801 “Kelantan 6-0 Perak”redis>subscribe chn801Reading messages…1) “subscribe”2) “chn801”3) (integer) 11) “message”2) “chn801”3) “Kelantan 6-0 Perak”
  43. 43. PHP ClientsPredishttps://github.com/nrk/predisPhpredishttps://github.com/nicolasff/phpredis
  44. 44. OK, NOW WHAT
  45. 45. Source : http://goo.gl/sPZQ6“Redis is more than a key-value store, it’s a lifestyle” – Mathias Meyer
  46. 46. Redis Cluster?Target : Redis 2.6Unstable branch – Basis/fundamental partsRelease when rock solid and usefulEnd of 2012?
  47. 47. SUMMARY
  48. 48. “Different technologies excel at different things” – Weixi Yen
  49. 49. ResourcesRedis commandshttp://redis.io/commandsRedis Manifestohttp://antirez.com/post/redis- manifesto.html
  50. 50. ResourcesRedis Cookbook
  51. 51. Resourceshttp://simonwillison.net/static/2010/redis- tutorial/
  52. 52. Diving deeper?Peter Cooper’shttp://www.scribd.com/doc/33531219/Redi s-PresentationPre-orderRedis: The Definitive Guide(Data modeling, caching, andmessaging)
  53. 53. Diving deeper?Scaling Redishttp://petrohi.me/post/6323289515/scalin g-redisInstagram Engineering bloghttp://instagram-engineering.tumblr.com/

×