Supercharge your UmbracoThe need for speed
Chris Gaskell@CGaskell
The Web is a Demanding Place
The Need for Speed
In computing a cache is a component that transparently stores data so that future requests for that
data can be served faster.
The data that is stored within a cache may be values that have
been computed earlier or duplicates of original values that are stored
elsewhere.http://en.wikipedia.org/wiki/Cache_(computing)
Who Caches Already?
Umbraco's Cache is Always Running
Need More?
Umbraco • Macro Caching• Partial View Caching
.NET• Output caching• Donut caching
Infrastructure• Varnish
Good Old Fashioned Optimisation
• JS & CSS minification & combination• Image resizing• Sprite sheets• Client side caching• GZIP
Macro Caching
Macro Caching
The original way to reuse blocks of functionality.
How to:1. Define macro in back office2. Add cache policy3. Call macro from within view
Macro Caching Demo
Macro Caching
The Good• Quick and easy to setup with instant results• No .NET• Easy to retro fit or fine tune on an existing install• Cache clears on CMS publish• Support for querystring parameters so long as they’re passed to the macro as parameters
• @Umbraco.RenderMacro("pageList", new { addSleep = "[@addsleep]"
The Bad• Difficult to turn off in dev (Macro cache disabler package doesn’t look to be compatible with
6+)• With MVC logic is moved into the view as there’s no chance to hijack the route
Cached Partials
Cached Partials
You don't normally need to cache the output of Partial views but there are times when this is necessary. Just like with macro caching you can cache the output of partial views.
This is done simply by using an HtmlHelper extension method:
@Html.CachedPartial("MyPartialName", new MyModel(), 3600)
The above will cache the output of your partial view for one hour (3600 seconds)
http://our.umbraco.org/documentation/Reference/Mvc/partial-views
Cached Partials Demo
Cached Partials
The Good• Quick and easy to setup with instant results• Easy to retro fit or fine tune on an existing MVC codebase• As the cache duration is passed this could be taken from a configuration setting and set to
easily in dev to 0 secs • Cache clears on CMS publish
The Bad• MVC logic is moved into the view as there’s no chance to hijack the route• If your view data changes the same cached output will be returned.
Please be aware: if you have a different model or view data for any page request, the result will be the cached result of the first execution
Output Caching
Output Caching
Output caching is a full page cache.
Output caching is part of the .NET framework and can be easily used with MVC or web forms applications.
In MVC output caching is implemented using an action filter attribute which can be applied to controller actions.
Output Cache Demo
Output Caching
The Good• Available ‘out of the box’ with ASP.NET – there’s nothing to install• Easy and quick to implement• Fast and robust• Easy to share common cache ‘profiles’ across the app from config
The Bad• Difficult to clear on a publish • The entire page is cached making personalisation and dynamic content difficult to
manage• Requires good management in development to ensure developers aren’t seeing cached
content
Donut Caching
Donut Caching
Donut caching is one form of output caching that is conspicuously absent from ASP.NET MVC and is greatly missed by many developers.
To implement donut caching the concept is fairly simple:1. Cache the page output and return it from the cache on subsequent
requests
2. Identify the areas that you do not want to cache (the donut holes). These areas are marked so that when the page is retrieved from cache these areas are replaced with non-cached versions before returning the page
Donut Cache Demo
Donut Caching
The Good• Gives the huge benefit of having portions of the page render for each request
while the surrounding mark-up comes from cache• Installs quickly (via nuget is best)• Easy to add into a well architected MVC solution• Has a cache manager allowing easy coupling to publishing for purging the cache
The Bad• Requires good management in development to ensure developers aren’t seeing
cached content• Does need some extra code to ensure the cache is purged on publish
But what if you need to go even faster?
Varnish
Story Time
Dresses
Give me dresses
Here’s some dress
data
I’ll make it look nice
Oooosome dresses
Hats
Has thisalready been
requested?No
Hats please
Here’ssome dataMake it
look niceI’ll store thisfor the next
request
Oooo Hats!
Hats
Has thisalready been
requested?
Yes
Oooo Hats!
ZzzzzzZzzzzz
Varnish Cache is a web application accelerator also known as a caching reverse HTTP proxy
You install it in front of any server that speaks HTTP and configure it to cache the contents
Varnish Cache is really, really fast. It typically speeds up delivery with a factor of 300 - 1000x, depending on your architecture
@bsdphk we have a new record on web infrastructure reduction: From 46 to 5 servers!! @varnishcache rocks!
€0.00
Infrastructure
RAM is what counts to allow the cache to scale. How much RAM depends on your requirements.
• With varnish you configure the size of the cache• Varnish also needs memory to do other things, so you'll see a fixed
couple of hundred extra megs. • Varnish has a overhead of about 1k per object. • Each thread (e.g., worker) will allocate its own memory, including a
stack. This memory usage will scale and contract as load spawns and reaps threads.
Installation on Ubuntu
To use the varnish-cache.org repository, do the following:
1. curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
2. echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list
3. sudo apt-get update4. sudo apt-get install varnish
Installing on Windows?
“Remember that Varnish on Cygwin Windows is not recommended for productions sites and it is only a proof-of-concept that can be used to test application on Windows or for checking you VCL while in that
platform.”
https://www.varnish-cache.org/trac/wiki/VarnishOnCygwinWindows
Inside Varnish
When a web page or content is requested:
Check if the content is cacheable If content is in cache
Return cached content Else
Fetch content from Web serverPut the content in the cacheReturn content
Else Return by fetching from Web server
/etc/varnish/default.vcl
/etc/varnish/default.vcl
Varnish Demo
Grab your internet enabled device and navigate to
http://bit.ly/1ozG165
Refresh the page and watch the timestamp jump
http://bit.ly/1ozG165
Now have a look at the same page through Varnish
http://bit.ly/1hjTPCK
All well and good, but what happens when I publish content changes?
Cache Invalidation
• purge; removes all variants of an object from cache, freeing up memory
• ban(); can be used to invalidate objects based on regular expressions, but does not necessarily free up memory any time soon
/etc/varnish/default.vcl
Purge on Publish
Real Life Umbraco VCLs
/etc/varnish/default.vcl
Load Testing
Load Testing the Demo
Without Varnish With Varnish
What Else
• Load Balancing• Can be configured as round-robin or random
• Probing • This accesses the heath of the application servers and can take servers in and out of the
balancing pool
• Serving stale cache• Should the application server go down Varnish can serve a ‘stale’ cache
• A Large set of monitoring and logging commands• https://www.varnish-cache.org/docs/4.0/reference/index.html
Thanks to Mike Gregson @mpgregson
Get the sample codehttps://bitbucket.org/chrisgaskell/umbraco-
caches
See it integrated into a V7 examplehttps://bitbucket.org/chrisgaskell/gemscafe
Chris Gaskell@CGaskell