28
1 Implementing Event-Driven Microservices Nikhil Barthwal www.nikhilbarthwal.com 2018

Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

1

ImplementingEvent-DrivenMicroservices

NikhilBarthwalwww.nikhilbarthwal.com

2018

Page 2: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Agenda(1/2)

• MotivationstomovefromMonolithictoMicroservices• UnderstandingtheDistributedDatainMicroservices• UsingDomainDrivenDesignforpartitioningdata• ProblemswithDistributedData• EventDrivenMicroservices

2

Page 3: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Agenda(2/2)

• CommunicationPatternsforMicroservices• EventSourcing• CommandQueryResponsibilitySegregation• IntroducingSagas• Resourcesforfurtherreading

3

Page 4: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Monolithic:Theoldschoolapproach…

4

Database

WebService

EasytoDevelop,TestandDeploy…

Butdoesn’tscale!

• Largecodebase→Slowerdevelopment• Moretimetotest&deploy• Toomuchteaminterdependency

Page 5: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

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 Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

LooseCouplingimpliesDistributedData

6

DatabaseperService!

FasterInnovation!

Looselycoupledservices

Reducesteaminterdependencies

ImageSource:http://microservices.io/patterns/microservices.html

Page 7: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

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 Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

DDD:Aggregates

8

• Clusterofdomainobjectsthatcanbetreatedasasingleunit

• Oneofitscomponentisroot• Alloutsidereferencewouldonlygototheaggregateroot

DomainModel=

CollectionofAggregates

Page 9: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

DataPartitionStrategies:UseDDD

9

Aggregatesprovide

boundariesfor

partition

Page 10: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

ProblemswithDistributedData

• Howdowekeepdataconsistent?• Howdowequeryscattereddata?

10

Page 11: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

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 Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Cannotuse2-PhaseCommit

• Coordinatorisasinglepointoffailure• Verychatty,withO(4n)messages,O(n2)withretries• Lockswoulddecreasethroughputs• Impactsavailability(CAPtheoremlimitations)

12

Guaranteesconsistency,but…

Page 13: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

ConsistencyEventualConsistency

Availability

Partition(Network)

EventualConsistency

13

UseEventDrivenMicroservice Architecture!

Page 14: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

EventDrivenArchitecture:Introduction

14

• Eventoccurswhenachangehappensinsystem• Alllistenersgetnotifiedoftheevent,maytakeaction• Highlydistributed/looselycoupledarchitecture• Oftenusedforasynchronousflowsofinformation

Page 15: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Communication:DirectMessaging

15

Benefits:

• Easierimplementation

• WorkswellwithServerless

Drawbacks:

• Increasedcoupling

• Difficulttoscale

OrderService

ProductService

CustomerService

FulfillmentService

PaymentService

ShippingService

Page 16: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Communication:ViaMessagebroker

16

Benefits:

• Morescalable

• IncreasedDecoupling

• BetterErrorcontrol

Drawbacks:

• RequiresBrokerasHighlyAvailabile component

OrderServiceProductService

CustomerService

FulfillmentService

PaymentService

ShippingService

MessageBroker

Page 17: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

CommunicationPatterns

17

Request/Response(synchronous)

Event-Driven

(asynchronous)Tier-3:LittletoNoimpactforCustomer

Tier-2:ImpactingCustomersindirectly

Tier-1:DirectlyCustomerfacing

Page 18: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

EventSourcing:Benefits&Drawbacks

18

Benefits:• 100%accurateauditlogging• Easytemporalqueries• Applicationscanprocesssameeventsbutcreateviews

Drawbacks:• AddsComplexity• NoStrictConsistency• Longerbootuptimes(Snapshotscanhelp)

Page 19: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

EventSourcing:Introduction

19

• Modelingstatechangesassequenceofevents• Storingtheeventthatcouldtriggerthestatechange• Enablesrollingbacktoparticulartimeinhistory

Examples:

Page 20: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

EventSourcing:Multipleviews

20ImageSource:https://www.confluent.io/wp-content/uploads/2016/09/Event-sourced-based-architecture.jpeg

Addingapplicationsthatprocessevent…

butcreateadifferentview!

Page 21: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

21

CQRSPattern

CommandQueryResponsibilitySegregation

MustforEventSourcing!

Read

Write

ImageSource:https://martinfowler.com/bliki/CQRS.html

Page 22: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

CQRS:Benefits&Drawbacks

22

Benefits:• NeededforEventSourcing• Improvedseparationofconcerns• Supportsscalablemultipledenormalizedviews

Drawbacks:• Increasedcomplexity• Potentialcodeduplication• ReplicationlagasNoStrictConsistency

Page 23: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Sagas:Introduction

23

• Basedona1987paper• Initiallyforasingledatabaserunningononenode

• Nowadaptedfordistributedsystemswithasynchronyandpartialfailure

Page 24: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Transaction&RollbackTransaction

• EveryTransactionhasaRollbacktransaction• Thislogicmustbeincludedintheservice

24

T1 T2 T3

R1 R2 R3

Transactions→

RollbackTransactions→

Page 25: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

Sagas:Example

Thisactioninitiatesthesaga

25

CheckCreditService WithdrawalService DepositService

DepositCheck

Longrunningtransactions…usecompensatingactionstohandlefailures!

DoesSenderhasenoughmoneyinhis

account?

Withdrawthemoney(unlessitisonhold)?

Depositthemoney(unlessaccountisdeactivated)?

Page 26: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

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 Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

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

Page 28: Implementing Event-Driven Microservices2018.continuouslifecycle.london/wp-content/uploads/...DDD: Aggregates 8 •Cluster of domain objects that can be treated as a single unit •One

28

Questions?

NikhilBarthwal

@[email protected]