a journey from hexagonal architecture to event sourcing

54
A Journey from Hexagonal Architecture to Event Sourcing Carlos Buenosvinos (@buenosvinos) Polycon, October 6th, 2017 Barcelona

Upload: carlos-buenosvinos

Post on 21-Jan-2018

1.780 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: A Journey from Hexagonal Architecture to Event Sourcing

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

Polycon, October 6th, 2017 Barcelona

Page 2: A Journey from Hexagonal Architecture to Event Sourcing

2

Who am I? Carlos Buenosvinos

• Technical Director @ XING • Consultant • +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

@buenosvinos3

Page 4: A Journey from Hexagonal Architecture to Event Sourcing

4

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

I want to tell you a real story about…

5

Page 6: A Journey from Hexagonal Architecture to Event Sourcing

a Company evolving its architecture.

6

Page 7: A Journey from Hexagonal Architecture to Event Sourcing

Spaghetti Architecture

7

• 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 8: A Journey from Hexagonal Architecture to Event Sourcing

Framework Fanboy Architecture

8

• 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 9: A Journey from Hexagonal Architecture to Event Sourcing

9

Application Service

Any Random Action in a Web

Controller

Page 10: A Journey from Hexagonal Architecture to Event Sourcing

I receive an email

10

Page 11: A Journey from Hexagonal Architecture to Event Sourcing

Buenosvinos Maturity Model

11

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

* Tested with just 20 companies aprox.

Page 12: A Journey from Hexagonal Architecture to Event Sourcing

Buenosvinos Maturity Model

12

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

* Tested with just 20 companies aprox.

Page 13: A Journey from Hexagonal Architecture to Event Sourcing

Benefits of Hexagonal Architecture

13

• Separation of Concerns • Decoupled 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 14: A Journey from Hexagonal Architecture to Event Sourcing

14

Web Controller

Page 15: A Journey from Hexagonal Architecture to Event Sourcing

15

Application Service

Page 16: A Journey from Hexagonal Architecture to Event Sourcing

Moving to Hexagonal Architecture

16

• Pick an action in the Web Controller - Identify Infrastructure references

(ORM, HTTP Rest Calls, Caching, etc.) - Extract Variable and to the top

• Extract Business Logic into Application Services (Copy and Paste)

• Move Infrastructure references away - ORM flush() into Repositories

• Start Unit Testing from Application Services

Page 17: A Journey from Hexagonal Architecture to Event Sourcing

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

17

Page 18: A Journey from Hexagonal Architecture to Event Sourcing

18

Coverage going up!

Page 19: A Journey from Hexagonal Architecture to Event Sourcing

Buenosvinos Maturity Model

19

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

* Tested with just 20 companies aprox.

Page 20: A Journey from Hexagonal Architecture to Event Sourcing

At this point, you can be consultant too!

20

Page 21: A Journey from Hexagonal Architecture to Event Sourcing

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

22

Page 23: A Journey from Hexagonal Architecture to Event Sourcing

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

Domain Events to the rescue!

24

Page 25: A Journey from Hexagonal Architecture to Event Sourcing

25

Domain Event Example

Page 26: A Journey from Hexagonal Architecture to Event Sourcing

26

Firing a Domain Event

Page 27: A Journey from Hexagonal Architecture to Event Sourcing

27

Domain Event Publisher Example

Page 28: A Journey from Hexagonal Architecture to Event Sourcing

28

Domain Event Listener

(Elastic Example)

Page 29: A Journey from Hexagonal Architecture to Event Sourcing

29

Page 30: A Journey from Hexagonal Architecture to Event Sourcing

30

Domain Event Listener

(MySQL Example)

Page 31: A Journey from Hexagonal Architecture to Event Sourcing

31

Domain Event Listener

(Rabbit Example)

Page 32: A Journey from Hexagonal Architecture to Event Sourcing

32

Registering Domain Event

Listeners

Page 33: A Journey from Hexagonal Architecture to Event Sourcing

Sync all the things!

33

Page 34: A Journey from Hexagonal Architecture to Event Sourcing

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

35

Composition by Domain Events

Page 36: A Journey from Hexagonal Architecture to Event Sourcing

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

* Tested with just 20 companies aprox.

Page 37: A Journey from Hexagonal Architecture to Event Sourcing

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

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

38

Page 39: A Journey from Hexagonal Architecture to Event Sourcing

Async all the things!

39

Page 40: A Journey from Hexagonal Architecture to Event Sourcing
Page 41: A Journey from Hexagonal Architecture to Event Sourcing

Have you ever sent an email about something

that didn’t finally happen?

41

Page 42: A Journey from Hexagonal Architecture to Event Sourcing

42

Page 43: A Journey from Hexagonal Architecture to Event Sourcing
Page 44: A Journey from Hexagonal Architecture to Event Sourcing
Page 45: A Journey from Hexagonal Architecture to Event Sourcing

Problem #1: 2 Infras / 1 Tx

45

Page 46: A Journey from Hexagonal Architecture to Event Sourcing

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

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

47

Page 48: A Journey from Hexagonal Architecture to Event Sourcing
Page 49: A Journey from Hexagonal Architecture to Event Sourcing

49

Max Performance!

Page 50: A Journey from Hexagonal Architecture to Event Sourcing

Buenosvinos Maturity Model

50

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

* Tested with just 20 companies aprox.

Page 51: A Journey from Hexagonal Architecture to Event Sourcing
Page 52: A Journey from Hexagonal Architecture to Event Sourcing

Buenosvinos Maturity Model

52

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

* Tested with just 20 companies aprox.

Page 53: A Journey from Hexagonal Architecture to Event Sourcing

What should be next? Getting Ready for Event Sourcing

53

• Entities are loaded from the Events already Stored

• Our Main Database is nothing but a Cache (Can we get rid of it? How much does it cost?)

• Watch Marco Pivetta’s Presentation and/or attend his Workshop on Sunday.

Page 54: A Journey from Hexagonal Architecture to Event Sourcing

Thank you for your attention.