Google

Saturday, April 25, 2009

Memcache for Code and Performance

"Memcache for Code and Performance "



When website has high traffic then continuos every time fetch record from db and show on the client side is tedious job and we need high performance then we use Memcache.



What is Memcache?



"memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load."



What is MemcacheDB?



It's a distributed key-value storage system designed for persistent. It is NOT a cache solution, but a persistent storage engine for fast and reliable key-value based object storage and retrieval. It conforms to memcache protocol(not completed, see below), so any memcached client can have connectivity with it. MemcacheDB uses Berkeley DB as a storing backend, so lots of features including transaction and replication are supported.



Before you get too excited keep in mind this is a key-value store. You read and write records by a single key. There aren't multiple indexes and there's no SQL. That's why it can be so fast.



The original client for memcached was written in Perl by the folks at Danga Interactive. The Python client is now maintained by a 3rd party group. The PHP client is available as a PECL package.PHP client is written in C as a PHP extension. The other two are written in Perl and Python. So, all the complex code has to be interpretted. For PHP however, most of the work happens in the engine, not in PHP code. This makes the PHP client far superior in performance to the other two. By default, memcached uses the port 11211.



The system is used by several very large, well-known sites including YouTube, LiveJournal, Slashdot, Wikipedia, SourceForge, ShowClix, GameFAQs, Facebook, Digg, Twitter, Fotolog, BoardGameGeek, NYTimes.com, deviantART, Jamendo, Kayak and in our Mobshare too.




PHP provides support for the Memcache functions through a PECL extension. To enable the PHP memcache extensions, you must build PHP using the --enable-memcache option to configure when building from source.



A few important things about memcache:



Memcache is a daemon, meaning it runs as a separate service on your machine. Just like MySQL runs as a separate service. In fact, to use memcache in PHP you have to connect to it, just like MySQL.



Think of memcache as the $_SESSION variable for PHP, but instead of it working on a per-user basis, it runs over the entire application — like MySQL. In fact, you can use memcache as you session handler for PHP.




Memcache to work on windows machine:



Download memcache [grab the 'win32 binary' version]



Install memcache as a service:



  • Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, memcached.exe] - thanks to Stephen for the heads up on the new version

  • If you’re running Vista, right click on memcached.exe and click Properties. Click the Compatibility tab. Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”.

  • Install the service using the command: c:\memcached\memcached.exe -d install from the command line

  • Start the server from the Microsoft Management Console or by running one of the following commands: c:\memcached\memcached.exe -d start, or net start "memcached Server"



Now that you have memcache installed, you’ll have to tie it in with PHP in order to use it.



1. Check your php extensions directory [should be something like: C:\php\ext] for php_memcache.dll
If you don’t have any luck finding it, try looking here: pecl4win.php.net/ext.php/php_memcache.dll [or look here for PHP 5.2.*]



2. Now find your php.ini file [default location for XP Pro is C:\WINDOWS\php.ini] and add this line to the extensions list:



extension=php_memcache.dll



3. Restart apache



4. Run this code to test the installation:



/*

$memcache = new Memcache;
$memcache->connect("localhost",11211);

"Server's version: " . $memcache->getVersion();

$tmp_object = new stdClass;
$tmp_object->str_attr = "test";
$tmp_object->int_attr = 123;

$memcache->set("key",$tmp_object,false,10);
echo "Store data in the cache (data will expire in 10 seconds)";

echo "Data from the cache:";
var_dump($memcache->get("key"));
*/

If you see anything but errors, you are now using memcache!



EDIT



Memcached, by default, loads with 64mb of memory for it’s use which is low for most applications. To change this to something else, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to look something like this:


“C:\memcached\memcached.exe” -d runservice -m 512



Now when you start the service via net start “memcached Server”, it will run with 512mb of memory at it’s disposal.



Memcache Functions:

The memcache object has several methods. With the Memcache::set method we can add an entry to the cache. The method had four arguments:

  • a key

  • any serializable variable we'd like to cache, in this case it's a string

  • a boolean whether we want to use on-the-fly zip compression using libz

  • the cache expiry measured in seconds

  • Ex: - $memcache->set("key",$tmp_object,false,10);
  • Memcache::add — Add an item to the server

  • Memcache::addServer — Add a memcached server to connection pool

  • Memcache::close — Close memcached server connection

  • Memcache::connect — Open memcached server connection

  • memcache_debug — Turn debug output on/off

  • Memcache::decrement — Decrement item's value

  • Memcache::delete — Delete item from the server

  • Memcache::flush — Flush all existing items at the server

  • Memcache::get — Retrieve item from the server

  • Memcache::getExtendedStats — Get statistics from all servers in pool

  • Memcache::getServerStatus — Returns server status

  • Memcache::getStats — Get statistics of the server

  • Memcache::getVersion — Return version of the server

  • Memcache::increment — Increment item's value

  • Memcache::pconnect — Open memcached server persistent connection

  • Memcache::replace — Replace value of the existing item

  • Memcache::setCompressThreshold — Enable automatic compression of large values

  • Memcache::setServerParams — Changes server parameters and status at runtime





The data is converted into a string with the serialize() function, so numbers, strings, arrays, and most objects will be stored easily… only resources (file descriptors, database connections, MemCache connections) can not be stored effectively.




It is important to keep in mind that if the MemCached server crashes or the process dies, all your cache data will be lost. Always store the actual data in a database server or another persistent storage system.




Conclusion



While the example we created is fairly trivial, it’s easy to see how this technique can be expanded throughout your site. Other queries and other data can be cached with similar benefits. Obviously, MemCached isn’t the only solution to web scaling and data caching, but it can be a pretty useful. Often, as seen in our example, a MemCached solution can be implemented and put in place with minimal code changes, minimal time, and, hopefully, minimum downtime for your users!