Scalling web applications using memcache
-
Upload
sudar-muthu -
Category
Technology
-
view
4.513 -
download
0
description
Transcript of Scalling web applications using memcache
Scaling web applications using Memcache
Sudar Muthu (@sudarmuthu)http://sudarmuthu.com
Research EngineerYahoo ResearchChennai Geeks - Meetup20-Nov-2010
AgendaOverviewMemcache is notHow Memcache worksGeneral UsageMemcache LimitsClient LibrariesCommon Use casesTips for optimizationUsing Memcache in WordPress
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
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
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
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.
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.
Client LibrariesPerl, Python, Ruby, Java, C#, CPostgreSQL (access memcached
from procs and triggers)MySQL (adds memcache_engine
storage engine)PHP (pecl/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
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;
}
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;
}
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;
}
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
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
Questions?
Thank you