Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Growing Hexagonal Microservices by TDD

Lemi Orhan Ergin
September 12, 2020

Growing Hexagonal Microservices by TDD

As Simon Brown says: “If you can’t build a well-structured monolith, what makes you think microservices is the answer?”
Hexagonal architecture is a model for designing extensible, modular software applications. The idea behind it is to isolate the domain of your application from outside concerns. If hexagonal architecture is structured properly, YAGNI principle can shape our scale needs and microservices born from well-crafted monoliths easier than ever.

A developer produces considerable amount of spaghetti/lasagna code and technical debt throughout her/his career. Hexagonal architecture lets developers focus on separation of concerns, therefore leads to maintainable and extensible codebase. It can also be called as Beyti code (Beyti is a Turkish-based well-known kebab type), named due to similarities with isolation layers, the core inside and repetitive structures.

Even though hexagonal architecture is somewhat easy to understand, building it well-crafted is a hard skill. Let us show how we grow hexagonal microservices from monoliths by using TDD. We will cover the following inter-related topics as well.

* TDD touching consumer driver contract tests, unit tests, dockerized integration tests.
* Evolution of a codebase, from a modular monolith to well-crafted microservices.
* Why mono repository is important for building hexagonal microservices and what are the cures to main headaches of mono repository.

Note: These are the slides I and Alican Akkuş presented at Java Day Istanbul 2020.

Lemi Orhan Ergin

September 12, 2020
Tweet

More Decks by Lemi Orhan Ergin

Other Decks in Technology

Transcript

  1. lemi orhan ergin co-founder, ali can akkuş software crafter, developing

    products, Craftbase alumni, Sony, eBay, ACM, iyzico founder, Software Craftsmanship Turkey programming, since 2001 with love speakerdeck.com/lemiorhan @lemiorhan developing products, Craftbase alumni, 32bit, iyzico practitioner, extreme programming jvm stack, java, groovy, spring alicanakkus.github.io @Alican_Akkus
  2. Layered architecture Web Domain Persistence From a certain layer, we

    can only access components in the same layer or in a layer below. LAYERS OF Isolation
  3. No way to understand what the code is about Domain

    logic is sca!ered throughout the layers Hard to understand where domain logic is Layers encourage horizontal coupling DDD do not work on partitioned architecture Many uncategorized components Application
  4. Your architectures should tell readers about the system, not about

    the frameworks you used in your system. SCREAMING ARCHITECTURE https://blog.cleancoder.com/uncle-bob/2011/09/30/Screaming-Architecture.html Screaming Architecture, Robert C. Martin Application
  5. Web Domain Persistence Architecture sinkhole anti-pa!ern The fast lane reader

    anti-pa!ern You have to be self-disciplined Domain logic sca!ered throughout the layers Database/state drives the design Uncategorized helper and utility classes Monolith in nature Changing existing code instead of adding new Hard to change over time When it grows, it’s hard to test Open for hacking your own code Documentation is critical The Inconvenient truth
  6. Robert C. Martin Author of Clean Code and Clean Coder

    Owner of cleancoders.com training site The very first value of so"ware is to tolerate and facilitate on-going changes
  7. Make future changes easy No chain reaction of modification Big

    impact with few changes No workarounds or “just once” hacks Avoid accidental complexity Easy testing and verification MAINTAINABILITY The very first value of so"ware is to tolerate and facilitate on-going changes
  8. Immune to technical evolution Easy to add new integrations Delay

    technological decisions Testability in isolation Framework independent domain FLEXIBILITY The very first value of so"ware is to tolerate and facilitate on-going changes
  9. A timeless goal of so"ware engineering has been to separate

    code that changes frequently from code that is stable. James Coplien Quote from his „Lean Architecture‰ book
  10. A timeless goal of so"ware engineering has been to separate

    code that changes frequently from code that is stable. DOMAIN INFRASTRUCTURE Business logic Constraints and Behaviors What we really care about Integrations with infra components Technology and framework dependent How we make domain work
  11. A timeless goal of so"ware engineering has been to separate

    code that changes frequently from code that is stable. DOMAIN INFRASTRUCTURE Heart of the so!ware The detail
  12. infra domain main component framework layer running at startup and

    builds the whole system technology agnostic core of application contains constraints and behaviors of the domain technology dependent layer that interacts with external devices via integration points
  13. user service repository adapter user service repository port DEPENDENCY INVERSION

    abstractions should not depend on details details should depend on abstractions high-level modules should not depend on low-level modules both should depend on abstractions LOW LEVEL MODULE DEPENDENCY INVERTED DEPENDENCY DIRECTED HIGH LEVEL MODULE
  14. infra domain rest api web ui 3rd party apps command

    line soap calls admin gui batch jobs mobile apps other hexagons THE LEFT SIDE we find the actors who drive the domain adapts requests from the outside to our domain user side, primary adapters
  15. infra domain adapter use facade THE LEFT SIDE we find

    the actors who drive the domain adapts requests from the outside to our domain user side, primary adapters
  16. infra domain mysql adapter stripe adapter cache provider elastic search

    mail server database sms provider message queue other hexagons THE RIGHT SIDE integrations with the components that the application interacts with functionality needed by the application for implementing the business logic framework side, secondary adapters
  17. infra domain port adapter use implement THE RIGHT SIDE integrations

    with the components that the application interacts with functionality needed by the application for implementing the business logic framework side, secondary adapters
  18. infra stripe adapter rest api mysql adapter web ui domain

    notification payment order basket restaurant scheduling caching mail account messaging billing search
  19. domain facade use use model use business logic use port

    mock TEST DRIVEN DEVELOPMENT main sub-product is unit tests test INSIDE-OUT TDD
  20. infra domain adapter implement port use use model entity use

    email client test FUNCTIONAL AND INTEGRATION TESTS tdd generates unit tests in addition to it mock client fake with wiremock
  21. TEST TYPES AT HEXAGONALS Domain Infra Unit Tests Unit Tests

    Integration Tests Component Tests Contract Tests External Verifier Stub Tests Functional Tests io.craftbase.orderapi io.craftbase.orderapi
  22. how you go is always as important as where you

    go @lemiorhan LEMi ORHAN ERGiN @alican_akkus ALİ CAN AKKUŞ