Using memcache to improve php performance

15
Using Memcache to improve PHP performance Sudar Muthu (@sudarmuthu) http://sudarmuthu.com Research Engineer Yahoo Research Bangalore PHP user group 29-May-2010

description

Find out how to improve the performance of your PHP applications using memcache. Presented at Bangalore PHP user group meetup on 29-May-2010

Transcript of Using memcache to improve php performance

Page 1: Using memcache to improve php performance

Using Memcache to improve PHP performance

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

Research EngineerYahoo ResearchBangalore PHP user group29-May-2010

Page 2: Using memcache to improve php performance

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

Page 3: Using memcache to improve php performance

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 licence”.

Uses RAM for storage Acts as a dictionary of stored

data with key /value pairs

Page 4: Using memcache to improve php performance

Memcache PrinciplesIt 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: Using memcache to improve php performance

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: Using memcache to improve php performance

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: Using memcache to improve php performance

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: Using memcache to improve php performance

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

from procs and triggers)MySQL (adds memcache_engine

storage engine)PHP (pecl/memcache)

Page 9: Using memcache to improve php performance

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: Using memcache to improve php performance

Common Use cases - 1Simple query result caching

$key = ”some:unique:key";  if ($memcache‐>get($key)) {    

return $memcache‐>get($key);  }  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: Using memcache to improve php performance

Common Use cases - 2 Caching network/webservice calls

$key = ”some:unique:key"; if ($memcache->get($key)) { return $memcache->get($key); } 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: Using memcache to improve php performance

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: Using memcache to improve php performance

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: Using memcache to improve php performance

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: Using memcache to improve php performance

Questions?

Thank you