{"id":784,"date":"2008-04-14T19:21:47","date_gmt":"2008-04-15T00:21:47","guid":{"rendered":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial"},"modified":"2008-07-22T19:13:42","modified_gmt":"2008-07-23T00:13:42","slug":"memcached-and-mysql-tutorial","status":"publish","type":"post","link":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial","title":{"rendered":"Memcached and MySQL tutorial"},"content":{"rendered":"<p>Memcached by Brian Aker, Alan Kasindorf (dormando). Here are some quick, somewhat sparse notes. Follow the slides, it will help.<\/p>\n<p>Slides: <a href=\"http:\/\/download.tangent.org\/talks\/Memcached%20Study.pdf\">http:\/\/download.tangent.org\/talks\/Memcached%20Study.pdf<\/a><\/p>\n<p>Memcached was actually created for LiveJournal. It has evolved a bit over time. Chaos to user based clustering, and then Brad implemented memcached. LiveJournal has about 30GB of cache available between 8-12 machines. The DB reads were down like 10x the moment they started using memcached (its much better now).<\/p>\n<p>Its not only for simple objects (not just a single row)- you can use it for complex queries, and the result can be stored in memcached. Eins.de, Patrick Lenz, is also the freshmeat.net guy. He put memcached on the same machine as the MySQL database server (he has 32-bit machines, and MySQL can only use a certain amount of RAM, so the rest was for memcached). This is definitely not the recommended way. Have separate memcached servers.<\/p>\n<p>PatG comes up to talk about Grazr, which is more of a write-through cache. Refer to Page 8 of the slides. Now, the thought is that maybe Pat should&#8217;ve used gearman, rather than writing their own software. Memcached has allowed them to do it asynchronously. They&#8217;re using bulk inserts now as well.<\/p>\n<p>DownUnder GeoSolutions uses lustre, which is a clustering filesystem. They&#8217;re not a web-based solution. They extract data off lustre, store it in memcached. Processing happens on the memcached RRU.<\/p>\n<p>memcached by itself does very little. There&#8217;s a simple daemon, and it responds to gets\/sets\/add\/replace. It sits on top of a very simple slab allocator. Everytime you called it, it ran malloc() and it would free() it when done, during the early days. So, now, it makes one slab allocator for different types of objects.<\/p>\n<p>memcached is event based. libevent is a generic wrapper around epoll\/kqueue, and its very scalable for network connections. 10,000 connections to a memcached, is ok &#8211; it only cares for how many of them are &#8220;active&#8221;.<\/p>\n<p>The protocol is very simple. Everyone hates it, but everyone uses it. You can even fire up telnet to talk to memcached. Its very easy to write to protocol.txt and to talk to it.<\/p>\n<p>memcached? A big stupid hash table. In a grid, its a distributed hash table. memcached is 2 hash tables &#8211; from client, and one in the server. 30 memcached&#8217;s don&#8217;t need to know about each other &#8211; they&#8217;re blind from each other. There is no cross traffic. You just add more servers, to scale up.<\/p>\n<p>Clients hash keys to the server list. Take a single key (250 bytes max), the client hashes it. You have a value, you want to access it, here&#8217;s a key. There is multiple hashing going on, as some clients do things like compressing data.<\/p>\n<p>How do I dump data? You don&#8217;t. Its a cache.<\/p>\n<p>How is it redundant? Its not. The server itself doesn&#8217;t know about other servers around it! PECL and the next version of libmemcached will understand replication. The redundancy happens in clients.<\/p>\n<p>How does it handle failover? It doesn&#8217;t. If it dies, it dies. A client can of course, handle it.<\/p>\n<p>How does it authenticate? It doesn&#8217;t at all. Don&#8217;t stick one of this, open faced, to the Internet &#8211; when you connect to it, you have full access to any commands in the server and all contents in the server. You don&#8217;t want folk just typing flush in the server ;)<\/p>\n<p>A very simple service, very simple server.<\/p>\n<p>Details on the Server? Page 14, is pretty much all the commands you can use in memcached. You can run this from telnet, even<br \/>\n&#8211; set operation throws data inside memcached (it doesn&#8217;t care if there&#8217;s other data in it)<br \/>\n&#8211; add is lightly atomic &#8211; it won&#8217;t add data that is already there<br \/>\n&#8211; stats can give you particular pieces of information, or give you a full dump. Hit ratio, cache efficiency, and lots more, can come out of this<\/p>\n<p>All drivers you are seeing, are just basically extending all these commands. cas (compare and swap atomic!) today is pretty limited<\/p>\n<p>memcached can even run on FreeBSD 4. Most people run memcached on Linux. No one has deployed memcahced on OSX in the audience.<\/p>\n<p>There&#8217;s MySQL integration. Most users grab object from database,<br \/>\nstore object to memcached. The UDF memcached functions are probably the most successful UDF in MySQL&#8217;s history :)<\/p>\n<p>There&#8217;s pgmemcache() for Postgresql, but not much is known about it<\/p>\n<p>Apache &#8211; mod_memcached, has CAS operations exposed. Different to the lighttpd implementation.<\/p>\n<p>There are limitations (page 23). If you wanted to change things, you can recompile memcached, but you might not want to do that. Largest slab class in the system, is 1 megabyte. So data size is under 1 megabyte. Beware if you&#8217;re running on a 32-bit system (going over 4GB and you will segfault). A 64-bit system should be fine, in general.<\/p>\n<p>memcached supports threads, thanks largely to Facebook. You probably don&#8217;t need this, unless you are Facebook. Memcached&#8217;s CPU footprint is tiny.<\/p>\n<p>If you gave memcached 16GB, you will not get your memory back, even if you run flush. The memory is permanently allocated from the OS (much like how Vista does things?). There is mlockall() support, so you can guarantee there will be no paging. Or just disable swap.<\/p>\n<p>jallspaw: memcached1: 22:02:00 up 992 days, 11:57,  0 users,  load average: 0.35, 0.37, 0.37<\/p>\n<p>(posted on IRC at #mysqlconf). memcached hardly every crashes.<\/p>\n<p>You can disable the LRU if you want (there&#8217;s a command line option for this).<\/p>\n<p>Hashing comes in 2 flavours &#8211; normal and consistent hashing. All drivers support CRC today.<\/p>\n<p>A consistent hash means, that instead of doing a modular divide, you can interlace among many servers across the network. When you have a 100 servers running and add a server into the network, you want to add a server, and not lose the entire cache network at once.<\/p>\n<p>libmemcached can do replicas, so it can take data from servers, and apply it to the ring. So if a server is taken out of the network, it can be found elsewhere on the ring. You can keep these networks up and running, and easily growing, with new servers, without losing cache coherency.<\/p>\n<p>Don&#8217;t only look at the return value, look at the fact that zero may actually be a credible value, even. An actual value of zero, versus a &#8220;we didn&#8217;t find anything&#8221; is very different.<\/p>\n<p>Slide 35, the ghetto locking implementation for memcache-client. Creates a pseudo-lock around a process. You&#8217;re the only process thats processing this area, so you add a key lock, where you ensure you test for nil, not zero (you&#8217;re testing for the existence of the lock). If your process dies, someone else will try in 30 seconds (lock expire). Add will only work if there&#8217;s no key existing at that point (remember, an add is not a set).<\/p>\n<p>PHP is probably the best supported language, for memcached. PECL memcached library is C backed, standard, and works fine. libmemcached will probably take over most of its features, eventually, but its not there yet now.<\/p>\n<p>Default, if you call increment by a key, it bumps by one. You can also step it up instead of 1, say like 500 or something. Refer to slide 41. Just like you can increment a key, you can decrement also.<\/p>\n<p>C\/libmemcached. C driver, there&#8217;s a C++ wrapper. Sync and async cached keys. It supports replication through the network. Has read through cache support.<\/p>\n<p>You can not only store a value, but you can also store flags. Flags to keep track of generations, keep track of MIME type internally (so not only store object type, but MIME type). This is unique for libmemcached. Most other drivers use this flags value to see if its compressed or not (the flag = 1 for compression, 0 for no).<\/p>\n<p>Multiget is 7-9x faster than just a get. Look at Page 48 for an example.<\/p>\n<p>Memcached for MySQL? Uses the UDF API. You can now incorporate most of the memcached stuff, in the SQL server, so you can do deletions and get operations easily.<\/p>\n<p><a href=\"http:\/\/tangent.org\/586\/Memcached_Functions_for_MySQL.html\">http:\/\/tangent.org\/586\/Memcached_Functions_for_MySQL.html<\/a><\/p>\n<p>What do you think about persistent connections? Use them. libevent supports them.<\/p>\n<p>Spaces to watch: MogileFS. HyperTable. HBase. People have stopped talking about POSIX filesystems, and are more talking about object filesystems. Its what all the cool kids are doing.<\/p>\n<p>Technorati Tags: <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/memcached\">memcached<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/libmemcached\">libmemcached<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/brian%20aker\">brian aker<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/alan%20kasindorf\">alan kasindorf<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/dormando\">dormando<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/krow\">krow<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/livejournal\">livejournal<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/grazr\">grazr<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/mysql\">mysql<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/mysqluc08\">mysqluc08<\/a>, <a class=\"performancingtags\" rel=\"tag\" href=\"http:\/\/technorati.com\/tag\/mysqluc2008\">mysqluc2008<\/a><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"mailto:?subject=%5BShared%20Post%5D%20Memcached%20and%20MySQL%20tutorial&body=http%3A%2F%2Fwww.bytebot.net%2Fblog%2Farchives%2F2008%2F04%2F14%2Fmemcached-and-mysql-tutorial&share=email\" target=\"_blank\" title=\"Click to email a link to a friend\" data-email-share-error-title=\"Do you have email set up?\" data-email-share-error-text=\"If you&#039;re having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.\" data-email-share-nonce=\"a96354e250\" data-email-share-track-url=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial?share=email\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-784\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-784\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-784\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Memcached by Brian Aker, Alan Kasindorf (dormando). Here are some quick, somewhat sparse notes. Follow the slides, it will help. Slides: http:\/\/download.tangent.org\/talks\/Memcached%20Study.pdf Memcached was actually created for LiveJournal. It has evolved a bit over time. Chaos to user based clustering, and then Brad implemented memcached. LiveJournal has about 30GB of cache available between 8-12 machines. [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"mailto:?subject=%5BShared%20Post%5D%20Memcached%20and%20MySQL%20tutorial&body=http%3A%2F%2Fwww.bytebot.net%2Fblog%2Farchives%2F2008%2F04%2F14%2Fmemcached-and-mysql-tutorial&share=email\" target=\"_blank\" title=\"Click to email a link to a friend\" data-email-share-error-title=\"Do you have email set up?\" data-email-share-error-text=\"If you&#039;re having problems sharing via email, you might not have email set up for your browser. You may need to create a new email yourself.\" data-email-share-nonce=\"a96354e250\" data-email-share-track-url=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial?share=email\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-784\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-784\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-784\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/www.bytebot.net\/blog\/archives\/2008\/04\/14\/memcached-and-mysql-tutorial?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[23],"tags":[335],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4vJD-cE","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":874,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/07\/22\/memcached-and-mysql-webminar-from-a-web-20-company","url_meta":{"origin":784,"position":0},"title":"Memcached and MySQL: webminar from a Web 2.0 company","date":"22\/7\/2008","format":false,"excerpt":"At OSCON, Brian and Dormando gave their ever famous talk, Memcached and MySQL: Everything You Need To Know. I didn't attend the tutorial, but they assured me it was similar to what was given at the MySQL Conference 2008 (everything, but the very nice buttons dormando was giving out with\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1432,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2009\/04\/17\/frank-mashraqi-on-hadoop-memcached-and-why-the-mysql-conference-is-cool","url_meta":{"origin":784,"position":1},"title":"Frank Mashraqi on Hadoop, memcached, and why the MySQL Conference is cool","date":"17\/4\/2009","format":false,"excerpt":"Today I spoke with Farhan \"Frank\" Mashraqi, former Fotolog DBA, now working at a startup, NetEdge, working on social analytics. He's talking about the two sessions he's giving next week at the MySQL Conference & Expo 2009, as well as the benefits of being at the MySQL Conference & Expo.\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":707,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2008\/01\/28\/memcache-keeping-data-in-the-handiest-place-memory","url_meta":{"origin":784,"position":2},"title":"Memcache, keeping data in the handiest place: memory","date":"28\/1\/2008","format":false,"excerpt":"While I ducked out of Giuseppe's miniconf talk, on MySQL Proxy (a great session, might I add - 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\u2026","rel":"","context":"In &quot;General&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":544,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2007\/05\/02\/scaling-mysql-presentations","url_meta":{"origin":784,"position":3},"title":"Scaling MySQL presentations","date":"2\/5\/2007","format":false,"excerpt":"Everyone likes to scale - Peter Van Dijck has got some top 10 presentations listed - Twitter, Flickr, LiveJournal, Six Apart (Vox), Last.fm, SlideShare, etc. Guess what these sites are all generally backed by? You guessed right - go MySQL. I however didn't know that Bloglines was backed by Sleepycat.\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":607,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2007\/08\/08\/high-scalability","url_meta":{"origin":784,"position":4},"title":"High Scalability","date":"8\/8\/2007","format":false,"excerpt":"I think anyone into databases, Linux and so on (i.e. the LAMP stack), will benefit from the High Scalability website. Heck, any web developer will. Its well tagged (say, you're into mysql or memcached), has a lot of content, and links to other sites that it gets its summaries\/information from.\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1706,"url":"http:\/\/www.bytebot.net\/blog\/archives\/2010\/03\/03\/mysql-ecosystem-complementary-talks-at-the-conference","url_meta":{"origin":784,"position":5},"title":"MySQL Ecosystem &#8211; complementary talks at the conference?","date":"3\/3\/2010","format":false,"excerpt":"Its times like this, I want to hear from the greater community - the ones that are reading say, Planet MySQL or Planet MariaDB. MySQL to me, and many others is an ecosystem. We've had for the longest time, complementary technology talks, like for memcached (which have been popular, filled\u2026","rel":"","context":"In &quot;MySQL&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/784"}],"collection":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/comments?post=784"}],"version-history":[{"count":1,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/784\/revisions"}],"predecessor-version":[{"id":873,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/posts\/784\/revisions\/873"}],"wp:attachment":[{"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/media?parent=784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/categories?post=784"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.bytebot.net\/blog\/wp-json\/wp\/v2\/tags?post=784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}