Top Banner
A Journey from Hexagonal Architecture to Event Sourcing Carlos Buenosvinos (@buenosvinos) SymfonyCon Cluj 2017, November 16th, 2017
57

A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Jan 21, 2018

Download

Engineering

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: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

A Journey from Hexagonal Architecture to Event SourcingCarlos Buenosvinos (@buenosvinos)

SymfonyCon Cluj 2017, November 16th, 2017

Page 2: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Who am I? Carlos Buenosvinos

• VP of Technology @ XING • Consultant as a Hobby • +10 years on Tech Leadership

(CTO, VP of Engineering, Director) • Teams up to 100 people • E-Commerce, E-Learning,

Payments, Classifieds, Recruiting • Atrápalo (500M EUR),

PCComponentes (300M EUR)

Page 3: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

@buenosvinos

Page 4: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Domain-Driven Design in PHP Book Carlos Buenosvinos, Christian Soronellas and Keyvan Akbary

https://leanpub.com/ddd-in-php

Page 5: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
Page 6: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

I want to tell you a real story about…

6

Page 7: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

a Company evolving its architecture.

7

Page 8: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Buenosvinos Maturity Model

8

Level 1: Spaghetti Level 2: Framework Fanboy Level 3: Hexagonal Architecture Level 4: Hex. + Domain Events Level 5: Stepping Stone (CQRS) Level 6: Event Sourcing + CQRS

Page 9: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Spaghetti Architecture

9

• Multiple Application Entry Points - create_user.php, delete_user.php, …

• Infrastructure and Domain Together - PHP and SQL within HTML

• Lack of Testing • Difficult to Maintain

Page 10: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Framework Fanboy Architecture

10

• Single Application Entry Point - app.php

• Some structure is present (MVC) • Still Mixing Infrastructure and Domain

- Long Controllers with SQL and Business Logic, Dummy Entities (getters and setters)

• No testing or using Web Framework • Difficult to upgrade Infrastructure

Components

Page 11: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

11

Application Service

Any Random Action in a Web

Controller

Page 12: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Benefits of Hexagonal Architecture

12

• Separation of Concerns • Promotes decoupling from the

Framework • Delays Infrastructure Decisions

- Persistence Storage or Delivery Mechanism (Web, API, CLI, etc.)

• Easy to Test - Testing Application Services (or CH) - Testing Entities - Mocking between the boundaries

Page 13: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

13

Web Controller

Page 14: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

14

Application Service

Page 15: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Moving to Hexagonal Architecture

15

1. Pick an action in the Web Controller Identify Infrastructure references (ORM, HTTP Rest Calls, Caching, etc.) Extract Variable and move them to the top 2. Extract Business Logic into Application

Services (Extract Class/Copy & Paste) 3. Move Infrastructure references away ORM flush() into add() in Repositories 4. Start Unit Testing from Application

Services

Page 16: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

New Tech Policies in the Team:

1. Everything New: Hexagonal Architecture

2. Touching an Old Feature: Boy Scout Rule ("Always leave

the campground cleaner than you found it.”)

3. 100% Coverage in the Application Services

16

Page 17: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

17

Coverage going up!

Page 18: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Buenosvinos Maturity Model

18

Level 1: Spaghetti Level 2: Framework Fanboy Level 3: Hexagonal Architecture Level 4: Hex. + Domain Events Level 5: Stepping Stone (CQRS) Level 6: Event Sourcing + CQRS

Page 19: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

At this point, you can be consultant too!

19

Page 20: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
Page 21: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

21

“… and I want to notify the user by email, logging and sending a BI tracking” Company’s Product Owner

Page 22: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

22

Page 23: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Team starts to face new issues

23

• # of Dependencies Grows • Application Service complexity Grows • More Chances to introduce Bugs

- More developers touching the same file (Main Task and Subtasks mixed)

• More Subtasks in the same feature, Worse Performance!!

Page 24: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Domain Events to the rescue!

24

Page 25: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

25

Domain Event Example

Page 26: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

26

Firing a Domain Event

Page 27: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

27

Domain Event Publisher Example

Page 28: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

28

Domain Event Listener

(Elastic Example)

Page 29: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

29

Page 30: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

30

Domain Event Listener

(MySQL Example)

Page 31: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

31

Domain Event Listener

(Rabbit Example)

Page 32: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

32

Registering Domain Event

Listeners

Page 33: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Sync all the things!

…and buy more servers

33

Page 34: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

TechPoliciesAdded (ok, it’s a bad joke!)

1. Subtasks of a Feature are developed in a different

Application Service and attached via Listener

2. Fire any new Event that Business may be interested

3. Let’s have a TV screen to monitor realtime Business

metrics to be proactive.34

Page 35: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

35

Composition by Domain Events

Page 36: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Buenosvinos Maturity Model

36

Level 1: Spaghetti Level 2: Framework Fanboy Level 3: Hexagonal Architecture Level 4: Hex. + Domain Events Level 5: Stepping Stone (CQRS) Level 6: Event Sourcing + CQRS

Page 37: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

37

“Hey team! Have you realised that the item page is getting quite slow?” Company’s Product Owner

Page 38: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Perfomance! Multiple Costly Requests (#, external, joins, etc.)

38

Page 39: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Async all the things!

39

Page 40: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

CQRS to the rescue!

40

Page 41: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

41

Page 42: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

42

Page 43: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
Page 44: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
Page 45: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Problem #1: 2 Infras / 1 Tx

45

Page 46: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

What strategies to deal with these inconsistencies can we follow?

46

• Let inconsistencies happen - The Command Handler will manage

itself (firing ArticleWasAdded, sent to RabbitMQ, but failed Database Tx)

- Feasible for most operational tasks (sending email, non-critical tracking, etc.)

• New Article Added Action and its Event persisted in the same Tx. Then Worker to move Events to Rabbit.

• Global TX (Noooooooooo!)

Page 47: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
Page 48: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Problem #2: Diff(ORM) !== Diff(Events)

48

Page 49: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
Page 50: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Changes from Hexagonal Architecture to Stepping Stone (CQRS)

50

• Entity state is still fetched from the database (read) - 3rd Normal Form - Now database is a cache

• Entities hold events triggered - Use a trait or a base class

• Repositories now only persist a bunch of events to your queue (no updates) - You will only send messages to

RabbitMQ or your queue

Page 51: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

51

Max Performance!

Page 52: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017
Page 53: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Buenosvinos Maturity Model

53

Level 1: Spaghetti Level 2: Framework Fanboy Level 3: Hexagonal Architecture Level 4: Hex. + Domain Events Level 5: Stepping Stone (CQRS) Level 6: Event Sourcing + CQRS

Page 54: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

I want it all!54

Page 55: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

What’s remaining to get to Event Sourcing?

55

• Entities state is not loaded from the database, is reconstituted from the Events from the Event Store.

• Migrate Entities one at a time.

• Enjoy!

Page 56: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Buenosvinos Maturity Model

56

Level 1: Spaghetti Level 2: Framework Fanboy Level 3: Hexagonal Architecture Level 4: Hex. + Domain Events Level 5: Stepping Stone (CQRS) Level 6: Event Sourcing + CQRS

Page 57: A Journey from Hexagonal Architecture to Event Sourcing - SymfonyCon Cluj 2017

Thank you for your attention.

https://joind.in/talk/21555 @buenosvinos