Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Scalling web applications using memcache

on

  • 5,180 views

 

Statistics

Views

Total Views
5,180
Views on SlideShare
4,803
Embed Views
377

Actions

Likes
3
Downloads
70
Comments
0

10 Embeds 377

http://funnel.hasgeek.com 153
http://sudarmuthu.com 147
http://feeds.feedburner.com 31
http://paper.li 17
http://funnel.hasgeek.in 16
https://funnel.hasgeek.com 9
url_unknown 1
http://www.slideshare.net 1
http://twitter.com 1
http://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Scalling web applications using memcache Scalling web applications using memcache Presentation Transcript

  • Scaling web applications usingMemcache
    Sudar Muthu (@sudarmuthu)
    http://sudarmuthu.com
    Research Engineer
    Yahoo Research
    Chennai Geeks - Meetup
    20-Nov-2010
  • Agenda
    Overview
    Memcache is not
    How Memcache works
    General Usage
    Memcache Limits
    Client Libraries
    Common Use cases
    Tips for optimization
    Using Memcache in WordPress
  • Overview
    A high-performance Distributed Memory Object Caching System
    Intended for speeding up dynamic web applications by alleviating database load.
    Open source & distributed under a “permissive free software license”.
    Uses RAM for storage
    Acts as a dictionary of stored data with key /value pairs
    View slide
  • Memcache is not..
    It is not a database
    It does not provide redundancy
    It doesn't handle failover
    It does not provide authentication
    It does not allow locks
    No direct dump or backup possible
    View slide
  • How Memcache works
    Two stage hash, like a giant hash table looking up key=value pairs
    Client hashes the key against a list of servers
    When the server is identified, the client sends its request
    Server performs a hash key lookup for the actual data
  • General Usage
    First look up in memcache before querying the database.
    If present, return it.
    Else query the database, store it in memcache and return it.
    If the data changes, delete it from cache.
  • Memcache Limits
    A single value cannot contain more than 1 MB of data.
    A single key cannot be more than 250 characters in length.
    Normally a single memcache instance cannot be more than 2 GB in size.
  • Client Libraries
    Perl, Python, Ruby, Java, C#, C
    PostgreSQL (access memcached from procs and triggers)
    MySQL (adds memcache_engine storage engine)
    PHP (pecl/memcache)
  • Client Libraries – PHP API
    Key Methods
    • boolMemcache::connect ( string $host [, int $port [, int $timeout ]] )
    • boolMemcache::set( string $key , mixed $var [, int $flag [, int $expire ]] )
    • string Memcache::get ( string $key [, int &$flags ] )
    • boolMemcache::delete ( string $key [, int $timeout ] )
    More: http://www.php.net/memcache
  • Common Use cases - 1
    Simple query result caching
    $key = ”some:unique:key”;
    $value = $memcache‐>get($key);
    if ($value) {    
    return $value; 
    }  else {    
    // Run the query get the result data $result = $db‐>query(“query”);      $memcache‐>set($key, $result, TRUE, 300); 
    // Store the result of the query for 5 minutes      return $result; 

  • Common Use cases - 2
    Caching network/webservice calls
    $key = ”some:unique:key";
    $value = $memcache->get($key);
    if ($value) {
    return $value;
    } else {
    // Download the page
    $url = ”Your webserviceurl";
    $result = fetch($url);
    $memcache->set($key, $result, false, 86400);
    // Store the result of the query for a day
    return $result;
    }
  • Common Use cases - 3
    Throttle user activity
    $key = “some:unique:key”;
    if ($memcache->get($key)) {
    //Deny access
    return false;
    } else {
    $memcache->set($key, “1”, false, 60);
    //Allow access
    return true;
    }
  • Tips for Optimization
    Pre warm your cache using scripts
    Batch your requests using multi-get
    values= get(Array(“Foo”,”Bar”,”Baz”))
    Cache things other than SQL data!
    XML, HTML, Page fragments
    Replicate data across clusters
    Do set/delete in multiple clusters 
    Read from either cluster
    Run multiple instances on the same server
    If you know the data, implement a simple encoding scheme
  • Using Memcache in WordPress
    Download Memcached Object Cache Plugin from http://wordpress.org/extend/plugins/memcached/
    Set up memcached servers
    Install peclmemcacheextention
    Copy object-cache.php to wp-content directory (not the Pluing’s directory)
    You are done 
  • Questions?
    Thank you