Go Premium for a chance to win a PS4. Enter to Win


When to use Memcached?

Posted on 2011-03-03
Medium Priority
Last Modified: 2012-05-11
Hi ,

I recently heard about Memchached and how it can cache queries from your db and save on resources ultimately.

How much faster would it actually be to cached a php page that grabs its information from a db?

I know the answer to the question depends on different variables but are there benchmarks out there to demonstrate the efficiency?

I am just not sure if it is worth going through the trouble of learn how to use it if it is not necessary.

Question by:Solutionabc
  • 3
  • 2
LVL 35

Accepted Solution

gr8gonzo earned 1000 total points
ID: 35029558
Memcache is not your only method of caching things. It is simply one form of storage (memory based). Usually memcache is overkill for most people, because it works best when you're under EXTREMELY heavy loads (hundreds of visitors per second pulling the same data).

If you need a benchmark to demonstrate how much faster it is for you, it probably is not worth the effort. You just need to think about what caching does in order to understand if it will help you - all it does is remember the answer of something that takes a long time to figure out.

Imagine if you were given a huge mathematical formula 1*2^19/4*8-6...blah blah... that takes an hour for you to solve. Once you solve it, if you write down the answer and you're asked again to solve that same problem, you don't have to go through all the steps - you just look at the answer you wrote down and you're done.

That's the concept of caching in general.

Now, MySQL (and most other databases) has its own query cache that is easy to turn on if it's not already enabled, and it should handle / manage everything automatically. It is much better to use that for your database query cache (if you have a really big query that takes a long time to run) than something like memcache.

If you have tens of thousands of people requesting the same static resources (images, simple PHP scripts, javascript files, etc) over and over again, it may be worth your while to use some kind of cache (memcache or what not) in conjunction with your web server to store those items in memory so the server can send that data from RAM instead of reading the hard drive for those files every time.

Keep in mind that any cache has to be managed in some way. If you cache a picture in memory for 5 days and you change / edit the picture on the 2nd day, you'll still see the old picture in the cache for another 3 days unless you forcibly tell the cache to clear itself and re-read the content. That's why it's best to only cache content that doesn't change often.

But again, unless you have a huge amount of traffic, you're not going to see much benefit from memcaching - usually the biggest speedups on slow sites come from optimizing queries.

Assisted Solution

JayDiablo earned 1000 total points
ID: 35030268
Memcached is extremely fast when used properly (much faster than querying your database).  That said, it's not useful for all queries.  When something is stored in memcache, you typically set an expiration time on that cached item, and assign that item a unique key (depending on your use case, this key could be an MD5 hash of the query that you're caching the result for, for example).  Now, in your data access layer, you'd calculate this MD5 hash before you goto the database to retrieve data.  With that hash you can ask memcache for the result-set rather than MySQL, if it exists, use that result and never touch the database.  If it doesn't exist, go to the database to get the data.

MySQL's query cache works a little bit differently, mainly because the cached result set that mysql stores is invalidated (basically deleted) if any of the tables that are involved in the query have had any changes made to them since the result was cached.  This works nicely for automatic caching where you don't have to worry about stale content at all.  On the other hand, with memcache, you may be pulling stale content, as memcache has no idea if the underlying database table has been changed.  You either need to actively manage your cached objects (deleting them when a relevant table is updated) or accept that it's okay to have stale content.

If, say, on the home page of your site you have a little box that shows some "top of the week" type data, that could be an excellent use case for a memcachable query, since it may not be very important if that data is the same for a 24 hour period (set a 24 hour cache expiration).  Especially if the query (or queries) involved to generate those statistics are resource intensive.  Now every time your home page is loaded the data will be loaded from memcache, rather than from the database, reducing the resources spent on that piece of content (which may not have to be "to the second" up-to-date).

On the flip side of that, you probably wouldn't want to use memcache to show a user some statistic that the user expects to be updated in real-time.

Memcache really isn't a simple "set and forget" option like the mysql query cache typically is, usually you need to plan what you want to cache, how long that can be cached, how you might clear that cache, etc...

Author Comment

ID: 35031591
Thanks for the great information.

So Memcache can basically cache anything displayed in the browser right? So if I was making http requests to other servers I could still cache the results and eliminate the need to make another call that day?

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 35031640
Yeah, you can cache anything you want really.

Also, to clarify one thing, stuff put into memcache isn't guaranteed to stay there for the duration of the expiration, so don't put something in there that you wouldn't be able to regenerate if it's not in the cache.  Memcache may "evict" things you put into it if it runs out of free memory for new cache items coming in.

So in your example, you may have to make a request for this other server's content more than once per day if for some reason your cached content gets evicted.  However, that should be expected, and your server can just make the request again if it can't be found in the cache.
LVL 35

Expert Comment

ID: 35031797
To be honest, I'd suggest avoiding memcache and just using your own caching system. If you're pulling a file from another server and it takes 10 seconds to do, but you really only need the latest content once per day, then use file_put_contents() to dump the result into a specific file, then use filemtime() to determine when the file was last modified. If it was last modified over 24 hours ago (86,400 seconds), then fetch the file from the server again:

$url = "http://www.blah.com";
$cacheFile = "www.blah.com.cache";
if(file_exists($cacheFile) && ((time()-filemtime($cacheFile)) < 86400))
   $content = file_get_contents($cacheFile);
  $content = file_get_contents($url);

Expert Comment

ID: 35031888
It's also possible to use APC as a memory cache as well, which can be handy if you're already using APC for an opcode cache.  One benefit to using APC over memcache is that you don't have to setup and maintain memcache servers (but in all honesty you could put a memcache server on each of your webservers and just call them locally, which would be very similar to using something like APC (or even a file cache as gr8gonzo mentions)).

In a situation where you have a cluster of webservers serving content, memcache can be a better solution as it's a single cache point that all servers can talk to (if setup that way).  With file caching and APC, you have as many cache points as you do servers.  The cache isn't shared between servers.  So you'd have to build the cache at least once per day, per server.

Memcache (and APC) will be faster than a file based cache under heavy load, but for most small applications that aren't receiving a lot of traffic you probably wouldn't see the difference.  Memcached and to a lesser extent, APC, are a bit more complicated to setup than sticking a bit of PHP code in your app, so do what you're comfortable with.  Looking to learn something new?  Give memcache a try.  Already using APC for opcode caching?  Look up how to cache user data with it (it's very easy to do if you already have APC installed).  There are certainly a lot of options when it comes to caching.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question