Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

De Arquitectura Hexagonal a Event Sourcing

1.144 visualizaciones

Publicado el

Arquitecturas basadas en eventos tales como Event Sourcing tienen múltiples beneficios: escalabilidad, gestión de la complejidad, auditoría, etc. Pero llegar a ellas es duro. Nos faltan conocimientos, experiencia y coraje.

En esta charla, mostraré algunos trucos y consejos para evolucionar vuestra arquitectura actual hacia Event Sourcing paso a paso. Empezaremos desde una aplicación con arquitectura hexagonal y acabaremos a las puertas de Event Sourcing.

Publicado en: Tecnología
  • Sé el primero en comentar

De Arquitectura Hexagonal a Event Sourcing

  1. 1. De Arquitectura Hexagonal a Event Sourcing Carlos Buenosvinos (@buenosvinos) Bilbostack, 26 de Enero, 2019 Bilbao
  2. 2. Carlos Buenosvinos @buenosvinos 3 VP Technology @ XING Consultor y Formador como Hobby +10 años como CTO, VPs, Director Equipos de hasta 100 personas E-Commerce, E-Learning, Payments, Classifieds, Recruiting Atrápalo (500M EUR), PCComponentes (300M EUR)
  3. 3. Domain-Driven Design in PHP Carlos Buenosvinos, Christian Soronellas and Keyvan Akbary https://leanpub.com/ddd-in-php
  4. 4. Ansistrano https://ansistrano.com 5
  5. 5. @buenosvinos 6
  6. 6. Barcelona Valencia Remote https://corporate.xing.com/en/career/jobs/
  7. 7. a Company 9 evolving its architecture.
  8. 8. Buenosvinos Maturity Model 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 11
  9. 9. Buenosvinos Maturity Model 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 12
  10. 10. I want to tell 1 3 you a real story about…
  11. 11. a Company 1 4 evolving its architecture.
  12. 12. I don’t rewrite Apps from Scratch 15
  13. 13. 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 8
  14. 14. New App Old App Matching by URL Moving Away: Middleware Approach https://carlosbuenosvinos.com/migrating-progressively-to-symfony-without-pain-with-stackphp/
  15. 15. 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 2 0
  16. 16. 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 28
  17. 17. 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 32
  18. 18. https://carlosbuenosvinos.com/rigor-talks-php-13-refactor-use-case-i-spanish/ https://carlosbuenosvinos.com/rigor-talks-php-14-refactor-use-case-ii-spanish/ https://carlosbuenosvinos.com/rigor-talks-php-15-refactor-use-case-iii-spanish/ https://carlosbuenosvinos.com/rigor-talks-php-16-refactor-use-case-iv-spanish/ https://carlosbuenosvinos.com/rigor-talks-php-17-refactor-use-case-v-spanish/ https://carlosbuenosvinos.com/rigor-talks-php-18-tell-dont-ask-spanish/ https://carlosbuenosvinos.com/rigor-talks-php-19-refactor-use-case-vi-spanish/
  19. 19. 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 34
  20. 20. Coverage going up! 35
  21. 21. Buenosvinos Maturity Model 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 36
  22. 22. At this point, 37 you can be consultant too!
  23. 23. “… and I want to notify the user by email, logging and sending a BI tracking” Company’s Product Owner 38
  24. 24. 2 2
  25. 25. 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!! 40
  26. 26. Domain Events 41 to the rescue!
  27. 27. Domain Event Example 42
  28. 28. Firing a Domain Event 43
  29. 29. Domain Event Publisher Example 44
  30. 30. Domain Event Listener (Elastic Example) 45
  31. 31. Domain Event Listener (MySQL Example) 47
  32. 32. Domain Event Listener (Rabbit Example) 48
  33. 33. Registering Domain Event Listeners 49
  34. 34. Sync all the things! 50
  35. 35. 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. 51
  36. 36. Composition by Domain Events 52
  37. 37. Buenosvinos Maturity Model 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 53
  38. 38. “Hey team! Have you realised that the item page is getting quite slow?” Company’s Product Owner 54
  39. 39. Perfomance! 55 Multiple Costly Requests (#, external, joins, etc.)
  40. 40. Async all the things! 56
  41. 41. 57
  42. 42. Restaurant Page 1 Query Redis o Elastic WorkerDomain Event Command (Application Service) QUERY COMMAND RESPONSIBILITY SEGREGATION (CQRS)
  43. 43. NOT PROPER CQRS
  44. 44. Problem #1: 2 Infras / 1 Tx 61
  45. 45. 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!) 62
  46. 46. Problem #2: Diff(ORM) !== Diff(Events) 63
  47. 47. Max Performance! 66
  48. 48. Buenosvinos Maturity Model 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. 67
  49. 49. Buenosvinos Maturity Model 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 69
  50. 50. 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 70
  51. 51. Our Main Database is nothing but a Cache (Can we get rid of it? How much does it cost?) 72
  52. 52. Buenosvinos Maturity Model 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 76
  53. 53. @buenosvinos 7 8
  54. 54. Thank you for your attention.

×