58
Supercharge your Umbraco The need for speed

Supercharge your umbraco

Embed Size (px)

Citation preview

Page 1: Supercharge your umbraco

Supercharge your UmbracoThe need for speed

Page 2: Supercharge your umbraco

Chris Gaskell@CGaskell

[email protected]

Page 3: Supercharge your umbraco

The Web is a Demanding Place

Page 4: Supercharge your umbraco

The Need for Speed

Page 5: Supercharge your umbraco

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)

Page 6: Supercharge your umbraco

Who Caches Already?

Page 7: Supercharge your umbraco

Umbraco's Cache is Always Running

Page 8: Supercharge your umbraco

Need More?

Umbraco • Macro Caching• Partial View Caching

.NET• Output caching• Donut caching

Infrastructure• Varnish

Page 9: Supercharge your umbraco

Good Old Fashioned Optimisation

• JS & CSS minification & combination• Image resizing• Sprite sheets• Client side caching• GZIP

Page 10: Supercharge your umbraco
Page 11: Supercharge your umbraco
Page 12: Supercharge your umbraco

Macro Caching

Page 13: Supercharge your umbraco

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

Page 14: Supercharge your umbraco

Macro Caching Demo

Page 15: Supercharge your umbraco

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

Page 16: Supercharge your umbraco

Cached Partials

Page 17: Supercharge your umbraco

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

Page 18: Supercharge your umbraco

Cached Partials Demo

Page 19: Supercharge your umbraco

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

Page 20: Supercharge your umbraco

Output Caching

Page 21: Supercharge your umbraco

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.

Page 22: Supercharge your umbraco

Output Cache Demo

Page 23: Supercharge your umbraco

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

Page 24: Supercharge your umbraco

Donut Caching

Page 25: Supercharge your umbraco

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

Page 26: Supercharge your umbraco
Page 27: Supercharge your umbraco

Donut Cache Demo

Page 28: Supercharge your umbraco

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

Page 29: Supercharge your umbraco

But what if you need to go even faster?

Page 30: Supercharge your umbraco

Varnish

Page 31: Supercharge your umbraco

Story Time

Page 32: Supercharge your umbraco

Dresses

Give me dresses

Here’s some dress

data

I’ll make it look nice

Oooosome dresses

Page 33: Supercharge your umbraco

Hats

Has thisalready been

requested?No

Hats please

Here’ssome dataMake it

look niceI’ll store thisfor the next

request

Oooo Hats!

Page 34: Supercharge your umbraco

Hats

Has thisalready been

requested?

Yes

Oooo Hats!

ZzzzzzZzzzzz

Page 35: Supercharge your umbraco

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

Page 36: Supercharge your umbraco

@bsdphk we have a new record on web infrastructure reduction: From 46 to 5 servers!! @varnishcache rocks!

Page 37: Supercharge your umbraco

€0.00

Page 38: Supercharge your umbraco

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.

Page 39: Supercharge your umbraco

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

Page 40: Supercharge your umbraco

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

Page 41: Supercharge your umbraco

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

Page 42: Supercharge your umbraco

/etc/varnish/default.vcl

Page 43: Supercharge your umbraco

/etc/varnish/default.vcl

Page 44: Supercharge your umbraco

Varnish Demo

Page 45: Supercharge your umbraco

Grab your internet enabled device and navigate to

http://bit.ly/1ozG165

Page 46: Supercharge your umbraco

Refresh the page and watch the timestamp jump

http://bit.ly/1ozG165

Page 47: Supercharge your umbraco

Now have a look at the same page through Varnish

http://bit.ly/1hjTPCK

Page 48: Supercharge your umbraco

All well and good, but what happens when I publish content changes?

Page 49: Supercharge your umbraco

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

Page 50: Supercharge your umbraco

/etc/varnish/default.vcl

Page 51: Supercharge your umbraco

Purge on Publish

Page 52: Supercharge your umbraco

Real Life Umbraco VCLs

Page 53: Supercharge your umbraco

/etc/varnish/default.vcl

Page 54: Supercharge your umbraco

Load Testing

Page 55: Supercharge your umbraco

Load Testing the Demo

Without Varnish With Varnish

Page 56: Supercharge your umbraco

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

Page 57: Supercharge your umbraco

Thanks to Mike Gregson @mpgregson

Page 58: Supercharge your umbraco

Get the sample codehttps://bitbucket.org/chrisgaskell/umbraco-

caches

See it integrated into a V7 examplehttps://bitbucket.org/chrisgaskell/gemscafe

Chris Gaskell@CGaskell

[email protected]