introduction to reactive programming · resilient › isolation and containment of failures...

31
Introduction to reactive programming Jonas Chapuis, Ph.D.

Upload: others

Post on 25-Apr-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

IntroductiontoreactiveprogrammingJonasChapuis,Ph.D.

Page 2: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

wikipedia

“Reactiveprogrammingisanasynchronousprogrammingparadigmorientedarounddataflows

andthepropagationof change”

Page 3: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

asynchronousdataflows

change

Thingshappening atarbitrarytimes Movementsand

transformationsofdata Evolutionofthe

stateoftheworld

Page 4: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

asynchronousdataflows

changeflowsof

Page 5: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Modernrequirements

10yearsago now

Servernodes 10’s 1000’s

Responsetimes seconds milliseconds

Serviceavailability 95% 99.999%

Datavolumes GBs TBs&PBs

Page 6: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Netflix:“thereactivecompany”

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

› Microservicesresilience&monitoring:Hystrix

Page 7: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

GoldenageofAPIs

Source:

Page 8: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

GoldenageofAPIs

Source:

Page 9: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Microservices

Page 10: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Microservices

Source:

Page 11: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

ReactiveManifesto

Resilient

Responsive

TheReactiveManifesto

MessageDriven

Elastic

Source:

Page 12: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Messages

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

Page 13: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Message-driven:availability ofnewinformationdrivesthesystem

(vscontrolflowdriven byathread-of-execution)

Page 14: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

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

Page 15: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

PushversusPull

Erik

Alice

Bob

Carol

Anythingnewgoingon,Alice?- No,sameold

Bob?Bob?Areyouthere??…

Anythingnewonyourside,Carol?- Ohyesyou'vejustmissedalltheaction

ErikMeijerSource:

Page 16: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Push versusPull

Erik

Alice

Bob

Carol

- I'vejustfinishedmyreport

- I'msicktoday

- Something incredible ishappening here

ErikMeijerSource:

Page 17: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

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.)

Page 18: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Elastic

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

• Scaleoutorin(adding orremoving machines)• Locationtransparency,decouplingandresilience

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

Page 19: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Non-blocking

› Threadsofexecutioncompetingforasharedresourceshouldnotwaitbyblocking

› Amdahl’sLaw:contentionistheenemyofscalability

Page 20: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Amdahl’s LawThetheoreticalspeedupofaprogramislimitedbythepartthatcannotbenefitfromtheimprovement.

Source:Wikipedia

Page 21: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

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

Page 22: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

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

Page 23: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Errorrate:syncvsasync

#concurrentconnections

Requesterrorratio

RawTCPRxonRawTCP

RxHttp

Threadpool (fail fast)

Single thread

Onethreadperconnection

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

async IO,non-blocking

Page 24: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Locationtransparency

› Componentsdon’tdependonbeinglocatedsomewheretoworkandfindtheirdependencies

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

Page 25: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Resilient

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

› Loosecoupling

Source:Wikipedia (1943)

Page 26: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Source:Corellian engineering

Page 27: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Bulkheads

Source:dzone

Page 28: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Circuitbreaker

closed

open

half-open

callspassthroughcountfail/success

tripbreakerfailthreshold reached callsfailinstantly

waitforawhile

attemptcallafterwaitperiod

tripbreakeronattemptfailure

resetbreakeronattemptsuccess

Page 29: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Source:NetflixHystrix

Circuitbreaker

Page 30: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Responsive

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

+ + →

Page 31: Introduction to reactive programming · Resilient › Isolation and containment of failures (bulkheads) › Failures are messages handled as other events › Components dedicated

Summary

› Anewparadigmfordesigningsystemsintheeraofcloudcomputing

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