How to cache Magento in Varnish (Theory) - There are 2 components to this
1) Static assets (eg. Images, CSS, JS) - This is a simple common pattern that involves detecting requests that belong to this category and setting a cache time (Or relying on the cache time being sent by the origin server)
Example of this in gist form
2) HTML documents - This is the much more complex part of a good Magento solution.
Its critical to cache HTML documents in Varnish to be able to improve Magento performance. HTML document generation is the most expensive (time consuming) thing that a Magento server will do.
The challenge with caching HTML documents comes from personalised content.
Personalised content and HTML documents
Magento, and all other ecommerce sites, manage the state of a particular user though a session. A session is a record of that particular user’s status on your site.
This covers things such as:
“Hello Bob” - at the top of the page
“4 Things in Your Cart” - the status of your shopping cart on each page
These are items that cannot be shared amongst users and would cause a major problem should this happen (we call this “session leakage”).
How do we cache HTML pages if the HTML pages contain personalised information about who the person is and what is in their shopping cart?
There are 2 main methods of achieving this:
Loading personalised elements of the page via additional requests after the page has loaded
A common implementation method here is to use AJAX to request page elements that are personalised
Leveraging a technology to mark components of the HTML document as cacheable and other’s uncachable (or un shareable amongst users).
Varnish supports a technology called ESI (Edge Side Includes) that allows different parts of a HTML document to be cached differently.
Your Varnish implementation strategy must factor in user personalisation.
Implementation options for Varnish
Magento 1.X - The most widely used method for caching HTML documents in Magento version 1 is the open source product called Magento Turpentine (by Nexus).
This is a plugin that is installed (via Magento Connect) and will automatically add ESI tags to your HTML documents so that Varnish can cache these resources. Magento Turpentine install / guide
Magento 2.X - The latest version of Magento (currently in beta) supports Varnish as its recommended solution for HTML caching in production. This is great news, Varnish is the recommended option from Magento and will work out of the box to improve your site speed.
How to make Varnish and Magento work well
Deployment is one thing, The next steps once you have a Varnish Magento solution implemented and working is to understand how well its performing. Getting metrics on cache hit rates and detailed logs on a request by request basis can be a challenge as it involves deploying a range of additional infrastructure (or being stuck doing manual log collection on a one off basis).
We have recently built this infrastructure to run Varnish as a service in the cloud (with full logs/metrics) - www.section.io - Plug aside this can be the most important element to actual success with you Varnish and Magento project as you need to be constantly tuning your implementation to manage things like varying query strings in urls (Hello google analytics "gclid"!) which can reduce your cache hit rates dramatically