Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... ·...
Transcript of Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... ·...
![Page 1: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/1.jpg)
1
ImplementingEvent-DrivenMicroservices
NikhilBarthwalwww.nikhilbarthwal.com
2018
![Page 2: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/2.jpg)
Agenda(1/2)
• MotivationstomovefromMonolithictoMicroservices• UnderstandingtheDistributedDatainMicroservices• UsingDomainDrivenDesignforpartitioningdata• ProblemswithDistributedData• EventDrivenMicroservices
2
![Page 3: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/3.jpg)
Agenda(2/2)
• CommunicationPatternsforMicroservices• EventSourcing• CommandQueryResponsibilitySegregation• IntroducingSagas• Resourcesforfurtherreading
3
![Page 4: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/4.jpg)
Monolithic:Theoldschoolapproach…
4
Database
WebService
EasytoDevelop,TestandDeploy…
Butdoesn’tscale!
• Largecodebase→Slowerdevelopment• Moretimetotest&deploy• Toomuchteaminterdependency
![Page 5: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/5.jpg)
Microservices:TheBravenewworld...
• Smallercodebasetounderstand&change• Autonomousteams• Fastertesting&deploy
5
AssemblyofLooselycouplesservices!ImageSource:https://www.slideshare.net/JontheBeach/distributed-sagas-a-protocol-for-coordinating-microservices
![Page 6: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/6.jpg)
LooseCouplingimpliesDistributedData
6
DatabaseperService!
FasterInnovation!
Looselycoupledservices
Reducesteaminterdependencies
ImageSource:http://microservices.io/patterns/microservices.html
![Page 7: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/7.jpg)
DomainDrivenDesign(DDD)
7
An approach to software development for complex needs byconnecting the implementation to an evolving model.
• Entities• ValueObjects• BoundedContext• Aggregates InfrastructureLayer
DomainLayer
ApplicationLayer
UserInterface
![Page 8: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/8.jpg)
DDD:Aggregates
8
• Clusterofdomainobjectsthatcanbetreatedasasingleunit
• Oneofitscomponentisroot• Alloutsidereferencewouldonlygototheaggregateroot
DomainModel=
CollectionofAggregates
![Page 9: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/9.jpg)
DataPartitionStrategies:UseDDD
9
Aggregatesprovide
boundariesfor
partition
![Page 10: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/10.jpg)
ProblemswithDistributedData
• Howdowekeepdataconsistent?• Howdowequeryscattereddata?
10
![Page 11: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/11.jpg)
CannotuseACIDTransactions
BEGIN TRANSACTION
SELECT ADDRESSFROM CUSTOMERSWHERE CUSTOMER_ID=XXX
SELECT PRICEFROM PRODUCTSWHERE CUSTOMER_ID=YYY
INSERT INTO ORDERS...
COMMIT TRANSACTION
11
PrivatetoCustomerservice
PrivatetoProductservice
![Page 12: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/12.jpg)
Cannotuse2-PhaseCommit
• Coordinatorisasinglepointoffailure• Verychatty,withO(4n)messages,O(n2)withretries• Lockswoulddecreasethroughputs• Impactsavailability(CAPtheoremlimitations)
12
Guaranteesconsistency,but…
![Page 13: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/13.jpg)
ConsistencyEventualConsistency
Availability
Partition(Network)
EventualConsistency
13
UseEventDrivenMicroservice Architecture!
![Page 14: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/14.jpg)
EventDrivenArchitecture:Introduction
14
• Eventoccurswhenachangehappensinsystem• Alllistenersgetnotifiedoftheevent,maytakeaction• Highlydistributed/looselycoupledarchitecture• Oftenusedforasynchronousflowsofinformation
![Page 15: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/15.jpg)
Communication:DirectMessaging
15
Benefits:
• Easierimplementation
• WorkswellwithServerless
Drawbacks:
• Increasedcoupling
• Difficulttoscale
OrderService
ProductService
CustomerService
FulfillmentService
PaymentService
ShippingService
![Page 16: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/16.jpg)
Communication:ViaMessagebroker
16
Benefits:
• Morescalable
• IncreasedDecoupling
• BetterErrorcontrol
Drawbacks:
• RequiresBrokerasHighlyAvailabile component
OrderServiceProductService
CustomerService
FulfillmentService
PaymentService
ShippingService
MessageBroker
![Page 17: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/17.jpg)
CommunicationPatterns
17
Request/Response(synchronous)
Event-Driven
(asynchronous)Tier-3:LittletoNoimpactforCustomer
Tier-2:ImpactingCustomersindirectly
Tier-1:DirectlyCustomerfacing
![Page 18: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/18.jpg)
EventSourcing:Benefits&Drawbacks
18
Benefits:• 100%accurateauditlogging• Easytemporalqueries• Applicationscanprocesssameeventsbutcreateviews
Drawbacks:• AddsComplexity• NoStrictConsistency• Longerbootuptimes(Snapshotscanhelp)
![Page 19: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/19.jpg)
EventSourcing:Introduction
19
• Modelingstatechangesassequenceofevents• Storingtheeventthatcouldtriggerthestatechange• Enablesrollingbacktoparticulartimeinhistory
Examples:
![Page 20: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/20.jpg)
EventSourcing:Multipleviews
20ImageSource:https://www.confluent.io/wp-content/uploads/2016/09/Event-sourced-based-architecture.jpeg
Addingapplicationsthatprocessevent…
butcreateadifferentview!
![Page 21: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/21.jpg)
21
CQRSPattern
CommandQueryResponsibilitySegregation
MustforEventSourcing!
Read
Write
ImageSource:https://martinfowler.com/bliki/CQRS.html
![Page 22: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/22.jpg)
CQRS:Benefits&Drawbacks
22
Benefits:• NeededforEventSourcing• Improvedseparationofconcerns• Supportsscalablemultipledenormalizedviews
Drawbacks:• Increasedcomplexity• Potentialcodeduplication• ReplicationlagasNoStrictConsistency
![Page 23: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/23.jpg)
Sagas:Introduction
23
• Basedona1987paper• Initiallyforasingledatabaserunningononenode
• Nowadaptedfordistributedsystemswithasynchronyandpartialfailure
![Page 24: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/24.jpg)
Transaction&RollbackTransaction
• EveryTransactionhasaRollbacktransaction• Thislogicmustbeincludedintheservice
24
T1 T2 T3
R1 R2 R3
Transactions→
RollbackTransactions→
![Page 25: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/25.jpg)
Sagas:Example
Thisactioninitiatesthesaga
25
CheckCreditService WithdrawalService DepositService
DepositCheck
Longrunningtransactions…usecompensatingactionstohandlefailures!
DoesSenderhasenoughmoneyinhis
account?
Withdrawthemoney(unlessitisonhold)?
Depositthemoney(unlessaccountisdeactivated)?
![Page 26: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/26.jpg)
DistributedSaga:Example
26
Requests:
• Idempotent
• CanAbort
CompensatingRequests:
• Idempotent
• Commutative
• CanNotAbort
Source:https://speakerdeck.com/caitiem20/distributed-sagas-a-protocol-for-coordinating-microservices
![Page 27: Implementing Event-Driven Microservicescontinuouslifecycle.london/wp-content/uploads/2017/... · Event Driven Architecture: Introduction 14 •Event occurs when a change happens in](https://reader030.fdocuments.in/reader030/viewer/2022040608/5ec95a092defd62bb266b5af/html5/thumbnails/27.jpg)
Resources
• UsingDDDtodevelopMicroservices:https://www.infoq.com/news/2016/02/ddd-microservices
• OverviewofSagaPattern:http://microservices.io/patterns/data/saga.html
• DistributedSagas:https://www.slideshare.net/JontheBeach/distributed-sagas-a-protocol-for-coordinating-microservices
27