Scalling web applications using memcache

15
Scaling web applications using Memcache Sudar Muthu (@sudarmuthu) http://sudarmuthu.com Research Engineer Yahoo Research Chennai Geeks - Meetup 20-Nov-2010

description

 

Transcript of Scalling web applications using memcache

Page 1: Scalling web applications using memcache

Scaling web applications using Memcache

Sudar Muthu (@sudarmuthu)http://sudarmuthu.com

Research EngineerYahoo ResearchChennai Geeks - Meetup20-Nov-2010

Page 2: Scalling web applications using memcache

AgendaOverviewMemcache is notHow Memcache worksGeneral UsageMemcache LimitsClient LibrariesCommon Use casesTips for optimizationUsing Memcache in WordPress

Page 3: Scalling web applications using memcache

OverviewA high-performance Distributed

Memory Object Caching SystemIntended 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

Page 4: Scalling web applications using memcache

Memcache is not..It is not a databaseIt does not provide redundancyIt doesn't handle failoverIt does not provide

authenticationIt does not allow locksNo direct dump or backup

possible

Page 5: Scalling web applications using memcache

How Memcache worksTwo stage hash, like a giant hash

table looking up key=value pairsClient 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

Page 6: Scalling web applications using memcache

General UsageFirst 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.

Page 7: Scalling web applications using memcache

Memcache LimitsA 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.

Page 8: Scalling web applications using memcache

Client LibrariesPerl, Python, Ruby, Java, C#, CPostgreSQL (access memcached

from procs and triggers)MySQL (adds memcache_engine

storage engine)PHP (pecl/memcache)

Page 9: Scalling web applications using memcache

Client Libraries – PHP API

Key Methods bool Memcache::connect ( string $host [,

int $port [, int $timeout ]] ) bool Memcache::set( string $key , mixed

$var [, int $flag [, int $expire ]] ) string Memcache::get ( string $key [, int

&$flags ] ) bool Memcache::delete ( string $key [, int

$timeout ] )

More: http://www.php.net/memcache

Page 10: Scalling web applications using memcache

Common Use cases - 1Simple 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; 

Page 11: Scalling web applications using memcache

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 webservice url";

$result = fetch($url); $memcache->set($key, $result, false, 86400); // Store the result of the query for a day return $result;

}

Page 12: Scalling web applications using memcache

Common Use cases - 3Throttle 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;

}

Page 13: Scalling web applications using memcache

Tips for OptimizationPre 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 fragmentsReplicate data across clusters

◦Do set/delete in multiple clusters  ◦Read from either cluster

Run multiple instances on the same serverIf you know the data, implement a simple

encoding scheme

Page 14: Scalling web applications using memcache

Using Memcache in WordPressDownload Memcached Object

Cache Plugin from http://wordpress.org/extend/plugins/memcached/

Set up memcached serversInstall pecl memcache extentionCopy object-cache.php to wp-

content directory (not the Pluing’s directory)

You are done

Page 15: Scalling web applications using memcache

Questions?

Thank you