Skip to content

What Is Varnish Cache In Layman’s Terms

by arlo on February 13th, 2012 

Varnish is a reverse web proxy caching system. It basically acts as an operator or a security guard, by intercepting all web requests before they reach your web server (apache).

How It Works

Varnish stores it’s cache in memory and not so much on the hard drive. In doing so, it is able to serve hundreds of more consecutive requests per second. This caching has other advantages like saving CPU time, file lookups and database requests for content that is not particularly “real-time”. Let’s take a look at what a web request might look like when using varnish.

Web Requests on a Server With Varnish

Web requests will most often produce the same repeated response. In which case the response should be cached and servered quickly without consuming additional resources. In the case that a response has not been cached, the request is allowed to pass through to necessary resources in order to produce a cacheable response.

Web Requests on a Server Without Varnish

Multiple PHP threads and database connections may produce high CPU consumption and increase response latency.

Additional Info

Varnish can also pass along http headers to your server side scripts (e.g. session objects) or unique HTTP headers to your client side. This unique functionality can be added using their javascript like language called VCL. You can also use raw C to connect to other existing C libraries.

The above images can be reused and copied. They are on a public Google Doc.

From → varnish

  • http://www.arlocarreon.com Arlo Carreon

    I don’t know if it’s just me, but I think I am getting better with the diagrams. =D

  • http://www.facebook.com/jozeperez Jozë Përez

    Your diagrams are indeed getting better. Great post by the way. Question: Can Varnish be installed on a shared hosting? Say HostGator.

  • http://www.arlocarreon.com Arlo Carreon

    I have not seen it provided yet by any hostings, but it would be more of a CPanel adoption which would certainly make it gain traction among-st shared hosting providers.

    CPanel could definitely give you control of only your domains cache and not touch anyone else’s on that machine.

    Also, Cpanel (hostgator as well) now offer cloudflare, which was similarly but is provided by a 3rd party. So you point some DNS records to cloudflare and they cache for you or point to your hosting if content not found in cache. Check your CPanel for it.

    Thanks for the questions!

  • http://tinywp.in/ Pothi Kalimuthu

    Hi Arlo,

    Those images look good indeed. :)

    Thanks for letting everyone to reuse and copy the images.

  • http://www.arlocarreon.com Arlo Carreon

    For shizzle! If you get a chance let me know if it’s used on a presentation or site. I get a kick out of it. =D

    You don’t have to credit or link to me.  Just let me know if you remember.  cheers.

  • Hitesh

    Nice article !!

    So when to use varnish and when to use memcache ?

  • http://www.arlocarreon.com Arlo Carreon

    Hey thanks! I use varnish to cache server responses to the client. So that apache doesn’t have to be the one responding all the time.

    I use memchaced or redis to store application data.  So I would store session data or big datasets in a “NoSQL” database.

    Basically:
    Varnish to cache for Apache
    Memcahced to cache for MySQL/MSSQL

  • Paul Leddy

    which graphing tool are you using?

  • http://www.arlocarreon.com Arlo Carreon

    Google Docs. Drawing document. Cool right?! If you click on the link you can see the original document.

  • David Morgan

    Super explanation! If my website were to be using a CDN, what would be the IP address I hand out? My Apache IP or the Varnish IP?

  • http://www.arlocarreon.com Arlo Carreon

    Sorry for the late response. The IP would be the same. Varnish is installed on your web server and is configured to intercept all http traffic (port 80).

  • R-J

    pcextreme.nl uses Varnish on their shared hosting platforms.
    Both to make the platforms more resistant to attacks and to make it lightning-fast.
    btw. check out the use of esi, Edge Side Includes, in Varnish. To create ‘dynamic’ pages from independently generated parts, that can all have there own ttl.

  • rurban

    Excellent diagrams to start with.
    What if the requested conent is not in the Mamory Cache?
    Can you show it on the graphic?

  • http://www.arlocarreon.com Arlo Carreon

    rurban, thanks and yes!

    The red arrows represent a unique request that has not been cached. In the varnish example it is the only request that hits all resources and is cached on the way out.

    In the latter example, it acts like all other requests by hitting all your resources along with other requests that might have been cached.

  • rahul

    how can i refresh the varnish cache

  • http://www.arlocarreon.com Arlo Carreon

    You are looking for what is called “purge”. There are different ways to purge the cache either for a single URL or for all of varnish. There are many resources available, just depends on your use case.

    e.g. http://www.garron.me/en/bits/how-flush-clear-varnish-cache.html