Introduction to reactive programming · Resilient › Isolation and containment of failures...

Post on 25-Apr-2020

12 views 0 download

Transcript of Introduction to reactive programming · Resilient › Isolation and containment of failures...

IntroductiontoreactiveprogrammingJonasChapuis,Ph.D.

wikipedia

“Reactiveprogrammingisanasynchronousprogrammingparadigmorientedarounddataflows

andthepropagationof change”

asynchronousdataflows

change

Thingshappening atarbitrarytimes Movementsand

transformationsofdata Evolutionofthe

stateoftheworld

asynchronousdataflows

changeflowsof

Modernrequirements

10yearsago now

Servernodes 10’s 1000’s

Responsetimes seconds milliseconds

Serviceavailability 95% 99.999%

Datavolumes GBs TBs&PBs

Netflix:“thereactivecompany”

› Foundedin1997,streamingsince2007› 94millionsubscribers,9billionUS$› 35.2%downstreamtrafficintheUS› +5billionAPIrequestsperday› Reactiveandasynchronousfromfronttoback(RxJava,RxJS,Redux-Observable)

› Microservicesresilience&monitoring:Hystrix

GoldenageofAPIs

Source:

GoldenageofAPIs

Source:

Microservices

Microservices

Source:

ReactiveManifesto

Resilient

Responsive

TheReactiveManifesto

MessageDriven

Elastic

Source:

Messages

› Occurrenceofanevent(e.g.mouseclick,dooropened,etc.)› Requestorresponse› Stateofa(sub)system› Infact,anypieceofinformation

Message-driven:availability ofnewinformationdrivesthesystem

(vscontrolflowdriven byathread-of-execution)

Example:“timetoleave”featureinCortana

userLocation.Sample(1 * M).DistinctUntilChanged().Select(here =>

traffic(here, meetingLocation).Select(t => meeting – t.EstimatedTime))

.Switch()

.Select(t => “Leave now for GVA ” + t)

Device-sideeventstream

Cloud-sideeventstream

Higher-orderqueryoperators

PushversusPull

Erik

Alice

Bob

Carol

Anythingnewgoingon,Alice?- No,sameold

Bob?Bob?Areyouthere??…

Anythingnewonyourside,Carol?- Ohyesyou'vejustmissedalltheaction

ErikMeijerSource:

Push versusPull

Erik

Alice

Bob

Carol

- I'vejustfinishedmyreport

- I'msicktoday

- Something incredible ishappening here

ErikMeijerSource:

Message-driven

› Loosecoupling• Nodirectdependency:Idon’tdependonsomeotherrespondingparty• Notemporaldependency:I’mnotwastingresourceswaitingonothers

› Composable• Componentsareassembledinunidirectionalchainsbyintegration code,whichdescribes

thesystem“wiring”› Nosharedstate:non-blocking• Operation codeonlygeneratesnewmessages,itdoesn’tmutatedata(immutability)

• Wedon’thavemultiplethreadscompetingtomutatedata:nosynchronizationneeded

• Anyrequiredmutationandsynchronizationispushedoutofprogramdomain(e.g.database,internalsofdatastructures,etc.)

Elastic

› Scalable systems• Scaleupordown(adding orremoving cores)• Minimizesharedmutablestate,blockingandcontention

• Scaleoutorin(adding orremoving machines)• Locationtransparency,decouplingandresilience

› Idealforcloud deployments• Resource &cost efficient• Pay-per-use

Non-blocking

› Threadsofexecutioncompetingforasharedresourceshouldnotwaitbyblocking

› Amdahl’sLaw:contentionistheenemyofscalability

Amdahl’s LawThetheoreticalspeedupofaprogramislimitedbythepartthatcannotbenefitfromtheimprovement.

Source:Wikipedia

JVMserverload:syncvsasync

Source:ReactiveProgramming withRxJava,T.Nurkiewicz &BenChrisensen,O’Reilly

Requests/sec

#concurrentconnections

RxonRawTCP

RxHttp(RxNettyHttpServer)

Onethreadperconnection

Threadpool(100threads, servlet)Single thread

RawTCP

async IO,non-blocking

Responsetime:syncvsasync99th percentileofresponsetime[ms](99%requestsfasterthan)

#concurrentconnections

Onethreadperconnection

Threadpool (fail fast)

RawTCPRxonRawTCP

RxHttp

Single thread

Source:ReactiveProgramming withRxJava,T.Nurkiewicz &BenChrisensen,O’Reilly

async IO,non-blocking

Errorrate:syncvsasync

#concurrentconnections

Requesterrorratio

RawTCPRxonRawTCP

RxHttp

Threadpool (fail fast)

Single thread

Onethreadperconnection

Source:ReactiveProgramming withRxJava,T.Nurkiewicz &BenChrisensen,O’Reilly

async IO,non-blocking

Locationtransparency

› Componentsdon’tdependonbeinglocatedsomewheretoworkandfindtheirdependencies

› Benefits• Auto-scaling• Load-balancing• Takeinstancesofflineandreplacethemwhileserviceisrunning• Isolationoffailures

Resilient

› Isolationandcontainmentoffailures(bulkheads)› Failuresaremessageshandledasotherevents› Componentsdedicatedtohandlesuchsituations(e.g.supervisorhierarchiesinactorsystems)

› Loosecoupling

Source:Wikipedia (1943)

Source:Corellian engineering

Bulkheads

Source:dzone

Circuitbreaker

closed

open

half-open

callspassthroughcountfail/success

tripbreakerfailthreshold reached callsfailinstantly

waitforawhile

attemptcallafterwaitperiod

tripbreakeronattemptfailure

resetbreakeronattemptsuccess

Source:NetflixHystrix

Circuitbreaker

Responsive

› Modernapplicationsareexpected tobe responsive• Highlyadaptive,delightfulandrichreal-timeuserinteraction• Rapidandconsistentresponse times• End-userconfidence,constantqualityofservice

+ + →

Summary

› Anewparadigmfordesigningsystemsintheeraofcloudcomputing

› Complementaryconcepts:reactivesystemsandreactiveprogramming• Reactivesystems:message-driven,resilient,scalableandresponsive• Reactiveprogramming:throughhigherabstraction,we gainexplicittime,latencyandflowcontrol