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

Seven architectural patterns

Seven architectural patterns

Layered architecture
Hexagonal architecture
Onion architecture
Clean architecture
Microservice architecture
Serverless architecture
Reactive systems

Nakamura, Ryotaro

April 18, 2017
Tweet

More Decks by Nakamura, Ryotaro

Other Decks in Programming

Transcript

  1. Contents • Layered architecture • Hexagonal architecture • Onion architecture

    • Clean architecture • Microservice architecure • Serverless architecture • Reactive systems 2
  2. Layered architecture Decomposed into groups of subtasks in which each

    group is at a particular level of abstraction 3 M. Fowler, Patterns of Enterprise Application Architecture, Addison-Wesley Longman Publishing (2002) Layer Responsibilities Presentation Provision of services, display of information Domain Logic that is the real point of the system Data Source Communication with databases, messaging systems Three principal layers
  3. J2EE Server Layered architecture example 4 The J2EE architecture for

    Web applications B. Doyle and C. V. Lopes, Survey of Technologies for Web Application Development, CoRR, abs/0801.2618 (2008) Java Virtual Machine (JVM) Java 2, Standard Edition(J2SE) J2EE Services and Interfaces (Data Source) JDNI JDBC JTS JMS JCA JAAS EJB Container(Domain) EJB Components Java Classes Web Container (Presentation) Java Servlets Java Servlet Pages (JSP)
  4. Contents • Layered architecture • Hexagonal architecture • Onion architecture

    • Clean architecture • Microservice architecure • Serverless architecture • Reactive systems 5
  5. External agencies 6 No human being can drive applications Data

    Source Domain Presentation User Crawler Interaction with clients A. Cockburn, Hexagonal architecture, http://alistair.cockburn.us/Hexagonal+architecture
  6. Similarity between the two layers 7 The presentation and data

    source layers are about connection to the outside world Interaction with external entities Data Source Domain Presentation User Crawler Internal RDB Cloud SQL A. Cockburn, Hexagonal architecture, http://alistair.cockburn.us/Hexagonal+architecture
  7. Hexagonal Architecture pattern 8 Hexagonal architecture Visualizes any system as

    a core surrounded by interfaces to external systems A. Cockburn, Hexagonal architecture, http://alistair.cockburn.us/Hexagonal+architecture Application User-side API Database Message Service User Crawler SQS Google Cloud Pub/Sub Cloud SQL RDB
  8. Intent 9 Communicating over a port to external agencies Allow

    an application to be developed and tested in isolation from its external entities Adapters convert events into usable messages and pass them to the application Adapter1 +response +getMessage Adapter2 +receive +getMessage Port +getMessage Event2 +response Event1 +receive Application boundary Internal classes A. Cockburn, Hexagonal architecture, http://alistair.cockburn.us/Hexagonal+architecture
  9. Contents • Layered architecture • Hexagonal architecture • Onion architecture

    • Clean architecture • Microservice architecure • Serverless architecture • Reactive systems 10
  10. A drawback to the top-down layred architecture 11 Dependencies between

    layers J. Palermo, The Onion Architecture, http://jeffreypalermo.com/blog/the-onion-architecture-part-3/ Each layer is often coupled to the layers below it Data Source Business Logic UI Dependencies
  11. Onion architecture 13 Onion Architecture All dependencies are toward the

    center, and the outer layer is reserved for things that change often Domain Model Object Services Application Services Tests Infrastructure UI Dependencies J. Palermo, The Onion Architecture, http://jeffreypalermo.com/blog/the-onion-architecture-part-3/
  12. Isolation from the application core 14 Using Interfaces to achieve

    loose coupling Inner layers define interfaces which outer layers implement Domain Model Object Services Application Services Tests Infrastructure UI ConferenceRepository IConferenceRepository UserSession IUserSession SpeakerController J. Palermo, The Onion Architecture, http://jeffreypalermo.com/blog/the-onion-architecture-part-3/
  13. Difference from traditional layered architecture 15 Directly calling any inner

    layer Any outer layer can directly call any inner layer Domain Model Object Services Application Services Tests Infrastructure UI IUserSession J. Palermo, The Onion Architecture, http://jeffreypalermo.com/blog/the-onion-architecture-part-3/
  14. Contents • Layered architecture • Hexagonal architecture • Onion architecture

    • Clean architecture • Microservice architecure • Serverless architecture • Reactive systems 16
  15. Clean architecture 17 No name in an outer circle can

    be mentioned by an inner circle R. C. Martin, The Clean Architecture, https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html The Clean Architecture Entities Use Cases Frameworks & Drivers Interface Adapters Database Row Entity IUserSession We don’t cheat and pass entities or database rows across the boundaries
  16. What data crosses boundaries 18 Basic structs or simple Data

    Transfer objects are passed across the boundaries The Clean Architecture Entities Use Cases Frameworks & Drivers Interface Adapters Use Case Adapter passes isolated, simple data structures R. C. Martin, The Clean Architecture, https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
  17. Clean architecture 19 • Entities encapsulate enterprise wide business rules

    • Use Cases contain application specific business rules • Interface Adapters convert data from some external form to the form used by the use cases and entities • Frameworks and Drivers composed of frameworks and tools such as the Database, the Web Framework, etc R. C. Martin, The Clean Architecture, https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html
  18. Contents • Layered architecture • Hexagonal architecture • Onion architecture

    • Clean architecture • Microservice architecure • Serverless architecture • Reactive systems 20
  19. Disadvantages of monolithic applications 21 Changing one service requires changes

    to other services Any small changes require the entire monolith to be rebuilt and deployed J. Lewis and F. Martin, Microservices, https://martinfowler.com/articles/microservices.html single instance of an application server Service A Service B tightly coupled
  20. Disadvantages of monolithic applications 22 Horizontally scaling the monolith by

    running instances behind a load balancer Scaling requires scaling of the entire application rather than parts of it that require greater resource J. Lewis and F. Martin, Microservices, https://martinfowler.com/articles/microservices.html requires greater resource Service A Service B Service A Service B Service A Service B wasteful scaling
  21. Microservices 23 A microservice owns its data An approach to

    distributed systems which breaks down an application into services Service is a unit of software that is independently replaceable and upgradeable DB Search scaling Product Load Balancer J. Lewis and F. Martin, Microservices, https://martinfowler.com/articles/microservices.html Load Balancer DB Product DB DB Search Search Load Balancer Load Balancer service communicating with an out-of-process mechanism
  22. Contents • Layered architecture • Hexagonal architecture • Onion architecture

    • Clean architecture • Microservice architecure • Serverless architecture • Reactive systems 24
  23. Shared Nothing 25 Load-balancing between instances of a stateless service

    Shared nothing system scales to a large number of machies J. Bonér, Reactive Microservices Architecture, O’Reilly Media, Inc., (2016) Load Balancer DB Search Search Product DB It does not matter to which instance a particular request is sent
  24. Function as a Service(FaaS) 26 A FaaS function that runs

    within the event driven context Provides stateless containers that are ephemeral, event-triggered, and fully managed by a 3rd party Client Request handle function 3rd-party service DB Click stateless container M. Fowler, Serverless Architectures, https://martinfowler.com/articles/serverless.html
  25. Serverless architectures 27 A serverless architecture applications that significantly depend

    on 3rd party services or on FaaS M. Fowler, Serverless Architectures, https://martinfowler.com/articles/serverless.html Client API gateway purchase search DB DB authentication service functions request
  26. Scaling 28 Horizontal scaling is completely automatic, elastic, and managed

    by the provider M. Fowler, Serverless Architectures, https://martinfowler.com/articles/serverless.html Scaling managed by the provider Client API gateway purchase search DB DB authentication service request request request search search purchase purchase
  27. Contents • Layered architecture • Hexagonal architecture • Onion architecture

    • Clean architecture • Microservice architecure • Serverless architecture • Reactive systems 29
  28. Using services as components 30 Cascading failure Applications need to

    be designed so that they can tolerate the failure of services J. Bonér, Reactive Microservices Architecture, O’Reilly Media, Inc., (2016) Client Service A Service B Service C requires responses from B and C
  29. Asyncrhonous message-passing 31 Asyncrhonous message-passing An asynchronous boundary between services

    is required to decouple them in time and space Client message broker Service Service asynchronous boundary J. Bonér, Reactive Microservices Architecture, O’Reilly Media, Inc. (2016) A. Tanenbaum, Distributed Systems: Principles and Paradigms, Createspace Independent Publishing Platform (2016) Time: allowing concurrency Space: hiding where a resource is located
  30. Failure isolation 32 A response is returned later Avoiding cascading

    failure can be achieved using asynchronous message-passing J. Bonér, Reactive Microservices Architecture, O’Reilly Media, Inc., (2016) message broker Op1 Thread Time Op2 Service Service asynchronous boundary Service Op3 Op1 Service
  31. Reactive systems 33 Asynchronous message-passing forms the basis for resilience,

    elasticity, and responsiveness The Reactive Manifesto, http://www.reactivemanifesto.org/ The reactive manifesto Responsive Elastic Resilient Message Driven A B A forms the basis for B