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

Rediscover Speed with Redis(and PHP)

Errazudin Ishak
July 09, 2012
260

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.

Errazudin Ishak

July 09, 2012
Tweet

Transcript

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

    how to do that Can I use it with Ok, now what Summary
  2. Day job Staff Engineer @ Mimos Bhd Malaysia Focuses on

    web application development, deployment, performance, security and stability.
  3. 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
  4. 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)
  5. Redis, unique? Simple, lightweight In memory (RAM).. Fast, fast, fast

    (very!) Disk-backed, background writes Master-slave config. Multi language support
  6. Use cases Realtime analytics Caching server (memcached on steroid) Queue

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

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

  9. Who

  10. Who

  11. Who

  12. Data Structure server Strings SET, GET, SETRANGE, INCR, DECR Lists

    LPUSH, RPUSH, RPOP, LRANGE… Sets SADD, SINTER, SMEMBER, ZADD … Hashes HSET, HMSET, HGETALL
  13. Lists List of Strings Max length of a list is

    232 - 1 elements (4294967295, more than 4 billion of elements per list).
  14. 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).
  15. 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;
  16. 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;
  17. 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']*/
  18. 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']*/
  19. 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"
  20. 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);
  21. 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
  22. 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”
  23. “Redis is more than a key-value store, it’s a lifestyle”

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

    parts Release when rock solid and useful End of 2012?