SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Hexagonal Architecture
Ports & Adapters Pattern
1
Foreword
● I call myself Tung, & you can do that too!
● I identify as an Engineer
● Please feel free to interrupt me anytime for any question :)
2
Agenda
● A Long Introduction
○ What is an Architecture?
○ Backend Architectures
● Hexagonal Architecture
○ The Hexagon
○ The Actors
○ The Ports
○ The Adapters
● Summary, Pros, & Cons
3
What comes to your mind when you hear “architecture”?
● That depends!
● The off-the-top-of-one’s-head answer varies depending on:
○ Your background
○ Your most recent / pending task
○ Your pain point at the moment
○ …
4
The famous (& boring) 3-layer architecture
5
There are many variants of this 3-layer thingy
6
Let’s focus on the backend
● Monolith
● Micro services
→ Let’s jump into a service :)
7
8
MVC
9
There’s 3-layer thingy for the backend alone
10
It’s really popular!
11
There are too many!
● The point is that these architectures are not mutually exclusive
● The frontend - backend boundary could be very fluid
○ “Backend” can use template engine to render web pages
○ “Frontend” can run code on the server to render parts of the pages there
● How small a service has to be to be called “micro”?
○ Micro services architecture with only 1 big service
○ “Nano” services architecture with lazy services
■ & tons of mappers to convert all the DTOs to-and-fro
12
Nothing is perfect!
● A service, reasonably sized
● 3-layer Controller - Service - Persistence
● & still a big ball of mud in there
→ These architectures are just ways to see, to organize things
13
Even hexagonal architecture
“Ports & Adapters pattern says nothing about the structure of the inside of the
hexagon. You can have layers… you can have components by feature… you can
have spaghetti code… you can have a Big Ball of Mud… you can apply DDD
tactical patterns… you can have a single CRUD… it’s up to you.”
14
15
The hexagon
● The core, pure business logic
● Technology agnostic
→ Could not be 100% so, as it still has to be written using a programming
language and therefore, influenced by that language
16
The actors
1. Drivers / Primary Actors → trigger interaction
2. Driven / Secondary Actors → are triggered by the application
a. Repository: 2-way exchange of info
b. Recipient: 1-way from application
● For knowing which kind is the actor in an application-actor interaction, ask
yourself “who” triggers the conversation
○ If the answer is “the actor” then it is a driver
○ If the answer is “the application” then the actor is a driven actor
17
18
The ports
● Ports are the application boundary, in the picture a port is an edge of the
hexagon
● From the outside world, actors can only interact with the hexagon ports, they
shouldn’t be able to access the inside of the hexagon
● An important thing to remark is that ports belong to the application
● Driver ports → API, features that the application provides to outside world
● Driven ports → SPI needed, dependencies of that application
19
The adapters
● Given a port, there may be an adapter for each desired technology that we
want to use
● Adapters are outside the application
● A driver adapter uses a driver port interface, converting a specific
technology request into a technology agnostic request to a driver port
○ E.g. from a REST call to a java method call (Controller class calls Service class)
● A driven adapter implements a driven port interface, converting the
technology agnostic methods of the port into specific technology methods
○ E.g. from a java method call to a push to a message queue (Service class calls Queue Client
class)
○ or from a java method call to a SQL query (Service class calls Repository class)
20
21
22
Summary of hexagonal architecture
1. The Hexagon → the application
a. Driver Ports → API offered by the application
b. Driven Ports → SPI required by the application
2. Actors → environment devices that interact with the application
a. Drivers → application users (either humans or hardware/software devices)
b. Driven Actors → provide services required by the application
3. Adapters → adapt specific technology to the application
a. Driver Adapters → use the drivers ports
b. Driven Adapters → implement the driven ports
23
The Main Component
This component will run at startup and it builds the whole system doing the
following:
● It initializes and configures the environment (databases, servers, …)
● For each driven port:
○ It chooses a driven adapter implementing the port, and creates an instance of the adapter
○ It creates an instance of the application injecting the driven adapters instances into the
application constructor
● For each driver port:
○ It chooses a driver adapter that uses the port, and creates an instance of the adapter, injecting
the application instance into the adapter constructor
○ It runs the driver adapter instance
24
Pros
Flexibility & immunity to technology evolution
● Swapping between different technologies is easy
○ For a given port, you can have multiple adapters, each one using a specific technology
○ For choosing one of them, you just have to configure which adapter to use for that port
● The technology you want to upgrade is located at an adapter outside the
application
○ Technology evolves more frequently than business logic does
○ You just have to change the adapter
○ The application itself remains immutable because it doesn’t depend on adapters
25
Pros
Delay technological decisions
● Focus just on business logic
○ Start with the application itself (ports are part of the application)
○ Choose a technology later, & create an adapter for it
26
Cons
Complexity
● A lot of modules and explicit dependencies defined between them
○ Modules in the sense of Maven / Gradle modules / subprojects
○ Explicitly create the separation between different elements
■ E.g. you can’t “accidentally” importing an adapter class into the application
● At least, there will be one module for the hexagon, one module for each
adapter, and one module for starting up the whole project:
○ The hexagon depends on nothing
○ The adapters depend on the hexagon
○ The starting-up depends on all of them
27
Cons
Commitment
● The team needs good agreement & strong discipline to follow the pattern
○ It’s just too easy to use fancy framework utilities & dirty the core
○ For example, lots of things have to be wired up manually, while all it takes is a single Spring
Boot annotation to do it all
28
Parting words
● Actor → Adapter → (Port) Hexagon
● Clear dependency direction & pure logic business code
○ Testing is just a breeze
● And at what price?
○ Complexity:
■ Numerous modules
■ Need for internal mappers to map DTOs between components
○ Compromises:
■ Should we write those boilerplate getters & setters, or relax & use Lombok?
■ We want the fancy Spring Boot Configuration Properties but the Config classes are part
of the hexagon?
29
References
● https://jmgarridopaz.github.io/content/hexagonalarchitecture.html → an
excellent article on the pattern
● https://www.goodreads.com/book/show/34372564-microservice-patterns →
the book Microservice Patterns by Chris Richardson
30
Tung Vu Anh
Senior Software Engineer
https://www.linkedin.com/in/tung-vu-swe/
31

Mais conteúdo relacionado

Mais procurados

Domain Driven Design(DDD) Presentation
Domain Driven Design(DDD) PresentationDomain Driven Design(DDD) Presentation
Domain Driven Design(DDD) PresentationOğuzhan Soykan
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven DesignAndriy Buday
 
Kata: Hexagonal Architecture / Ports and Adapters
Kata: Hexagonal Architecture / Ports and AdaptersKata: Hexagonal Architecture / Ports and Adapters
Kata: Hexagonal Architecture / Ports and Adaptersholsky
 
Hexagonal architecture with Spring Boot
Hexagonal architecture with Spring BootHexagonal architecture with Spring Boot
Hexagonal architecture with Spring BootMikalai Alimenkou
 
Hexagonal architecture with Spring Boot
Hexagonal architecture with Spring BootHexagonal architecture with Spring Boot
Hexagonal architecture with Spring BootMikalai Alimenkou
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven DesignNader Albert
 
Clean architecture
Clean architectureClean architecture
Clean architecture.NET Crowd
 
Domain Driven Design Quickly
Domain Driven Design QuicklyDomain Driven Design Quickly
Domain Driven Design QuicklyMariam Hakobyan
 
Domain driven design
Domain driven designDomain driven design
Domain driven designits_skm
 
A Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation SlidesA Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation Slidesthinkddd
 
Domain Driven Design: Zero to Hero
Domain Driven Design: Zero to HeroDomain Driven Design: Zero to Hero
Domain Driven Design: Zero to HeroFabrício Rissetto
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven DesignRyan Riley
 
Clean architecture with ddd layering in php
Clean architecture with ddd layering in phpClean architecture with ddd layering in php
Clean architecture with ddd layering in phpLeonardo Proietti
 
Clean architecture with asp.net core
Clean architecture with asp.net coreClean architecture with asp.net core
Clean architecture with asp.net coreSam Nasr, MCSA, MVP
 
Domain Driven Design Introduction
Domain Driven Design IntroductionDomain Driven Design Introduction
Domain Driven Design Introductionwojtek_s
 
Hexagonal architecture for java applications
Hexagonal architecture for java applicationsHexagonal architecture for java applications
Hexagonal architecture for java applicationsFabricio Epaminondas
 

Mais procurados (20)

Domain Driven Design(DDD) Presentation
Domain Driven Design(DDD) PresentationDomain Driven Design(DDD) Presentation
Domain Driven Design(DDD) Presentation
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven Design
 
Kata: Hexagonal Architecture / Ports and Adapters
Kata: Hexagonal Architecture / Ports and AdaptersKata: Hexagonal Architecture / Ports and Adapters
Kata: Hexagonal Architecture / Ports and Adapters
 
Hexagonal architecture with Spring Boot
Hexagonal architecture with Spring BootHexagonal architecture with Spring Boot
Hexagonal architecture with Spring Boot
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
 
Domain Driven Design
Domain Driven Design Domain Driven Design
Domain Driven Design
 
Hexagonal architecture with Spring Boot
Hexagonal architecture with Spring BootHexagonal architecture with Spring Boot
Hexagonal architecture with Spring Boot
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Domain Driven Design Quickly
Domain Driven Design QuicklyDomain Driven Design Quickly
Domain Driven Design Quickly
 
Domain driven design
Domain driven designDomain driven design
Domain driven design
 
A Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation SlidesA Practical Guide to Domain Driven Design: Presentation Slides
A Practical Guide to Domain Driven Design: Presentation Slides
 
Domain Driven Design: Zero to Hero
Domain Driven Design: Zero to HeroDomain Driven Design: Zero to Hero
Domain Driven Design: Zero to Hero
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Clean architecture with ddd layering in php
Clean architecture with ddd layering in phpClean architecture with ddd layering in php
Clean architecture with ddd layering in php
 
Clean architecture with asp.net core
Clean architecture with asp.net coreClean architecture with asp.net core
Clean architecture with asp.net core
 
Domain Driven Design Introduction
Domain Driven Design IntroductionDomain Driven Design Introduction
Domain Driven Design Introduction
 
Hexagonal architecture for java applications
Hexagonal architecture for java applicationsHexagonal architecture for java applications
Hexagonal architecture for java applications
 
Domain driven design
Domain driven designDomain driven design
Domain driven design
 

Semelhante a Hexagonal Architecture.pdf

Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCDamienCarpy
 
On component interface
On component interfaceOn component interface
On component interfaceLaurence Chen
 
From class to architecture
From class to architectureFrom class to architecture
From class to architectureMarcin Hawraniak
 
Leveraging Open Standards to Build Highly Extensible Autonomous Systems
Leveraging Open Standards to Build Highly Extensible Autonomous SystemsLeveraging Open Standards to Build Highly Extensible Autonomous Systems
Leveraging Open Standards to Build Highly Extensible Autonomous SystemsICS
 
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018Gregory Taylor
 
Secure Developer Access at Decisiv
Secure Developer Access at DecisivSecure Developer Access at Decisiv
Secure Developer Access at DecisivTeleport
 
Evolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoEvolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoVMware Tanzu
 
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEOSFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEOLinaro
 
The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийSigma Software
 
The working architecture of node js applications open tech week javascript ...
The working architecture of node js applications   open tech week javascript ...The working architecture of node js applications   open tech week javascript ...
The working architecture of node js applications open tech week javascript ...Viktor Turskyi
 
George Grey Welcome Keynote - BUD17-100K1
George Grey Welcome Keynote - BUD17-100K1George Grey Welcome Keynote - BUD17-100K1
George Grey Welcome Keynote - BUD17-100K1Linaro
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Pôle Systematic Paris-Region
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendVlad Fedosov
 
Software Architecture - All you need to know
Software Architecture - All you need to knowSoftware Architecture - All you need to know
Software Architecture - All you need to knowVincent Composieux
 
Shaping the Future of Automatic Programming
Shaping the Future of Automatic ProgrammingShaping the Future of Automatic Programming
Shaping the Future of Automatic ProgrammingChristos Tsakostas
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraLINAGORA
 
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary SlidesRise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary SlidesDiUS
 

Semelhante a Hexagonal Architecture.pdf (20)

Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
Meetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaCMeetup 2020 - Back to the Basics part 101 : IaC
Meetup 2020 - Back to the Basics part 101 : IaC
 
On component interface
On component interfaceOn component interface
On component interface
 
From class to architecture
From class to architectureFrom class to architecture
From class to architecture
 
Leveraging Open Standards to Build Highly Extensible Autonomous Systems
Leveraging Open Standards to Build Highly Extensible Autonomous SystemsLeveraging Open Standards to Build Highly Extensible Autonomous Systems
Leveraging Open Standards to Build Highly Extensible Autonomous Systems
 
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
Kubernetes at Reddit: An Origin Story - KubeCon NA 2018
 
Secure Developer Access at Decisiv
Secure Developer Access at DecisivSecure Developer Access at Decisiv
Secure Developer Access at Decisiv
 
Evolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand RaoEvolving to Cloud-Native - Anand Rao
Evolving to Cloud-Native - Anand Rao
 
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEOSFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
SFO15-100K1: Welcome Keynote: George Grey, Linaro CEO
 
The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор Турский
 
The working architecture of node js applications open tech week javascript ...
The working architecture of node js applications   open tech week javascript ...The working architecture of node js applications   open tech week javascript ...
The working architecture of node js applications open tech week javascript ...
 
George Grey Welcome Keynote - BUD17-100K1
George Grey Welcome Keynote - BUD17-100K1George Grey Welcome Keynote - BUD17-100K1
George Grey Welcome Keynote - BUD17-100K1
 
A CQRS Journey
A CQRS JourneyA CQRS Journey
A CQRS Journey
 
Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...Designing and coding for cloud-native applications using Python, Harjinder Mi...
Designing and coding for cloud-native applications using Python, Harjinder Mi...
 
Engineering Frontends
Engineering FrontendsEngineering Frontends
Engineering Frontends
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontend
 
Software Architecture - All you need to know
Software Architecture - All you need to knowSoftware Architecture - All you need to know
Software Architecture - All you need to know
 
Shaping the Future of Automatic Programming
Shaping the Future of Automatic ProgrammingShaping the Future of Automatic Programming
Shaping the Future of Automatic Programming
 
Angular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - LinagoraAngular (v2 and up) - Morning to understand - Linagora
Angular (v2 and up) - Morning to understand - Linagora
 
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary SlidesRise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
Rise of the machines: Continuous Delivery at SEEK - YOW! Night Summary Slides
 

Último

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 

Último (20)

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

Hexagonal Architecture.pdf

  • 1. Hexagonal Architecture Ports & Adapters Pattern 1
  • 2. Foreword ● I call myself Tung, & you can do that too! ● I identify as an Engineer ● Please feel free to interrupt me anytime for any question :) 2
  • 3. Agenda ● A Long Introduction ○ What is an Architecture? ○ Backend Architectures ● Hexagonal Architecture ○ The Hexagon ○ The Actors ○ The Ports ○ The Adapters ● Summary, Pros, & Cons 3
  • 4. What comes to your mind when you hear “architecture”? ● That depends! ● The off-the-top-of-one’s-head answer varies depending on: ○ Your background ○ Your most recent / pending task ○ Your pain point at the moment ○ … 4
  • 5. The famous (& boring) 3-layer architecture 5
  • 6. There are many variants of this 3-layer thingy 6
  • 7. Let’s focus on the backend ● Monolith ● Micro services → Let’s jump into a service :) 7
  • 8. 8
  • 10. There’s 3-layer thingy for the backend alone 10
  • 12. There are too many! ● The point is that these architectures are not mutually exclusive ● The frontend - backend boundary could be very fluid ○ “Backend” can use template engine to render web pages ○ “Frontend” can run code on the server to render parts of the pages there ● How small a service has to be to be called “micro”? ○ Micro services architecture with only 1 big service ○ “Nano” services architecture with lazy services ■ & tons of mappers to convert all the DTOs to-and-fro 12
  • 13. Nothing is perfect! ● A service, reasonably sized ● 3-layer Controller - Service - Persistence ● & still a big ball of mud in there → These architectures are just ways to see, to organize things 13
  • 14. Even hexagonal architecture “Ports & Adapters pattern says nothing about the structure of the inside of the hexagon. You can have layers… you can have components by feature… you can have spaghetti code… you can have a Big Ball of Mud… you can apply DDD tactical patterns… you can have a single CRUD… it’s up to you.” 14
  • 15. 15
  • 16. The hexagon ● The core, pure business logic ● Technology agnostic → Could not be 100% so, as it still has to be written using a programming language and therefore, influenced by that language 16
  • 17. The actors 1. Drivers / Primary Actors → trigger interaction 2. Driven / Secondary Actors → are triggered by the application a. Repository: 2-way exchange of info b. Recipient: 1-way from application ● For knowing which kind is the actor in an application-actor interaction, ask yourself “who” triggers the conversation ○ If the answer is “the actor” then it is a driver ○ If the answer is “the application” then the actor is a driven actor 17
  • 18. 18
  • 19. The ports ● Ports are the application boundary, in the picture a port is an edge of the hexagon ● From the outside world, actors can only interact with the hexagon ports, they shouldn’t be able to access the inside of the hexagon ● An important thing to remark is that ports belong to the application ● Driver ports → API, features that the application provides to outside world ● Driven ports → SPI needed, dependencies of that application 19
  • 20. The adapters ● Given a port, there may be an adapter for each desired technology that we want to use ● Adapters are outside the application ● A driver adapter uses a driver port interface, converting a specific technology request into a technology agnostic request to a driver port ○ E.g. from a REST call to a java method call (Controller class calls Service class) ● A driven adapter implements a driven port interface, converting the technology agnostic methods of the port into specific technology methods ○ E.g. from a java method call to a push to a message queue (Service class calls Queue Client class) ○ or from a java method call to a SQL query (Service class calls Repository class) 20
  • 21. 21
  • 22. 22
  • 23. Summary of hexagonal architecture 1. The Hexagon → the application a. Driver Ports → API offered by the application b. Driven Ports → SPI required by the application 2. Actors → environment devices that interact with the application a. Drivers → application users (either humans or hardware/software devices) b. Driven Actors → provide services required by the application 3. Adapters → adapt specific technology to the application a. Driver Adapters → use the drivers ports b. Driven Adapters → implement the driven ports 23
  • 24. The Main Component This component will run at startup and it builds the whole system doing the following: ● It initializes and configures the environment (databases, servers, …) ● For each driven port: ○ It chooses a driven adapter implementing the port, and creates an instance of the adapter ○ It creates an instance of the application injecting the driven adapters instances into the application constructor ● For each driver port: ○ It chooses a driver adapter that uses the port, and creates an instance of the adapter, injecting the application instance into the adapter constructor ○ It runs the driver adapter instance 24
  • 25. Pros Flexibility & immunity to technology evolution ● Swapping between different technologies is easy ○ For a given port, you can have multiple adapters, each one using a specific technology ○ For choosing one of them, you just have to configure which adapter to use for that port ● The technology you want to upgrade is located at an adapter outside the application ○ Technology evolves more frequently than business logic does ○ You just have to change the adapter ○ The application itself remains immutable because it doesn’t depend on adapters 25
  • 26. Pros Delay technological decisions ● Focus just on business logic ○ Start with the application itself (ports are part of the application) ○ Choose a technology later, & create an adapter for it 26
  • 27. Cons Complexity ● A lot of modules and explicit dependencies defined between them ○ Modules in the sense of Maven / Gradle modules / subprojects ○ Explicitly create the separation between different elements ■ E.g. you can’t “accidentally” importing an adapter class into the application ● At least, there will be one module for the hexagon, one module for each adapter, and one module for starting up the whole project: ○ The hexagon depends on nothing ○ The adapters depend on the hexagon ○ The starting-up depends on all of them 27
  • 28. Cons Commitment ● The team needs good agreement & strong discipline to follow the pattern ○ It’s just too easy to use fancy framework utilities & dirty the core ○ For example, lots of things have to be wired up manually, while all it takes is a single Spring Boot annotation to do it all 28
  • 29. Parting words ● Actor → Adapter → (Port) Hexagon ● Clear dependency direction & pure logic business code ○ Testing is just a breeze ● And at what price? ○ Complexity: ■ Numerous modules ■ Need for internal mappers to map DTOs between components ○ Compromises: ■ Should we write those boilerplate getters & setters, or relax & use Lombok? ■ We want the fancy Spring Boot Configuration Properties but the Config classes are part of the hexagon? 29
  • 30. References ● https://jmgarridopaz.github.io/content/hexagonalarchitecture.html → an excellent article on the pattern ● https://www.goodreads.com/book/show/34372564-microservice-patterns → the book Microservice Patterns by Chris Richardson 30
  • 31. Tung Vu Anh Senior Software Engineer https://www.linkedin.com/in/tung-vu-swe/ 31