Slide 1

Slide 1 text

Embracing the network Patrick Hamann — DIBI, March 2016
 @patrickhamann Modern techniques for building resilient front ends !

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Why?

Slide 5

Slide 5 text

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

https://en.wikipedia.org/wiki/Fallacies_of_distributed_computing 1. The network is reliable. 2. Latency is zero. 3. Bandwidth is infinite. 4. The network is secure. 5. Topology doesn't change. 6. There is one administrator. 7. Transport cost is zero. 8. The network is homogeneous. 
 The fallacies of distributed computing 
 PETER DEUTSCH — SUN MICROSYSTEMS, 1994

Slide 10

Slide 10 text

Multiple points of failure Dmytrii Shchadei — http://www.slideshare.net/metrofun/reduce-mobile-latency " # # # $ Internal latency Firewalls, Load Balancers, Servers % Internet routing latency CDNs, ISPs, Caches, Proxies Control plane latency ~600ms on average UK 3G connection ~200ms

Slide 11

Slide 11 text

Title Text

Slide 12

Slide 12 text

Title Text Request Map: http://requestmap.webperf.tools/

Slide 13

Slide 13 text

Baking in the assumption that everything can and will fail leads you to think differently about how you solve problems. 
 SAM NEWMAN — BUILDING MICROSERVICES, O’REILLY 2015 http://shop.oreilly.com/product/0636920033158.do

Slide 14

Slide 14 text

1. Testing and monitoring failure 2. Designing for failure 3. Embracing failure 4. The future

Slide 15

Slide 15 text

Testing for failure

Slide 16

Slide 16 text

A single point of failure (SPOF) is a part of a system that, if it fails, will stop the entire system from working. SPOFs are undesirable in any system with a goal of high availability or reliability, be it a business practice, software application, or other industrial system. https://en.wikipedia.org/wiki/Single_point_of_failure

Slide 17

Slide 17 text

http://uk.businessinsider.com/doubleclick-for-publishers-down-2014-11

Slide 18

Slide 18 text

http://uk.businessinsider.com/doubleclick-for-publishers-down-2014-11

Slide 19

Slide 19 text

http://uk.businessinsider.com/doubleclick-for-publishers-down-2014-11 http://www.stevesouders.com/blog/2010/06/01/frontend-spof/

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

https://en.wikipedia.org/wiki/Fallacies_of_distributed_computing

Slide 22

Slide 22 text

Facebook 2G Tuesdays - www.businessinsider.com.au/facebook-2g-tuesdays-to-slow-employee-internet-speeds-down-2015-10?op=1

Slide 23

Slide 23 text

User Timing API http://www.w3.org/TR/resource-timing/ window.performance.mark(“fonts-loaded“); window.performance.mark("css-start"); 
 performance.mark("css-end"); performance.measure("css-loading", "css-start", "css-end");

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

• Test 1st and 3rd party dependencies for SPOFs • Use network shaping to simulate mobile conditions • Monitor metrics contextual to your business • Mark and measure your custom events • Alert if metrics pass their thresholds/budgets

Slide 27

Slide 27 text

1. Testing and monitoring failure 2. Designing for failure 3. Embracing failure 4. The future

Slide 28

Slide 28 text

Designing for failure

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Delay User perception 0 - 100ms Instant 100 - 300ms Small perceptible delay 300 - 1000ms Machine is working 1000+ ms Likely mental context switch 10,000+ ms Task is abandoned

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

• Respond within a 1000ms • Avoid loading spinners • Consider using skeleton screens • Re-use familiar UI patterns within error messaging • Use human friendly copy, especially in error states • Visualise the state of the network

Slide 39

Slide 39 text

1. Testing and monitoring failure 2. Designing for failure 3. Embracing failure 4. The future

Slide 40

Slide 40 text

Embracing failure

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Loading fonts https://tabatkins.github.io/specs/css-font-rendering/ Chrome Opera Safari Firefox IE Timeout 3s 3s no timeout 3s 0 Fallback Yes Yes n/a Yes Yes Swap Yes Yes n/a Yes Yes

Slide 43

Slide 43 text

CSS Font Rendering Controls Module https://tabatkins.github.io/specs/css-font-rendering/ @font-face { font-family: 'MyShinyFont'; src: url('http://fonts.ft.com/async.woff2'); font-display: swap; } @font-face { font-family: 'MyBlockingFont'; src: url: url('http://fonts.ft.com/blocking.woff2'); font-display: block; }

Slide 44

Slide 44 text

& ServiceWorker ' ( %

Slide 45

Slide 45 text

& ServiceWorker ' ( % 

Slide 46

Slide 46 text

& ServiceWorker ' ( %

Slide 47

Slide 47 text

Cache only self.addEventListener('install', function(event) { event.waitUntil( caches.open(cacheName).then(function(cache) { return cache.addAll([ ‘http://fonts.ft.com/fonts.css', 'http://fonts.ft.com/serif', 'http://fonts.ft.com/sans-serif' ]); }) ); }); https://jakearchibald.com/2014/offline-cookbook

Slide 48

Slide 48 text

Cache only https://jakearchibald.com/2014/offline-cookbook self.addEventListener('fetch', function(event) { // If this is a request for font file if (/fonts\.ft\.com/.test(event.request)) { // If a match isn't found in the cache, the response // will look like a connection error event.respondWith(caches.match(event.request)); } });

Slide 49

Slide 49 text

X Cache only & ' ( %  ( X ( ( '

Slide 50

Slide 50 text

Title Text https://jakearchibald.com/2014/offline-cookbook

Slide 51

Slide 51 text

Stale-while-revalidate self.addEventListener('fetch', function(event) { // Get stale from cache event.respondWith( caches.open(cacheName).then(function(cache) { return cache.match(event.request).then(function(response) { // If in cache relvalidate var network = fetch(event.request).then(function(response){ if(response.status < 400) { cache.put(event.request, response.clone()); } }); // Return stale or network if none return response || network; }); }) ); }); https://jakearchibald.com/2014/offline-cookbook

Slide 52

Slide 52 text

Stale-while-revalidate & ' ( %  (

Slide 53

Slide 53 text

Stale-if-error self.addEventListener('fetch', function(event) { // Always fetch response from the network event.respondWith( fetch(event.request).then(function(response) { // If we received an error response if(!response.ok) { // If we received an error response return caches.match(event.request) } else { // Response was healthy so update cached version cache.put(event.request, response.clone()); return response; } }) ); });

Slide 54

Slide 54 text

Timeouts & ' ( % X * X

Slide 55

Slide 55 text

Timeout race function timeout(delay) { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(new Response('', { status: 408, statusText: 'Request timed out.' })); }, delay); }); } self.addEventListener('fetch', function(event) { // Attempt to fetch with timeout event.respondWith(Promise.race([timeout(2000), fetch(event.request)])); });

Slide 56

Slide 56 text

Dead letter queue & ' ( % X + offline online

Slide 57

Slide 57 text

• Pre-fetch and cache critical resources • Serve stale and revalidate • Serve stale if error • Always use timeouts • Queue and batch requests • Treat the network as an enhancement!

Slide 58

Slide 58 text

The future

Slide 59

Slide 59 text

Offline Network unavaliable

Slide 60

Slide 60 text

Push notifications

Slide 61

Slide 61 text

Background sync navigator.serviceWorker.ready.then(function(registration) { regitration.periodicSync.register({ tag: 'get-latet-news', // default: '' minPeriod: 12 * 60 * 60 * 1000, // default: 0 powertState: 'avoid-draining', // default: 'auto' networkState: 'avoid-cellular' // default: 'online' }).then(function(periodicsyncReg) { // success }, function() { // failure }); });

Slide 62

Slide 62 text

1. Testing and monitoring failure 2. Designing for failure 3. Embracing failure 4. The future

Slide 63

Slide 63 text

Thanks @patrickhamann
 speakerdeck.com/patrickhamann github.com/Financial-Times ! , - Do you want to help build this stuff? Join in.