Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scalling web applications using memcache

5,326 views

Published on

Published in: Technology
  • Be the first to comment

Scalling web applications using memcache

  1. 1. Scaling web applications usingMemcache<br />Sudar Muthu (@sudarmuthu)<br />http://sudarmuthu.com<br />Research Engineer<br />Yahoo Research<br />Chennai Geeks - Meetup<br />20-Nov-2010<br />
  2. 2. Agenda<br />Overview<br />Memcache is not<br />How Memcache works<br />General Usage<br />Memcache Limits<br />Client Libraries<br />Common Use cases<br />Tips for optimization<br />Using Memcache in WordPress<br />
  3. 3. Overview<br />A high-performance Distributed Memory Object Caching System<br />Intended for speeding up dynamic web applications by alleviating database load.<br />Open source & distributed under a “permissive free software license”.<br />Uses RAM for storage<br /> Acts as a dictionary of stored data with key /value pairs <br />
  4. 4. Memcache is not..<br />It is not a database<br />It does not provide redundancy<br />It doesn't handle failover<br />It does not provide authentication<br />It does not allow locks<br />No direct dump or backup possible<br />
  5. 5. How Memcache works<br />Two stage hash, like a giant hash table looking up key=value pairs<br />Client hashes the key against a list of servers <br />When the server is identified, the client sends its request <br />Server performs a hash key lookup for the actual data <br />
  6. 6. General Usage<br />First look up in memcache before querying the database.<br />If present, return it.<br />Else query the database, store it in memcache and return it.<br />If the data changes, delete it from cache.<br />
  7. 7. Memcache Limits<br />A single value cannot contain more than 1 MB of data.<br />A single key cannot be more than 250 characters in length.<br />Normally a single memcache instance cannot be more than 2 GB in size.<br />
  8. 8. Client Libraries<br />Perl, Python, Ruby, Java, C#, C<br />PostgreSQL (access memcached from procs and triggers)<br />MySQL (adds memcache_engine storage engine)<br />PHP (pecl/memcache)<br />
  9. 9. Client Libraries – PHP API<br />Key Methods<br /><ul><li>boolMemcache::connect ( string $host [, int $port [, int $timeout ]] )
  10. 10. boolMemcache::set( string $key , mixed $var [, int $flag [, int $expire ]] )
  11. 11. string Memcache::get ( string $key [, int &$flags ] )
  12. 12. boolMemcache::delete ( string $key [, int $timeout ] )</li></ul>More: http://www.php.net/memcache<br />
  13. 13. Common Use cases - 1<br />Simple query result caching<br />$key = ”some:unique:key”;<br />$value = $memcache‐>get($key);<br />if ($value) {    <br /> return $value;  <br />}  else {    <br /> // Run the query get the result data $result = $db‐>query(“query”);      $memcache‐>set($key, $result, TRUE, 300); <br /> // Store the result of the query for 5 minutes      return $result;  <br />} <br />
  14. 14. Common Use cases - 2<br />Caching network/webservice calls<br />$key = ”some:unique:key";<br />$value = $memcache->get($key);<br />if ($value) {<br /> return $value; <br />} else { <br /> // Download the page <br /> $url = ”Your webserviceurl";<br /> $result = fetch($url); <br /> $memcache->set($key, $result, false, 86400); <br /> // Store the result of the query for a day <br /> return $result; <br />} <br />
  15. 15. Common Use cases - 3<br />Throttle user activity <br />$key = “some:unique:key”; <br />if ($memcache->get($key)) {<br /> //Deny access <br /> return false; <br />} else { <br /> $memcache->set($key, “1”, false, 60); <br /> //Allow access <br /> return true; <br />} <br />
  16. 16. Tips for Optimization<br />Pre warm your cache using scripts <br />Batch your requests using multi-get <br />values= get(Array(“Foo”,”Bar”,”Baz”))<br />Cache things other than SQL data! <br />XML, HTML, Page fragments<br />Replicate data across clusters<br />Do set/delete in multiple clusters  <br />Read from either cluster<br />Run multiple instances on the same server<br />If you know the data, implement a simple encoding scheme<br />
  17. 17. Using Memcache in WordPress<br />Download Memcached Object Cache Plugin from http://wordpress.org/extend/plugins/memcached/<br />Set up memcached servers<br />Install peclmemcacheextention<br />Copy object-cache.php to wp-content directory (not the Pluing’s directory)<br />You are done <br />
  18. 18. Questions?<br />Thank you<br />

×