Scaling web applications using Memcache
Memcache is a high-performance distributed memory caching system intended for speeding up dynamic web applications by reducing database load. It uses RAM for storage and acts as a dictionary of key-value pairs. Memcache is not a database and does not provide redundancy, failover, authentication or locking. It uses a two stage hashing process to look up key-value pairs across multiple servers. General usage involves first checking Memcache and if not present, querying the database, storing in Memcache, and returning the result. Common libraries exist for many languages including PHP. Common use cases include caching query results, API responses, and throttling user activity. Optimization techniques include pre-warming the cache
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Â
Scaling web applications using Memcache: How to optimize performance by caching queries and API calls in memory
1. Scaling web applications usingMemcache Sudar Muthu (@sudarmuthu) http://sudarmuthu.com Research Engineer Yahoo Research Chennai Geeks - Meetup 20-Nov-2010
2. Agenda Overview Memcache is not How Memcache works General Usage Memcache Limits Client Libraries Common Use cases Tips for optimization Using Memcache in WordPress
3. 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
4. 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
5. 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
6. 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.
7. 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.
8. Client Libraries Perl, Python, Ruby, Java, C#, C PostgreSQL (access memcached from procs and triggers) MySQL (adds memcache_engine storage engine) PHP (pecl/memcache)
13. 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; }Â
14. 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; }
15. 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; }
16. 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
17. 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 ï