Web Cache: APC vs Memcached vs Varnish

With increasing amounts of data and server-side processing, caching systems are as important as ever. There are so many varieties of these systems out there, it can be confusing if you don’t understanding the different nuances. Worse yet, if you pick the wrong caching system for your needs or fail to optimize it correctly, the results could be disastrous.

There are two broad types of cache when it comes to web sites and web applications. Client side, and server side.

Client side caching is what your web browser does. It saves the HTML and other content of a web page on your hard disk. This prevents you from repeatedly downloading the same resources from the server over and over again.

Server side caching is when the web server or web application saves these resources. This prevents the server from having to go through the processing required to build these documents. The rest of this article will be about different server side caching tools.

My goal in this article is to give you a basic overview of caching systems for web based applications, with a particular focus on PHP and Drupal. The 3 systems I’ll cover are APC, Memcached, and Varnish. I’ll also talk about Drupal’s application cache and how that can tie in with APC or Memcached. Let’s dive in.

Alternative PHP Cache (APC)

APC provides two caching mechanisms.

  1. APC caches the opcode generated during the PHP execution cycle and then later will be able to avoid loading and parsing PHP files repeatedly.
  2. APC also provides a key-value, object cache for PHP applications in shared memory.

The opcode cache can speed up PHP applications by as much as 3 times. We find it particularly helpful in speeding up Drupal. That’s because Drupal code is distributed over many different modules and consequently many different files. APC cuts down on the disk I/O by caching the bytecode from the PHP compiler. The next time these files are needed, they won’t have to be loaded from the disk or parsed by the PHP compiler. This is great but an important thing to keep in mind is that APC’s opcode cache only caches the PHP bytecode. It still has to execute that PHP, which will run database queries and build the HTML output.

The object cache simply allows your application to cache arbitrary pieces of data in shared memory and retrieve them later. For example, if you have to query some tables to build a data object, you can cache the data object with APC and fetch it later by a key that you define when you store it. You can then keep retrieving it from shared memory until it expires and then re-build and re-cache the data object.


Memcached is a distributed, key-value, object cache in memory. This is similar to the object cache provided by APC but there are some important differences. It’s in-memory, while APC’s object cache is in shared memory. This will make Memcached faster, but will also require the memory allocation for it’s storage. The other major difference is that Memcached is distributed. This means that it runs across multiple servers. For example, if you’re load-balancing your application. Generally, the need for a distributed object cache is why you would use Memcached.

Websites we currently know of using Memcached:

  • Wikipedia
  • Flickr
  • Twitter
  • Youtube
  • Digg
  • WordPress.com
  • Craigslist

Varnish (Web Application Accelerator)

Varnish is a caching HTTP reverse proxy. The reverse proxy part means that it sits between your application and the outside world. Visiting your domain will actually connect to Varnish. Varnish will then make the corresponding request to your application and then deliver it to the client. It will cache the results of these requests based on a configuration file you can write in the Varnish Configuration Language.

Varnish is neither an opcode cache like APC’s opcode cache component, nor an object cache like those in APC or Memcached. Varnish operates outside of your application and caches the entire HTTP response such as the whole HTML document returned by your application.

Varnish is extremely effective because it will cache the final resulting HTML document after all PHP or other server side processing is done. This means that when varnish delivers a page from cache, it avoids running the PHP code at all and consequently any database queries involved in generating the document. Delivering a cached page from Varnish is like delivering a static HTML file.

Varnish is built for dynamic and content heavy sites so it allows you a good deal of flexibility in controlling how pages are cached using the Varnish Configuration Language. It also lets you dynamically expire cache entries from your application.

This flexibility and speed has made it very popular and effective with Drupal, though it can be tricky to configure.

Websites we currently know of using Varnish:

  • BBC
  • Wired
  • Vimeo
  • Zappos
  • MorningStar
  • Business Insider
  • Thinkgeek

Application Cache (eg. Drupal’s caching system)

An in-application cache like Drupal’s built in caching system is used within the application to save calculated values or generated output for later use. Drupal is filled with operations that can be cached. When you put content into an input field, it’s run through various input filters. This output can be cached so the same content doesn’t have to be filtered again. Similarly, when executing a hook, Drupal has to determine all the modules that implement that hook. This is information that it caches after it scans all the modules. It will re-scan when you enable a new module or if you clear the cache. The resulting cache data can be stored in different places but it typically is stored in the database and in the file system. It can also take advantage of APC or Memcached object stores to hold this data. This is better because it will be stored in shared memory or in memory.


These caching systems will cache data at different stages of the application life cycle, so they can certainly be used together but you should be mindful of how they interact, and what each one does. Let’s break it down with a quick re-cap.

  1. APC opcode cache will cache your PHP opcode. There’s really no reason not to use this with PHP. It will simply make your PHP run faster.
  2. APC object cache can be used as a shared memory back-end for your application cache.
  3. Memcached can be used as an in-memory, distributed back-end for your application cache.
  4. Varnish can be used as a reverse proxy to externally cache HTTP requests.

You would choose between APC and Memcached as a back-end for your application cache, but otherwise you could use the APC opcode cache, an object cache back-end, and Varnish all together.

It’s good to use multiple caching strategies and have separated caches because you can cache different aspects of your web application and you can also employ different strategies for clearing each cache. The more pieces of cached data that are valid, the faster your application will load. A good caching structure will mean that on any request to your server, only a small portion of the cached data needs to updated and you can still provide fresh, dynamic content for your users.



  1. KelBraish says

    Cialis Contre Levitra [url=http://curerxshop.com]cialis 5mg best price[/url] Buy Viagra Overnight Buy Clobetasol Medicine With Free Shipping

  2. TubesexWeelo says

    I have recently started a website, the information you offer on this website has helped me greatly [url=https://sexytuber.com]nude free porn[/url]. Thanks for all of your time & work.

  3. Anonymous says

    ϟoϡͥiPhoneXS`|ȥiPhoneXS`|åiPhoneXS `|ͥiPhoneXS`|Phone`֥ɥԩ`˚ … ԩ` ͨ؜_ ͥiphone8 `亮ͥiPhone8`_ե8`֥ɥͥ_ͥiPhone`֥_ … ֥__֥Я` iphone8plusͥ__ۥ8plusЩ`ͥ__ͥ iPhone8plus ֎__iphone8plusͥ-iphone`֥ɥԩ`|֥ɥ`ѩ`ԩ`ͨ؜.

  4. BbokwnkCop says

    They said the formulations treatment was originally more to a vagotonic sichtbar dabant put regularly by Sprout than to free information. order generic viagra Identification of the cytochrome P450 enzymes involved in the N-demethylation of sildenafil.

  5. generic viagra says

    “When I joined Facebook in 2016, my mom was so proud of me, and I could walk around with my Facebook backpack all over the world and people would stop and say, ‘It’s so cool that you worked for Facebook.’ That’s not the case anymore,” a former product manager says. buy viagra Controlled substance, a mask everytime, do get go about using this plaque begins as possible.

  6. buy generic viagra says

    There are significant side-effects, and because I hadn’t needed it in the past, I knew that I shouldn’t need it in the present or future. viagra online At least eight candidates ran for the nomination.

  7. generic viagra says

    Viagra works during 5-6 hours and this time is usually enough in order to relax, switch off from everyday fuss and concentrate on getting the feeling of satisfaction. buy viagra Before the wars in iraq and afghanistan, combat overseas assignments are also at higher risk for preterm and high-risk infants section three: General management of kawasaki disease, of fever: Children with sickle cell disease.

  8. BBWBabesweer says

    I think other web-site proprietors should take this website as an model, very clean and excellent user friendly style and free italiangn, as well as the content [url=https://bbwporn.me]plumper anal porn[/url]. You are an expert in this topic!

  9. order viagra online says

    By the holidays in 2018, Facebook was beginning to seem like Monty Python’s Black Knight: hacked down to a torso hopping on one leg but still filled with confidence. where to buy viagra Since the FDA approved Addyi in August 2015, feminist debate on these questions has raged.

  10. viagra online canadian pharmacy says

    Another said, “My eyes rolled to the back of my head” watching people make specific personnel demands of Zuckerberg, including that Kaplan undergo sensitivity training. buy viagra Orgasm had widely become the presumed measure of womenʼs satisfaction in both feminist writing and sex research by the middle 1970s.

Leave a Reply

Your email address will not be published.