{"id":707,"date":"2008-01-28T20:22:18","date_gmt":"2008-01-29T01:22:18","guid":{"rendered":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/01\/28\/memcache-keeping-data-in-the-handiest-place-memory"},"modified":"2008-07-22T19:13:01","modified_gmt":"2008-07-23T00:13:01","slug":"memcache-keeping-data-in-the-handiest-place-memory","status":"publish","type":"post","link":"https:\/\/www.bytebot.net\/blog\/archives\/2008\/01\/28\/memcache-keeping-data-in-the-handiest-place-memory","title":{"rendered":"Memcache, keeping data in the handiest place: memory"},"content":{"rendered":"<p>While I ducked out of Giuseppe&#8217;s miniconf talk, on MySQL Proxy (a great session, might I add &#8211; it takes up 2 slots right up until lunch), I went over to the LinuxChix miniconf, to attend a talk about <a href=\"http:\/\/conf.au.linuxchix.org\/schedule\/memcache-brendawallace\/\">memcache<\/a>, by Brenda Wallace. Brenda, works at <a href=\"http:\/\/www.catalyst.net.nz\/\">Catalyst IT<\/a>, in New Zealand &#8211; they use a lot of memcache, in the telco business.<\/p>\n<p>Memcache: volatile cache for keeping data in. Its a daemon. The code, can connect to memcache, put values in, read values, delete values. An example of how to use memcache, is given in PHP5.<\/p>\n<p>A killer feature, is the setting of expiry. You can tell it to cache for 30 seconds, and then forget about it, no worries there.<\/p>\n<p>What do you store? Database, generated content (front page of a website, just like a blog even), web service lookups (useful in telco, or say, if you&#8217;re playing with the Flickr API), LDAP, things that are far away (from the other side of the pond, etc.).<\/p>\n<p>Wikipedia made memcache famous, Twitter uses it a lot, and there are probably heaps more.<\/p>\n<p>Many APIs, and there&#8217;s a postgres client too. There&#8217;s a memcache storage engine for MySQL as well.<\/p>\n<p>Code should be written such that if its in memcache, use that, otherwise, get it from the database and put it in memcache.<\/p>\n<p>Another nifty feature, is incrementing a value &#8211; increment functions $memc-&gt;inc(&#8216;name&#8217;);. You can also read stats, to see a cache hit or miss.<\/p>\n<p>Memcache doesn&#8217;t have locks. Memcache is not atomic. There are other libraries out there to do locking, there is a known Perl library for this.<\/p>\n<p>What not to store? Remember, its completely volatile. Don&#8217;t store anything you&#8217;d be sad to lose, and make sure the real copy is safe elsewhere. There is no method to get list of keys in store. There is a 1MB limit per item, so if data is larger than that, you&#8217;re in trouble.<\/p>\n<p>Where do you run it? Remember, it is memory hungry, but CPU lite. Running memcache on the webserver is the recommended method, so beware of the security.<\/p>\n<p>There is no authentication. You just connect (no username, no password). So, when running on the web server, you probably want a firewall. In shared hosting, everyone on that host can read\/write to your memcache instance.<\/p>\n<p>No check for validity. No referential integrity, its not a database.<\/p>\n<p>There is transparent failover! So if one fails, the client just automatically connects it to another.<\/p>\n<p>Usage ideas? Communicate between layers (talk to a PHP app, from Java). Instead of squid, you can store stuff in memcache, if you want.<\/p>\n<p>Some competing technologies: Tugela &#8211; same concept, but its saved to disk, so it will survive a reboot. This is the Wikipedia fork, of memcached. Couch DB is mention, but its not really a competitor, seeing that its a document database. Lucene is another competitor, but remember, its a fast indexer, and its non-volatile.<\/p>\n<p><em>I haven&#8217;t looked into memcached much, but its quite clear, its a great technology to look at. Now the fact that you can use the MySQL storage engine, it might actually be really, interesting.<\/em><\/p>\n<p>Technorati Tags: <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/linux.conf.au\">linux.conf.au<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/lca08\">lca08<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/linuxchix\">linuxchix<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/memcached\">memcached<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>While I ducked out of Giuseppe&#8217;s miniconf talk, on MySQL Proxy (a great session, might I add &#8211; it takes up 2 slots right up until lunch), I went over to the LinuxChix miniconf, to attend a talk about memcache, by Brenda Wallace. Brenda, works at Catalyst IT, in New Zealand &#8211; they use a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[335],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-general","tag-memcached"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p4vJD-bp","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":1,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":872,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/707\/revisions\/872"}],"wp:attachment":[{"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}