Serving up content on the Internet is something our web sites do daily. But are we doing this in the fastest way possible? How are users in faraway countries experiencing our apps? Why do we have three webservers serving the same content over and over again? In this session, we’ll explore the Azure Content Delivery Network or CDN, a service which makes it easy to serve up blobs, videos and other content from servers close to our users. We’ll explore simple file serving as well as some more advanced, dynamic edge caching scenarios.
Reducing latency on the web with the Azure CDN- TechDays NL 2014
1.
2. Laat ons weten wat u vindt van deze sessie! Vul de evaluatie
in via www.techdaysapp.nl en maak kans op een van de 20
prijzen*. Prijswinnaars worden bekend gemaakt via Twitter
(#TechDaysNL). Gebruik hiervoor de code op uw badge.
Let us know how you feel about this session! Give your
feedback via www.techdaysapp.nl and possibly win one of
the 20 prizes*. Winners will be announced via Twitter
(#TechDaysNL). Use your personal code on your badge.
* Over de uitslag kan niet worden gecorrespondeerd, prijzen zijn voorbeelden – All results are final, prizes are
examples
10. Speed of light and TCP don’t like each
other
•US East – US West = 7400 km
• or 25 ms at speed of light (299792,458 km/second in a vacuum)
• or 37 ms through fiber optics (66% of SoL, glass refraction index 1.5)
•TCP request/response, ACK request/response
• double that 37 ms, add some compute: ~90 ms US East to West
•Theoretical max. packet size is 64 kB
• usually +/- 1500 bytes (MTU)
•TCP slow start
http://www.stevesouders.com/blog/2010/07/13/velocity-tcp-and-
the-lower-bound-of-web-performance/
11. Combine TCP slow start and fibre optics
US East – US West
~90ms + TCP slow start
256 kB ~ 10 TCP roundtrips
that 90 ms becomes 900 ms...
http://www.stevesouders.com/blog/2010/07/13/velocity-tcp-and-
the-lower-bound-of-web-performance/
12. Cost of web load
•Serving static files costs CPU
• Full IIS pipeline for a tiny static file
•Serving static files costs I/O
• Files have to be copied from file stream to response stream
• Why do this? Why not let the server handle our dynamic
content?
•Cookies!
• Request/response cycle adds the cookie
• Even for a 1 kB PNG
13. So there are some problems on the
Internet...
•Browsers / connections
•Speed of light / TCP slow start
•Cost of web load (CPU, I/O and cookies)
15. Workarounds!
•Browsers / connections
• Serve some content off a different hostname
•Speed of light / TCP slow start
• Move content closer to the user
•Cost of web load (CPU, I/O and cookies)
• Serve off a cookieless domain
• Move content off the web server and let someone else handle it
17. The Azure CDN
•Serve content from storage / cloud service
•Separate hostname (custom domain possible)
•Many locations around the globe
•DNS anycast to get content close to user
22. What did we just do?
•First request
•Second request (on same endpoint)
yawn!
23. How to use this in real life?
•Create one/more public blob containers
•Upload static files in there
(CSS, images, scripts, ...)
•Update your application to the new URLs
25. How to delete content from the CDN?
•Read a book or 2 and wait (7 days default...)
•Think about this upfront!
• If you know expiry, set the Cache-Control header (shorter = more
updates)
• If you don’t, use versioning in query strings
• Better: use both
26. Versioning
•Enable query strings on the CDN endpoint
•Use a query string with a meaningful version number
•CDN will keep a cache per URL per query string
• /foo/bar?v=1
• /foo/bar?v=2
• /foo/bar?v=3
27. Cloud services will make life easier
•We had to “manually” upload content to storage
• May be good, may be cumbersome, depends!
• Would be nice if we could “deploy and forget”
•Set a cloud service as the CDN origin
• Will serve all content from /cdn URL
• Same cache-control rules as with storage
31. Defining “dynamic content”
•Content that is generated
• Parameters from query string, ASP.NET routing, ...
• And/or based on data
•Content that refreshes, but not too often
• Anything > a couple of minutes
•Examples
• Charts, images, generated documents, json, API’s, ...
36. Laat ons weten wat u vindt van deze sessie! Vul de evaluatie
in via www.techdaysapp.nl en maak kans op een van de 20
prijzen*. Prijswinnaars worden bekend gemaakt via Twitter
(#TechDaysNL). Gebruik hiervoor de code op uw badge.
Let us know how you feel about this session! Give your
feedback via www.techdaysapp.nl and possibly win one of
the 20 prizes*. Winners will be announced via Twitter
(#TechDaysNL). Use your personal code on your badge.
* Over de uitslag kan niet worden gecorrespondeerd, prijzen zijn voorbeelden – All results are final, prizes are
examples
Notas del editor
Maarten
Twitter does too... Still there are 9 requests on this page going to the same host, causing delays in rendering the page.
TCP slow start: start with 1 concurrent connection, then upgrade to multiple when ACK is received
So for 256 kB
Go to the portalCreate a new CDN endpointExplain it can be based on a storage accountExplain it can be based on a cloud serviceExplain options briefly, we’ll cover most of them in more detailExplain linking a custom domain is possibleExplain SSL is possible, unfortunately with a MS provided cert which is useles for custom domainProvisioning takes +/- an hour for settings to propagate to all CDN locations, be patient!Do a tracert to e.g. az591544.vo.msecnd.net and show that it resolves to a nearby location and does not go overseasNow make a request to the CDN https://az591544.vo.msecnd.net/demo/helloworld.gif Explain it is coming from https://azurecdndemo.blob.core.windows.net/demo/helloworld.gif Mention it has to come from a public container
Open Demo1StaticOpen the _Layout.cshtml and change all scripts/css at the top to the CDN urlsLaunch the application and note it does not work because our content is not on the CDN yetGo to /Home/Synchronize and explain what it does in the background, show the codeExplain what we are doing (uploading, setting content type, ...)Mention by default blobs are cached for 7 days! 7 days!Illustrate this by updating bootstrap.min.js and adding an alert in there, resync and refresh: NOTHING HAPPENSThere are some solutions to this:Set the cache-control header on the blob, see /Home/Synchronize source code for an example. Note you would have toknow what the expiry will be...Work with query string parameters, for example version. Update our _Layout.cshtml to use a version / timestamp / whatever query stringThe CDN will now create a cache per file/querystring
Open Demo2StaticOpen the _Layout.cshtml and note the application is using bundlingLet’s configure bundling to use the CDN when not debuggingOpen bundle config and mention we can enable CDN, which will use the alternate URL when not in debug mode. This allos nice debug/release workflow.Mention we still want to use the query string versioning! New deployment should yield new contents! // Configure CDN var version = System.Reflection.Assembly.GetAssembly(typeof(BundleConfig)).GetName().Version.ToString(); var cdnUrl = "http://az592218.vo.msecnd.net/demo2static/{0}?" + version; bundles.UseCdn = true;Explain the URL we are using as the alternative URL and why there is no /cdn in thereDEPLOY! Run the application and notice it fails because no content could be found. Why???We’re not rewriting content to the /cdn folder! Good thing is we can solve that with some Web.config rewrite rulesAdd <rewite> block and explain what it doesDeploy, now it works, yay!Go through some other web.config options:setting cache-control and expires headers!http compression!
Open Demo2StaticShow the application: /MemeGenerator with some quotes (when i peel onions, onions cry – counted to infinity, twice – can divide, by zero – refactors, with his fists)Open the MemeGeneratorController and go through codeExplain this one is not too heavy, but still measuring strings, doing computation, ...Imagine world cup score table, a bit more dynamic (where we hope Belgium score increases pretty much all the time) and high load, why not serve that up through the CDN. Yes, delay on data freshness, but fast and not too much work for the server.http://az592218.vo.msecnd.net/demo2static/MemeGenerator/Show?top=When%20I%20peel%20onions&bottom=Onions%20cry