Top Banner
CQRS and Event Sourcing Nikolay Vasilev
24

CQRS and Event Sourcing

Apr 12, 2017

Download

Technology

Nikolay Vasilev
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CQRS and Event Sourcing

CQRS and Event Sourcing

Nikolay  Vasilev  

Page 2: CQRS and Event Sourcing

CQRS and Event Sourcing •  Applica0on  Architecture  •  CQRS  and  Event  Sourcing  •  Code  /  Example  •  Ques0ons  •  Resources  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 2

Page 3: CQRS and Event Sourcing

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0

Application Architecture

Page 4: CQRS and Event Sourcing

Source:  [Evans,  DDD]  

Layered Architecture •  One  of  the  oldest  paGerns  •  Each  layer  could  couple  only  to  itself    

or  below  •  Lower  levels  could  loosely  couple  

to  upper  via  Observer  or  Mediator  [GoF]  •  UI  Layer  

–  Only  renders  informa0on  –  Example:  OHS  (Remote  Façade)  

•  Applica0on  Layer  –  Very  lightweight    –  Building  Blocks:  Applica0on  Services  –  Coordinate  opera0ons  over    

Aggregates  (i.e.  express  Use  Cases)  Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 4

Page 5: CQRS and Event Sourcing

Source:  [Evans,  DDD]  

Layered Architecture •  Applica0on  Layer  

–  Register  Subscribers  for  Dom.  Events    –  Transac0ons,  Email  sending  etc.  –  Devoid  of  domain  logic  

•  Domain  Layer  –  Expressive  Behavioural-­‐Rich  Domain    

Model  

•  Infrastructure  Layer  –  Low  Level  Services  –  Focus  on  technology  specific  decisions  –  Persistence,  Messaging,  etc.  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 5

Page 6: CQRS and Event Sourcing

Source:  [Vernon,  DDD]  

Hexagonal Architecture (HA) •  Also  known  as  “Ports  and  Adapters”  •  Ports  

–  Outer  hexagon  (HTTP,  Messaging  etc.)  –  Each  hexagon’s  side  represents  a  port  –  Either  for  input  or  output  –  Do  not  implement  ports(use  e.g.  Jersey)  

•  Adapters  –  Transform  client’s  input  into  internal    

API’s  “terms”  –  Transform  internal  API’s  output  to  

input  suitable  for  clients  –  Each  client  type  has  an  Adapter  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 6

Page 7: CQRS and Event Sourcing

Source:  [Vernon,  DDD]  

Hexagonal Architecture (HA) •  Outer  hexagon  reaches  inner  

hexagon  via  applica0on’s  API  •  Design  applica0on  inside  per    

func0onal  requirements(use  cases  API)  •  Layered  Architecture  +  DI    

encourages  the  use  of  Hexagonal  Arch.  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 7

Page 8: CQRS and Event Sourcing

Event Driven Architecture (EDA) •  Event  Sourcing  

 •  Promotes,  detect,  consume  and  react  to  events  

 •  Pipes  and  Filters  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 8

EpisodeAdded  b#1,s#2  

SeriesRenamed  b#20,s#1  

BrandAc0vated  b#3  

Page 9: CQRS and Event Sourcing

Event Driven Architecture (EDA) •  EDA  could  be  combined  with    

Hexagonal  Architecture  (HA)    –  EDA  used  for  integra0on  –  HA  used  for  Bounded  Contexts  –  LA  could  replace  HA  as  well  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 9

Source:  [Vernon,  DDD]  

Page 10: CQRS and Event Sourcing

Event Driven Architecture (EDA) •  Events  as  mechanism  for  Storage  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 10

Source:  [Vernon,  DDD]  

Page 11: CQRS and Event Sourcing

Event Driven Architecture (EDA) •  Append-­‐only  architecture  

–  No  “Delete”  ac0on    

•  Distribute  easier  than  RDBMS  –  Due  to  no  locks  –  Sharding  (uses  only  Aggregate  keys)  

•  Loading  Objects  –  Loads  accumulated  events  for  an  Aggregate    –  Performance  issue  for  over  than  100  events  –  Rolling  Snapshots  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 11

EpisodeAdded  b#1,s#2  

SeriesRenamed  b#20,s#1  

SeriesRemoved  b#10,s#2  

BrandAc0vated  b#3  

4  

3  

snap  

2  

1  

Page 12: CQRS and Event Sourcing

CQRS •  Command  and  Query  Responsibility  Segrega0on  (CQRS)  •  Bertrand  Mayer’s  CQS  Principle  

–  Command:    If  method  alters  the  state  should  not  return  value  (i.e.  returns  void)  –  Query:    If  you  return  value  you  cannot  mutate  state  

•  What  if  we  apply  it  at  architectural  level?  ;)  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 12

Read Service

Write Service

Page 13: CQRS and Event Sourcing

CQRS •  Stereotypical  vs.  CQRS  Architecture  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 13

Source:  [Young,  CQRS]  

Stereotypical Architecture

Page 14: CQRS and Event Sourcing

CQRS •  Stereotypical  vs.  CQRS  Architecture  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 14

Source:  [Young,  CQRS]  

CQRS

Page 15: CQRS and Event Sourcing

CQRS and Event Sourcing

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0

Page 16: CQRS and Event Sourcing

Source:  [Young,  CQRS]  

CQRS + ES •  CQRS  +  EDA  with    

Domain  Events    

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 16

Page 17: CQRS and Event Sourcing

CQRS + ES

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 17

Source:  [Young,  CQRS]  

Page 18: CQRS and Event Sourcing

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0

Code / Example

Page 19: CQRS and Event Sourcing

Code / Example •  Example  project  

–  DDD-­‐CQRS  Sample,  (v.02)  –  HLA:        h"p://prezi.com/akrfq7jyau8w/ddd-­‐cqrs-­‐leaven-­‐v20/  

–  Code  base:      h"ps://github.com/Bo"egaIT/ddd-­‐leaven-­‐v2    –  User  Group:    h"ps://groups.google.com/forum/#!forum/ddd-­‐cqrs-­‐sample  

•  More  projects  on  official  CQRS  site  –  hGp://cqrs.wordpress.com/examples/  

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 19

Page 20: CQRS and Event Sourcing

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0

Questions?

Page 21: CQRS and Event Sourcing

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 21

Thank  you  for  your  aGen0on!  

Page 22: CQRS and Event Sourcing

Resources

Page 23: CQRS and Event Sourcing

Resources •  [Evans,  DDD]  "Domain-­‐Driven  Design:  Tackling  Complexity  in  the  Heart  of  Sonware",  Eric  

Evans,  Addison  Wesley,  20/08/2003,  ISBN:  0-­‐321-­‐12521-­‐5  •  [Vernon,  DDD],  "Implemen0ng  Domain-­‐Driven  Design",  Vaughn  Vernon,  2013,          ISBN-­‐10:  

0-­‐321-­‐83457-­‐7,  ISBN-­‐13:  978-­‐0-­‐321-­‐83457-­‐7  •  [Young,  CQRS],  "CQRS  Documents  by  Greg  Young",  Greg  Young,  11/2010.  hGp://

cqrs.files.wordpress.com/2010/11/cqrs_documents.pdf  •  [Cockburn,  Hex  Arch]  "Hexagonal  Architecture",  h"p://alistair.cockburn.us/Hexagonal

+architecture  •  [Fowler,  NoSQL  talk]  "NoSQL  maGers  Cologne  2013  -­‐  Key  Note:  NoSQL  Dis0lled  to  an  hour  -­‐  

Mar0n  Fowler",  Mar0n  Fowler,  2013,  h"p://vimeo.com/66052102    •  Domain  Driven  Design  Website,  h"p://domaindrivendesign.org/  •  DDD  User  Group,  h"p://tech.groups.yahoo.com/group/domaindrivendesign/  •  DDD  Community,  h"p://dddcommunity.org/  •  CQRS  Official  Website,  h"p://cqrs.wordpress.com/    Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 23

Page 24: CQRS and Event Sourcing

Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 24