My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
TutorialCachingStory  
This is a story of Caching
Updated Feb 10 (43 hours ago) by dorma...@rydia.net

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 please

Programmer 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.

Comment by jfox.glo...@gmail.com, Jun 29, 2009

Nice story :)

Comment by ranje...@gmail.com, Jul 3, 2009

gr8 idea of imparting knowledge via story. It is really interesting to read it as a story. Good one.

Comment by ang...@gmail.com, Jul 24, 2009

I remember this history, greetings to David the plucky cat and Chris the plucky emo.

Comment by christia...@gmail.com, Jul 24, 2009

what is "plucky"? it's synonymous of squirrel?

Comment by alexande...@gmail.com, Aug 9, 2009

Beautiful, thanks a lot!

Comment by kamol...@gmail.com, Aug 10, 2009

It is the best way to understand memcached. Thank You!

Comment by arijit.sarbagna@gmail.com, Aug 19, 2009

Excellent way of explaining the basics! Loved it! Sweeeeet!

Comment by rajat...@gmail.com, Sep 10, 2009

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.

Comment by lawrence...@gmail.com, Oct 17, 2009

clever, informative and very well written. Cheers

Comment by moorthy....@gmail.com, Nov 1, 2009

Wonderful and easy to understand!

Comment by niallvom...@gmail.com, Nov 16, 2009

Interesting story and solution, blankITsolution 2009

Comment by peter.ne...@gmail.com, Nov 28, 2009

I get it.

Comment by zAn0Th...@gmail.com, Dec 13, 2009

Thank you for this great story. Now, i get it ;)

Comment by jaya...@gmail.com, Dec 15, 2009

And me too :)

Comment by xmart...@gmail.com, Dec 15, 2009

What if I don't have a frong page... will it still work? ;-)

Comment by ldard...@gmail.com, Jan 20, 2010

600 seconds are 10 minutes, not 5

Comment by kunalmsh...@gmail.com, Jan 25, 2010

Awesome!

Comment by jeffreyd...@gmail.com, Jan 26, 2010

Ah. Does this me memcached is worthless if I only have one server?

Comment by lob...@gmail.com, Feb 9, 2010

Good story, tutoring vividly!

Comment by sharoonk...@gmail.com, Feb 12, 2010

I love the story

Comment by webash.u...@gmail.com, Feb 25, 2010

I enjoyed the story. Nice.

Comment by Banniste...@gmail.com, Mar 3, 2010

lol, awesome! @jeffreyd.davis You still get the great advantage of storing everything in memory.

Comment by caojiang...@gmail.com, Mar 4, 2010

perfect story! I like it. and isabella@isapilates.com

Comment by antonbou...@gmail.com, Mar 16, 2010

Great story dude!!

Comment by rauh...@gmail.com, Mar 17, 2010

sweetheart loved it!!!!!! knowledge shd be spread in such cheesy way!!!!!

Comment by Eduar...@gmail.com, Mar 18, 2010

The best way to explain it! Thanks Guys!

Comment by xfuaj...@hotmail.com, Mar 19, 2010

Great story,maybe i've understood.

Comment by daniele....@gmail.com, Mar 19, 2010

First time i read a tutorial as 'story', and I think is great! Good job guys!

Comment by kayon...@gmail.com, Mar 24, 2010

Interesting approach

Comment by xitru...@gmail.com, Apr 8, 2010

Great story, I get it, thanks guys anthonyle81@gmail.com

Comment by wloche@gmail.com, Apr 13, 2010

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 :)

Comment by contact_...@yahoo.com, Apr 19, 2010

Nice way to explain the basics of memcache.

Comment by lekhnath...@gmail.com, Apr 21, 2010

Good approach to introduce the basics of memcached

Comment by garciaj...@gmail.com, May 5, 2010

hihihi i like the story and the way the message gets explained.

Comment by rushik.u...@gmail.com, May 7, 2010

liked the story......lemme try practically....

Comment by intel...@gmail.com, May 11, 2010

This was actually quite a bit of help and it was interesting to read :) Brilliant

Comment by ken...@gmail.com, May 12, 2010

I like it :)

Comment by ankurgoe...@gmail.com, May 15, 2010

great guys...

Comment by cool_cap...@yahoo.co.in, May 17, 2010

:) 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?

Comment by mmj...@gmail.com, May 19, 2010

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.

Comment by steven...@gmail.com, Jun 8, 2010

i have learned what's memcached with my poor english because of this nice story.

thanks guys.

Comment by mraru...@gmail.com, Jun 22, 2010

The KISS rule is always cool.. Keep It Simple and Stupid. Thanks a million.

Comment by whouseho...@gmail.com, Jun 23, 2010

well done. awesome!!

Comment by yangdon...@gmail.com, Jul 18, 2010

This is Wonderful!!! I love it! If all tech things are explained via a story, man, everybody will be in love with tech books.

Comment by landy...@gmail.com, Jul 21, 2010

LOVE IT!!

Comment by digitalprecision@gmail.com, Jul 26, 2010

EVERYTHING on the interwebs should be written like this

Comment by loneline...@gmail.com, Jul 29, 2010

GREAT

Comment by anto...@peopleperhour.com, Jul 30, 2010

the best story ever! I like it :D

Comment by dam2...@gmail.com, Aug 4, 2010

got the nail in the head!

Comment by idealit...@gmail.com, Aug 28, 2010

Should 'get huge_data_for_front_page' be 'get huge_data_for_frong_page' ?

Comment by coolwang...@126.com, Sep 1, 2010

What a fun story ... what a good tutorial ...

Comment by andrew.r...@gmail.com, Sep 8, 2010

I couldn't figure out what one is supposed to use memcached for and now I totally get it. THANK YOU!

Comment by zzza...@gmail.com, Sep 9, 2010

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.

Comment by loretopa...@gmail.com, Sep 21, 2010

memcached rocks!

Comment by buraga.s...@gmail.com, Sep 28, 2010

Nice story, I ask the pluncky programmer and sysadmin my client can be also .net or java?

Comment by richard....@gmail.com, Sep 30, 2010

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.

Comment by ab...@hireplug.com, Oct 7, 2010

nice getting started in a funny way..

Comment by pop...@gmail.com, Oct 7, 2010

I've memcached this story!

Comment by louiedgo...@gmail.com, Nov 10, 2010

Love this story wish they could do a lot of this kind when explaining.. :)

Comment by joshuay...@gmail.com, Nov 15, 2010

nice and impressive story! well done dude!

Comment by joshuay...@gmail.com, Nov 15, 2010

nice and impressive story! well done dude!

Comment by wik.wu2...@gmail.com, Nov 17, 2010

Cool story!

Comment by suon...@gmail.com, Nov 20, 2010

A farfetched story to introduce Memcached!

Comment by Willy.Ch...@gmail.com, Dec 2, 2010

really a easy understanding story about memcached,thanks for your great job!

Comment by fylme...@gmail.com, Dec 10, 2010

very good!

Comment by coldtea...@gmail.com, Dec 10, 2010

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

Comment by xiaoyang...@gmail.com, Dec 14, 2010

great story

Comment by mjunaid...@gmail.com, Dec 21, 2010

awsm!!

Comment by bobbo...@gmail.com, Jan 24, 2011

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!

Comment by prakasht...@gmail.com, Jan 25, 2011

This is the best tutorial I have ever read!!! Great job guys!!!

Comment by elventa...@gmail.com, Feb 4, 2011

awesome :)

Comment by sushantp...@gmail.com, Feb 8, 2011

g8 very helpful :)

Comment by mbe...@gmail.com, Feb 9, 2011

cool!

Comment by shahan...@gmail.com, Feb 10, 2011

great story

Comment by zhb5...@gmail.com, Feb 12, 2011

说的是什么呀? 我想知道怎么 java 怎么通过 Memcached 去取 C 语言放进去的数据流(InputStream?) Who can help me ?????

Comment by jackthom...@gmail.com, Feb 13, 2011

fukin genius

Comment by ankursh...@gmail.com, Feb 28, 2011

Great story guys, I love this.

Comment by kanongzh...@gmail.com, Mar 15, 2011

great story I ever heard about programing! Maybe memecache can help me on my website。 I use redis before。

Comment by ksna.man...@gmail.com, Mar 22, 2011

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?

Comment by zan...@gmail.com, Mar 25, 2011

I hardly ever comment, but I had to for this one. I wish all computer manuals used stories like this one.

Comment by cyr...@gmail.com, Mar 29, 2011

Great story & info. I wish more wikki pages did it!

Comment by simon.ma...@gmail.com, Apr 4, 2011

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.

Comment by Ryan...@gmail.com, Apr 7, 2011

nice story. helped a lot. thx

Comment by juanp.re...@gmail.com, Apr 20, 2011

Hi guys, I never laughed so much with a technical explanation, thanks.

Comment by ksw2...@gmail.com, Apr 29, 2011

I wish all documents were like this.. I don't feel like I'm working.. Very nice documentation... lol

Comment by hit...@gmail.com, May 5, 2011

really awesome! I like it :)

Comment by sandee...@gmail.com, May 6, 2011

thank you . Really good and made easy.

Comment by jsmithy...@gmail.com, May 9, 2011

Thank you.Really good to understand memcached.Thank you.

Comment by joshparo...@gmail.com, May 23, 2011

Loved this story. The best way I've learned a new technology yet!

Comment by freewind...@gmail.com, Jun 18, 2011

good story~

Comment by hellowor...@gmail.com, Jun 21, 2011

very intersting story , very good !

Comment by fatihpiristine, Jun 27, 2011

cool story :)

Comment by stealth....@gmail.com, Jul 8, 2011

osom!

Comment by ganeshvb...@gmail.com, Jul 10, 2011

awesome..........This rocks!!!!!!!!!

Comment by satya...@gmail.com, Jul 12, 2011

Title/Moral of the story: How to Understand Memcached in 10 seconds... :)

Comment by vaibhavk...@gmail.com, Jul 16, 2011

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.

Comment by ducdungn...@gmail.com, Aug 1, 2011

very nice! thanks guys!

Comment by base...@gmail.com, Aug 3, 2011

cool!

Comment by edrisse...@gmail.com, Aug 4, 2011

lol, very effective and fun way of describing the basics. I'm downloading this ASP.

Comment by har...@seamedia.in, Aug 8, 2011

Interesting!!

Comment by duju...@gmail.com, Aug 23, 2011

love it

Comment by sellstuf...@gmail.com, Sep 8, 2011

sooper fantastic dhool bhale :)

Comment by poor...@gmail.com, Sep 18, 2011

great smooth story

Comment by zhongxia...@izenesoft.com, Sep 18, 2011

Nice...

Powered by Google Project Hosting