Resilient Javascript From Front to Back End With Circuit Breakers - - PowerPoint PPT Presentation

resilient javascript from front to back end with circuit
SMART_READER_LITE
LIVE PREVIEW

Resilient Javascript From Front to Back End With Circuit Breakers - - PowerPoint PPT Presentation

Resilient Javascript From Front to Back End With Circuit Breakers Lance Ball Principal Software Engineer https://lanceball.com Twitter: @lanceball GitHub: @lance Riviera Dev 2018 Thursday, May 17 2018 Resilience Resiliency is defined as


slide-1
SLIDE 1

Resilient Javascript From Front to Back End With Circuit Breakers

Lance Ball Principal Software Engineer https://lanceball.com Twitter: @lanceball GitHub: @lance

Riviera Dev 2018 Thursday, May 17 2018

slide-2
SLIDE 2

Resilience

Resiliency is defined as the capability of a system to maintain its functions and structure in the face of internal and external change and to degrade gracefully when it must.

TOWARD INHERENTLY SECURE AND RESILIENT SOCIETIES Brad Allenby, Jonathan Fink http://science.sciencemag.org/content/309/5737/1034.full

slide-3
SLIDE 3

Microservices

slide-4
SLIDE 4

µ svc A µ svc B µ svc C µ svc D µ svc J µ svc H µ svc F µ svc G µ svc E

My App

slide-5
SLIDE 5

Microservices are not a panacea

slide-6
SLIDE 6

µ svc A µ svc B µ svc C µ svc D µ svc J µ svc H µ svc F µ svc G µ svc E

A Single Failure

slide-7
SLIDE 7

function wait (timeout) { return new Promise(resolve => { setTimeout(resolve, timeout) }); }

µ-Service D

slide-8
SLIDE 8

const MAX_ATTEMPTS = 10; let retryAttempts = 0; function fetchData (url) { return request.get(url) .then(formatData) .catch(err => { if (retryAttempts > MAX_ATTEMPTS) return Promise.reject(err); retryAttempts++; await wait(500); return fetchData(url); }); }

µ-Service D

slide-9
SLIDE 9

What Happens When We Keep On Trying?

(hint: things get worse)

slide-10
SLIDE 10

µ-Service G Causes D and E to Block

So now what?

slide-11
SLIDE 11

µ svc A µ svc B µ svc C µ svc D µ svc J µ svc H µ svc F µ svc G µ svc E

Causes More

slide-12
SLIDE 12

µ svc A µ svc B µ svc C µ svc D µ svc J µ svc H µ svc F µ svc G µ svc E

Cascade

slide-13
SLIDE 13

µ svc A µ svc B µ svc C µ svc D µ svc J µ svc H µ svc F µ svc G µ svc E

Dead App

slide-14
SLIDE 14

Assume that an application connects to a remote service 100 times per second and the service fails. The application developer does not want to have the same error reoccur constantly. They also want to handle the error quickly and gracefully without waiting for TCP connection timeout.

slide-15
SLIDE 15

Naive Implementations are a Band-Aid

slide-16
SLIDE 16

Circuit Breakers

slide-17
SLIDE 17
slide-18
SLIDE 18

const CircuitBreaker = require(‘opossum’); const options = { timeout: 1000, errorThresholdPercentage: 50, resetTimeout: 5000 } const circuit = CircuitBreaker( fetchData('/some/url'), options );

slide-19
SLIDE 19

function fetchData (url) { return _ => { return request.get(url) .then(formatData) .catch(err => { console.log(err) }); } }

slide-20
SLIDE 20

circuit.fallback( _ => 'Sorry, out of service right now' ); circuit.on('fallback', result => reportFallbackEvent(result));

slide-21
SLIDE 21

Events

slide-22
SLIDE 22
slide-23
SLIDE 23

★ fire ○ When the circuit is fired ★ success ○ When the call is successful ★ failure ○ When the call fails ★

  • pen

○ When the circuit opens ★ close ○ When the circuit closes ★ halfOpen ○ When the circuit enters half-open state ★ fallback ○ When a fallback function is called ★ cacheHit ○ A success value is in the cache ★ cacheMiss ○ A value was not found in the cache ★ timeout ○ When the call times out ★ semaphore-locked ○ When resources are used up and no more calls can be made ★ health-check-failed ○ When a user-supplied health check function fails ★ snapshot ○ When a statistics snapshot is taken

Events

slide-24
SLIDE 24

Statistics Snapshots

slide-25
SLIDE 25

RHOAR Circuit Breaker

Demo Time!

slide-26
SLIDE 26

But What About The Front End?

slide-27
SLIDE 27
slide-28
SLIDE 28

Elizabethan Insults

Moar Demo Time!

slide-29
SLIDE 29

const insult = circuitBreaker(getOrPostInsult, circuitBreakerOptions); insult.fallback(_ => { return { name: 'Server Admin', adj1: 'sleep-addled', adj2: 'half witted', noun: 'bumbershoot' }; }); insult.on('failure', console.log); insult.on('reject', console.log); insult.on('open', console.log); $('#invoke').click(e => insult.fire(e).then(updateInsultList)); $('#form-submit').submit(e => insult.fire(e).then(updateInsultList)); $('#clear').click(clearInsultList);

slide-30
SLIDE 30

Merci Beaucoup!

https://github.com/bucharest-gold/nodejs-circuit-breaker https://github.com/lance/elizabethan-insults https://github.com/bucharest-gold/opossum https://launch.openshift.io