Injustice - Developers Among Us (SciFiDevCon 2024)
Road From Hex. Architecture to Event Sourcing
1. Road from Hex. Architecture
to Event Sourcing
Carlos Buenosvinos (@buenosvinos)
February, 05th , 2019
Porto
2. Carlos Buenosvinos
@buenosvinos
2
VP Technology @ XING
Consultant and Trainer as Hobby
+10 years as CTO, VPs, Director
Teams up to 100 people
E-Commerce, E-Learning,
Payments, Classifieds, Recruiting
Atrápalo (500M EUR),
PCComponentes (300M EUR)
16. Level 1:
Spaghetti Architecture
• 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
1
6
17. New App Old App
Matching by
URL
Moving Away:
Middleware Approach
https://carlosbuenosvinos.com/migrating-progressively-to-symfony-without-pain-with-stackphp/
18. Level 2:
Layered Architecture /
Framework Fanboy Architecture
• 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
1
8
19.
20.
21.
22.
23.
24.
25.
26. Moving to
Hexagonal Architecture
• Pick an action in the Web Controller
- Coloring Exercise: 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
- Interfaces Infrastructure and the Rest
- Database Tx can be move to Repositories
• Start Unit Testing from Application Services 26
27.
28.
29.
30. Benefits of
Hexagonal Architecture
• 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
30
32. 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
32
38. Team starts to
face new issues
• # 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!!
38
49. 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.
49
60. What strategies to deal with these
inconsistencies can we follow?
• 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!)
60
68. Moving to Event Sourcing
• Every Entity is reconstituted from
Events
• Benefits:
• Historical Logs of our Entities
• Audit Environments
• Realtime processing
• Reproduce environments up to
specific state
68
69.
70. Our Main Database is nothing
but a Cache (Can we get rid
of it? How much does it cost?)
70