|
TutorialCachingStory
This is a story of Caching
ed note: this is an overview of basic memcached use case, and how memcached clients work Two plucky adventurers, Programmer and Sysadmin, set out on a journey. Together they make websites. Websites with webservers and databases. Users from all over the Internet talk to the webservers and ask them to make pages for them. The webservers ask the databases for junk they need to make the pages. Programmer codes, Sysadmin adds webservers and database servers. One day the Sysadmin realizes that their database is sick! It's spewing bile and red stuff all over! Sysadmin declares it has a fever, a load average of 20! Programmer asks Sysadmin, "well, what can we do?" Sysadmin says, "I heard about this great thing called memcached. It really helped livejournal!" "Okay, let's try it!" says the Programmer. Our plucky Sysadmin eyes his webservers, of which he has six. He decides to use three of them to run the 'memcached' server. Sysadmin adds a gigabyte of ram to each webserver, and starts up memcached with a limit of 1 gigabyte each. So he has three memcached instances, each can hold up to 1 gigabyte of data. So the Programmer and the Sysadmin step back and behold their glorious memcached! "So now what?" they say, "it's not DOING anything!" The memcacheds aren't talking to anything and they certainly don't have any data. And NOW their database has a load of 25! Our adventurous Programmer grabs the pecl/memcache client library manual, which the plucky Sysadmin has helpfully installed on all SIX webservers. "Never fear!" he says. "I've got an idea!" He takes the IP addresses and port numbers of the THREE memcacheds and adds them to an array in php. $MEMCACHE_SERVERS = array(
"10.1.1.1", //web1
"10.1.1.2", //web2
"10.1.1.3", //web3
);Then he makes an object, which he cleverly calls '$memcache'. $memcache = new Memcache();
foreach($MEMCACHE_SERVERS as $server){
$memcache->addServer ( $server );
}Now Programmer thinks. He thinks and thinks and thinks. "I know!" he says. "There's this thing on the front page that runs SELECT * FROM hugetable WHERE timestamp > lastweek ORDER BY timestamp ASC LIMIT 50000; and it takes five seconds!" "Let's put it in memcached," he says. So he wraps his code for the SELECT and uses his $memcache object. His code asks: Are the results of this select in memcache? If not, run the query, take the results, and PUT it in memcache! Like so: $huge_data_for_front_page = $memcache->get("huge_data_for_front_page");
if($huge_data_for_front_page === false){
$huge_data_for_front_page = array();
$sql = "SELECT * FROM hugetable WHERE timestamp > lastweek ORDER BY timestamp ASC LIMIT 50000";
$res = mysql_query($sql, $mysql_connection);
while($rec = mysql_fetch_assoc($res)){
$huge_data_for_frong_page[] = $rec;
}
// cache for 10 minutes
$memcache->set("huge_data_for_front_page", $huge_data_for_front_page, 0, 600);
}
// use $huge_data_for_front_page how you pleaseProgrammer pushes code. Sysadmin sweats. BAM! DB load is down to 10! The website is pretty fast now. So now, the Sysadmin puzzles, "What the HELL just happened!?" "I put graphs on my memcacheds! I used cacti, and this is what I see! I see traffic to one memcached, but I made three :(." So, the Sysadmin quickly learns the ascii protocol and telnets to port 11211 on each memcached and asks it: Hey, 'get huge_data_for_front_page' are you there? The first memcached does not answer... The second memcached does not answer... The third memcached, however, spits back a huge glob of crap into his telnet session! There's the data! Only once memcached has the key that the Programmer cached! Puzzled, he asks on the mailing list. They all respond in unison, "It's a distributed cache! That's what it does!" But what does that mean? Still confused, and a little scared for his life, the Sysadmin asks the Programmer to cache a few more things. "Let's see what happens. We're curious folk. We can figure this one out," says the Sysadmin. "Well, there is another query that is not slow, but is run 100 times per second. Maybe that would help," says the Programmer. So he wraps that up like he did before. Sure enough, the server loads drops to 8! So the Programmer codes more and more things get cached. He uses new techniques. "I found them on the list and the faq! What nice blokes," he says. The DB load drops; 7, 5, 3, 2, 1! "Okay," says the Sysadmin, "let's try again." Now he looks at the graphs. ALL of the memcacheds are running! All of them are getting requests! This is great! They're all used! So again, he takes keys that the Programmer uses and looks for them on his memcached servers. 'get this_key' 'get that_key' But each time he does this, he only finds each key on one memcached! Now WHY would you do this, he thinks? And he puzzles all night. That's silly! Don't you want the keys to be on all memcacheds? "But wait", he thinks "I gave each memcached 1 gigabyte of memory, and that means, in total, I can cache three gigabytes of my database, instead of just ONE! Oh man, this is great," he thinks. "This'll save me a ton of cash. Brad Fitzpatrick, I love your ass!" "But hmm, the next problem, and this one's a puzzler, this webserver right here, this one runing memcached it's old, it's sick and needs to be upgraded. But in order to do that I have to take it offline! What will happen to my poor memcache cluster? Eh, let's find out," he says, and he shuts down the box. Now he looks at his graphs. "Oh noes, the DB load, it's gone up in stride! The load isn't one, it's now two. Hmm, but still tolerable. All of the other memcacheds are still getting traffic. This ain't so bad. Just a few cache misses, and I'm almost done with my work. So he turns the machine back on, and puts memcached back to work. After a few minutes, the DB load drops again back down to 1, where it should always be. "The cache restored itself! I get it now. If it's not available it just means a few of my requests get missed. But it's not enough to kill me. That's pretty sweet." So, the Programmer and Sysadmin continue to build websites. They continue to cache. When they have questions, they ask the mailing list or read the faq again. They watch their graphs. And all live happily ever after. Author: Dormando via IRC. Edited by Brian Moon for fun. Further fun editing by Emufarmers. This story has been illustrated by the online comic TOBlender.com. |
Nice story :)
gr8 idea of imparting knowledge via story. It is really interesting to read it as a story. Good one.
I remember this history, greetings to David the plucky cat and Chris the plucky emo.
what is "plucky"? it's synonymous of squirrel?
Beautiful, thanks a lot!
It is the best way to understand memcached. Thank You!
Excellent way of explaining the basics! Loved it! Sweeeeet!
Excellent Article and the way it is written.I wish if you could write a complete book on this topic like that, it will be a best seller.
clever, informative and very well written. Cheers
Wonderful and easy to understand!
Interesting story and solution, blankITsolution 2009
I get it.
Thank you for this great story. Now, i get it ;)
And me too :)
What if I don't have a frong page... will it still work? ;-)
600 seconds are 10 minutes, not 5
Awesome!
Ah. Does this me memcached is worthless if I only have one server?
Good story, tutoring vividly!
I love the story
I enjoyed the story. Nice.
lol, awesome! @jeffreyd.davis You still get the great advantage of storing everything in memory.
perfect story! I like it. and isabella@isapilates.com
Great story dude!!
sweetheart loved it!!!!!! knowledge shd be spread in such cheesy way!!!!!
The best way to explain it! Thanks Guys!
Great story,maybe i've understood.
First time i read a tutorial as 'story', and I think is great! Good job guys!
Interesting approach
Great story, I get it, thanks guys anthonyle81@gmail.com
My story is almost thé same except I use memcached to store results from a supposed fast web service! I had a lost of time out errors, memcached save me! Thx for this great tool :)
Nice way to explain the basics of memcache.
Good approach to introduce the basics of memcached
hihihi i like the story and the way the message gets explained.
liked the story......lemme try practically....
This was actually quite a bit of help and it was interesting to read :) Brilliant
I like it :)
great guys...
:) great story! But like the sysAdmin, I am still wondering why wouldn't all the memcached servers share data. Why is the cached data discreetly residing on only one server at a time - I get it that author says it gives us ability to cache more data but isn't the purpose here not only cache data but also prevent session/state loss? Or is the session/state loss a tradeoff for achieving more caching?
But the story never does answer the sysadmin's question, namely: if his three memcached servers are "a distributed cache", then why is it that his "get" command via telnet on 2 of the three servers does not show the data for any given key? The story never does give the answer as far as I can tell.
http://myjavastuffs.blogspot.com/2010_03_01_archive.html
i have learned what's memcached with my poor english because of this nice story.
thanks guys.
The KISS rule is always cool.. Keep It Simple and Stupid. Thanks a million.
well done. awesome!!
This is Wonderful!!! I love it! If all tech things are explained via a story, man, everybody will be in love with tech books.
LOVE IT!!
EVERYTHING on the interwebs should be written like this
GREAT
the best story ever! I like it :D
got the nail in the head!
Should 'get huge_data_for_front_page' be 'get huge_data_for_frong_page' ?
What a fun story ... what a good tutorial ...
I couldn't figure out what one is supposed to use memcached for and now I totally get it. THANK YOU!
When memcache instances are used in a cluster mode, it behaves just like having one giant single memcache instance. Therefore, if you save something, it will end up in only of the memcache instance.
memcached rocks!
Nice story, I ask the pluncky programmer and sysadmin my client can be also .net or java?
Interesting, but the big question for me after reading is is "How does it ensure that the object ends up in only one cache?" - I can guess, in which case it sounds such an interesting solution.
nice getting started in a funny way..
I've memcached this story!
Love this story wish they could do a lot of this kind when explaining.. :)
nice and impressive story! well done dude!
nice and impressive story! well done dude!
Cool story!
A farfetched story to introduce Memcached!
really a easy understanding story about memcached,thanks for your great job!
very good!
Very usefull, really I will like to try out with, I have a system with 24 processors, and Im getting a lot of load arround 24~ , olmost all of it from MySQL
great story
awsm!!
I couldn't understand how to build a pool of memcacheds and get them to replicate until I read this and discovered its distributed. Thanks!
This is the best tutorial I have ever read!!! Great job guys!!!
awesome :)
g8 very helpful :)
cool!
great story
说的是什么呀? 我想知道怎么 java 怎么通过 Memcached 去取 C 语言放进去的数据流(InputStream?) Who can help me ?????
fukin genius
Great story guys, I love this.
great story I ever heard about programing! Maybe memecache can help me on my website。 I use redis before。
as i find out, e.g, i have two servers. on the first one, we have our codes, and on the other one, our memchache. so, by now, our first server has a 4 core processor and 8 GB or RAM. what do you recommend for our second Server? i think it must have more RAM. right? for example 12 GB of RAM. because memchache stores the Cache on this RAM. right?
I hardly ever comment, but I had to for this one. I wish all computer manuals used stories like this one.
Great story & info. I wish more wikki pages did it!
This definitely clears up how memcached is a distributed cache rather a replicated system. I guess system administrators (like myself) who are attempting to alleviate databases load assume memcached is another replicable data store.
nice story. helped a lot. thx
Hi guys, I never laughed so much with a technical explanation, thanks.
I wish all documents were like this.. I don't feel like I'm working.. Very nice documentation... lol
really awesome! I like it :)
thank you . Really good and made easy.
Thank you.Really good to understand memcached.Thank you.
Loved this story. The best way I've learned a new technology yet!
good story~
very intersting story , very good !
cool story :)
osom!
awesome..........This rocks!!!!!!!!!
Title/Moral of the story: How to Understand Memcached in 10 seconds... :)
What a way to speak about the features and guiding the developers all along.
PS: I might love someone, not his 'ass'!! Yukk... change that guys.
very nice! thanks guys!
cool!
lol, very effective and fun way of describing the basics. I'm downloading this ASP.
Interesting!!
love it
sooper fantastic dhool bhale :)
great smooth story
Nice...