Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
1
ImplementingEvent-DrivenMicroservices
NikhilBarthwalwww.nikhilbarthwal.com
2018
Agenda(1/2)
• MotivationstomovefromMonolithictoMicroservices• UnderstandingtheDistributedDatainMicroservices• UsingDomainDrivenDesignforpartitioningdata• ProblemswithDistributedData• EventDrivenMicroservices
2
Agenda(2/2)
• CommunicationPatternsforMicroservices• EventSourcing• CommandQueryResponsibilitySegregation• IntroducingSagas• Resourcesforfurtherreading
3
Monolithic:Theoldschoolapproach…
4
Database
WebService
EasytoDevelop,TestandDeploy…
Butdoesn’tscale!
• Largecodebase→Slowerdevelopment• Moretimetotest&deploy• Toomuchteaminterdependency
Microservices:TheBravenewworld...
• Smallercodebasetounderstand&change• Autonomousteams• Fastertesting&deploy
5
AssemblyofLooselycouplesservices!ImageSource:https://www.slideshare.net/JontheBeach/distributed-sagas-a-protocol-for-coordinating-microservices
LooseCouplingimpliesDistributedData
6
DatabaseperService!
FasterInnovation!
Looselycoupledservices
Reducesteaminterdependencies
ImageSource:http://microservices.io/patterns/microservices.html
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
DDD:Aggregates
8
• Clusterofdomainobjectsthatcanbetreatedasasingleunit
• Oneofitscomponentisroot• Alloutsidereferencewouldonlygototheaggregateroot
DomainModel=
CollectionofAggregates
DataPartitionStrategies:UseDDD
9
Aggregatesprovide
boundariesfor
partition
ProblemswithDistributedData
• Howdowekeepdataconsistent?• Howdowequeryscattereddata?
10
CannotuseACIDTransactions
BEGIN TRANSACTION
SELECT ADDRESSFROM CUSTOMERSWHERE CUSTOMER_ID=XXX
SELECT PRICEFROM PRODUCTSWHERE CUSTOMER_ID=YYY
INSERT INTO ORDERS...
COMMIT TRANSACTION
11
PrivatetoCustomerservice
PrivatetoProductservice
Cannotuse2-PhaseCommit
• Coordinatorisasinglepointoffailure• Verychatty,withO(4n)messages,O(n2)withretries• Lockswoulddecreasethroughputs• Impactsavailability(CAPtheoremlimitations)
12
Guaranteesconsistency,but…
ConsistencyEventualConsistency
Availability
Partition(Network)
EventualConsistency
13
UseEventDrivenMicroservice Architecture!
EventDrivenArchitecture:Introduction
14
• Eventoccurswhenachangehappensinsystem• Alllistenersgetnotifiedoftheevent,maytakeaction• Highlydistributed/looselycoupledarchitecture• Oftenusedforasynchronousflowsofinformation
Communication:DirectMessaging
15
Benefits:
• Easierimplementation
• WorkswellwithServerless
Drawbacks:
• Increasedcoupling
• Difficulttoscale
OrderService
ProductService
CustomerService
FulfillmentService
PaymentService
ShippingService
Communication:ViaMessagebroker
16
Benefits:
• Morescalable
• IncreasedDecoupling
• BetterErrorcontrol
Drawbacks:
• RequiresBrokerasHighlyAvailabile component
OrderServiceProductService
CustomerService
FulfillmentService
PaymentService
ShippingService
MessageBroker
CommunicationPatterns
17
Request/Response(synchronous)
Event-Driven
(asynchronous)Tier-3:LittletoNoimpactforCustomer
Tier-2:ImpactingCustomersindirectly
Tier-1:DirectlyCustomerfacing
EventSourcing:Benefits&Drawbacks
18
Benefits:• 100%accurateauditlogging• Easytemporalqueries• Applicationscanprocesssameeventsbutcreateviews
Drawbacks:• AddsComplexity• NoStrictConsistency• Longerbootuptimes(Snapshotscanhelp)
EventSourcing:Introduction
19
• Modelingstatechangesassequenceofevents• Storingtheeventthatcouldtriggerthestatechange• Enablesrollingbacktoparticulartimeinhistory
Examples:
EventSourcing:Multipleviews
20ImageSource:https://www.confluent.io/wp-content/uploads/2016/09/Event-sourced-based-architecture.jpeg
Addingapplicationsthatprocessevent…
butcreateadifferentview!
21
CQRSPattern
CommandQueryResponsibilitySegregation
MustforEventSourcing!
Read
Write
ImageSource:https://martinfowler.com/bliki/CQRS.html
CQRS:Benefits&Drawbacks
22
Benefits:• NeededforEventSourcing• Improvedseparationofconcerns• Supportsscalablemultipledenormalizedviews
Drawbacks:• Increasedcomplexity• Potentialcodeduplication• ReplicationlagasNoStrictConsistency
Sagas:Introduction
23
• Basedona1987paper• Initiallyforasingledatabaserunningononenode
• Nowadaptedfordistributedsystemswithasynchronyandpartialfailure
Transaction&RollbackTransaction
• EveryTransactionhasaRollbacktransaction• Thislogicmustbeincludedintheservice
24
T1 T2 T3
R1 R2 R3
Transactions→
RollbackTransactions→
Sagas:Example
Thisactioninitiatesthesaga
25
CheckCreditService WithdrawalService DepositService
DepositCheck
Longrunningtransactions…usecompensatingactionstohandlefailures!
DoesSenderhasenoughmoneyinhis
account?
Withdrawthemoney(unlessitisonhold)?
Depositthemoney(unlessaccountisdeactivated)?
DistributedSaga:Example
26
Requests:
• Idempotent
• CanAbort
CompensatingRequests:
• Idempotent
• Commutative
• CanNotAbort
Source:https://speakerdeck.com/caitiem20/distributed-sagas-a-protocol-for-coordinating-microservices
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