CQRS and Event Sourcing
Nikolay	
  Vasilev	
  
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
Bulgarian Java User Group
Nikolay Vasilev, CC BY-SA 3.0
Application Architecture
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
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
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
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
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	
  
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]	
  
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]	
  
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	
  
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
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
CQRS
•  Stereotypical	
  vs.	
  CQRS	
  Architecture	
  
Bulgarian Java User Group
Nikolay Vasilev, CC BY-SA 3.0
CQRS + ES Page 14
Source:	
  [Young,	
  CQRS]	
  
CQRS
CQRS and Event Sourcing
Bulgarian Java User Group
Nikolay Vasilev, CC BY-SA 3.0
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
CQRS + ES
Bulgarian Java User Group
Nikolay Vasilev, CC BY-SA 3.0
CQRS + ES Page 17
Source:	
  [Young,	
  CQRS]	
  
Bulgarian Java User Group
Nikolay Vasilev, CC BY-SA 3.0
Code / Example
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
Bulgarian Java User Group
Nikolay Vasilev, CC BY-SA 3.0
Questions?
Bulgarian Java User Group
Nikolay Vasilev, CC BY-SA 3.0
CQRS + ES Page 21
Thank	
  you	
  for	
  your	
  aGen0on!	
  
Resources
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
Bulgarian Java User Group
Nikolay Vasilev, CC BY-SA 3.0
CQRS + ES Page 24

CQRS and Event Sourcing

  • 1.
    CQRS and EventSourcing Nikolay  Vasilev  
  • 2.
    CQRS and EventSourcing •  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
  • 3.
    Bulgarian Java UserGroup Nikolay Vasilev, CC BY-SA 3.0 Application Architecture
  • 4.
    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
  • 5.
    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
  • 6.
    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
  • 7.
    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
  • 8.
    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  
  • 9.
    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]  
  • 10.
    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]  
  • 11.
    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  
  • 12.
    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
  • 13.
    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
  • 14.
    CQRS •  Stereotypical  vs.  CQRS  Architecture   Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 14 Source:  [Young,  CQRS]   CQRS
  • 15.
    CQRS and EventSourcing Bulgarian Java User Group Nikolay Vasilev, CC BY-SA 3.0
  • 16.
    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
  • 17.
    CQRS + ES BulgarianJava User Group Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 17 Source:  [Young,  CQRS]  
  • 18.
    Bulgarian Java UserGroup Nikolay Vasilev, CC BY-SA 3.0 Code / Example
  • 19.
    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
  • 20.
    Bulgarian Java UserGroup Nikolay Vasilev, CC BY-SA 3.0 Questions?
  • 21.
    Bulgarian Java UserGroup Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 21 Thank  you  for  your  aGen0on!  
  • 22.
  • 23.
    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
  • 24.
    Bulgarian Java UserGroup Nikolay Vasilev, CC BY-SA 3.0 CQRS + ES Page 24