Slide 1

Slide 1 text

Poznan Elixir Meetup #6 mkaszubowski94

Slide 2

Slide 2 text

Time

Slide 3

Slide 3 text

Time Development speed

Slide 4

Slide 4 text

Time Software quality

Slide 5

Slide 5 text

Time Developer happiness

Slide 6

Slide 6 text

Time ?

Slide 7

Slide 7 text

Time The big rewrite

Slide 8

Slide 8 text

Time

Slide 9

Slide 9 text

Time

Slide 10

Slide 10 text

Software Architecture Poznan Elixir Meetup #6 mkaszubowski94

Slide 11

Slide 11 text

*Disclaimer

Slide 12

Slide 12 text

What is software architecture?

Slide 13

Slide 13 text

"the important stuff Ralph Johnson

Slide 14

Slide 14 text

"the important stuff (whatever that is)" Ralph Johnson

Slide 15

Slide 15 text

"The goal of software architecture is to minimize the human resources required to build and maintain the required system." Robert C. Martin, "Clean Architecture"

Slide 16

Slide 16 text

Architecture is about taking a step back, looking at a bigger picture and being aware of trade-offs when making decisions.

Slide 17

Slide 17 text

Quality attributes (non-functional requirements)

Slide 18

Slide 18 text

Quality attributes Maintainability Reusability Availability Performance Reliability Scalability Security Supportability Testability Deployability Simplicity Development speed Composability Fault tolerance ...

Slide 19

Slide 19 text

Security Usability

Slide 20

Slide 20 text

Common architecture patterns

Slide 21

Slide 21 text

Monolith architecture

Slide 22

Slide 22 text

Layered architecture

Slide 23

Slide 23 text

Layered architecture

Slide 24

Slide 24 text

Typical layered architecture

Slide 25

Slide 25 text

Hexagonal architecture

Slide 26

Slide 26 text

Clean architecture

Slide 27

Slide 27 text

Clean architecture

Slide 28

Slide 28 text

Microservices architecture

Slide 29

Slide 29 text

Event-driven architecture

Slide 30

Slide 30 text

Serverless architecture

Slide 31

Slide 31 text

Of course you can mix them

Slide 32

Slide 32 text

Example: ISO/OSI model

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

‣ Isolation ‣ Separation of concerns ‣ Hiding of implementation details ‣ Good abstractions ISO/OSI model

Slide 36

Slide 36 text

Example: applying that lessons

Slide 37

Slide 37 text

def release_expired_numbers() do expired_numbers = TwilioService.get_expired_numbers() Enum.map(expired_numbers, fn number  case Tesla.delete(@twilio_url, ) do {:ok, %Tesla.Env{status: 204}}  TwilioService.delete_phone_number(number) _error  :error end end) end

Slide 38

Slide 38 text

def release_expired_numbers() do expired_numbers = TwilioService.get_expired_numbers() Enum.map(expired_numbers, fn number  case Tesla.delete(@twilio_url, ) do {:ok, %Tesla.Env{status: 204}}  TwilioService.delete_phone_number(number) _error  :error end end) end

Slide 39

Slide 39 text

def release_expired_numbers() do expired_numbers = TwilioService.get_expired_numbers() Enum.map(expired_numbers, fn number  case Tesla.delete(@twilio_url, ) do {:ok, %Tesla.Env{status: 204}}  TwilioService.delete_phone_number(number) _error  :error end end) end

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

def release_expired_numbers() do expired_numbers = PhoneNumberService.get_expired_numbers() Enum.map(expired_numbers, fn number  case PhoneNumberApi.release_expired_number(number) do :ok  PhoneNumberService.delete_phone_number(number) _error  # ? end end) end

Slide 42

Slide 42 text

def release_expired_numbers() do expired_numbers = PhoneNumberService.get_expired_numbers() Enum.map(expired_numbers, fn number  case PhoneNumberApi.release_expired_number(number) do :ok  PhoneNumberService.delete_phone_number(number) _error  # ? end end) end

Slide 43

Slide 43 text

def release_expired_numbers() do expired_numbers = PhoneNumberService.get_expired_numbers() Enum.map(expired_numbers, fn number  case PhoneNumberApi.release_expired_number(number) do :ok  PhoneNumberService.delete_phone_number(number) _error  # ? end end) end

Slide 44

Slide 44 text

def release_expired_numbers() do expired_numbers = PhoneNumberService.get_expired_numbers() Enum.map(expired_numbers, fn number  case PhoneNumberApi.release_expired_number(number) do :ok  PhoneNumberService.delete_phone_number(number) _error  # ? end end) end

Slide 45

Slide 45 text

Example: selling tickets

Slide 46

Slide 46 text

defmodule MyApp.Event do schema "events" do field :name, :string field :start_date, :utc_datetime field :end_date, :utc_datetime field :description, :string field :tickets_count, :integer field :price, :integer field :location, Geo.Point belongs_to :host, MyApp.User has_many :tickets, MyApp.Ticket timestamps() end end

Slide 47

Slide 47 text

defmodule MyApp.Event do schema "events" do field :name, :string field :start_date, :utc_datetime field :end_date, :utc_datetime field :description, :string field :tickets_count, :integer field :price, :integer field :location, Geo.Point belongs_to :host, MyApp.User has_many :tickets, MyApp.Ticket timestamps() end end

Slide 48

Slide 48 text

Single Responsibility Principle

Slide 49

Slide 49 text

"The single responsibility principle (...) states that every module or class should have responsibility over a single part of the functionality provided by the software. "

Slide 50

Slide 50 text

"A class should have only one reason to change."

Slide 51

Slide 51 text

EventService • Finding events Responsibilities:

Slide 52

Slide 52 text

EventService • Finding events • Selling tickets Responsibilities:

Slide 53

Slide 53 text

EventService • Finding events • Selling tickets • Ratings Responsibilities:

Slide 54

Slide 54 text

EventService • Finding events • Selling tickets • Ratings • ... Responsibilities:

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

How much would it take to rewrite entire EventService from scratch?

Slide 57

Slide 57 text

EventService • Finding events • Selling tickets • Ratings • ... Responsibilities:

Slide 58

Slide 58 text

EventService • Finding events • Selling tickets • ... Responsibilities: RatingService

Slide 59

Slide 59 text

EventService • Finding events Responsibilities: RatingService SalesService

Slide 60

Slide 60 text

EventService SalesService RatingService

Slide 61

Slide 61 text

Nouns vs. Verbs

Slide 62

Slide 62 text

EventService SalesService RatingService "Displays events near you" "Allows users to buy tickets for events" "Allows users to give rating to the event"

Slide 63

Slide 63 text

"When you think about what a service knows, you always end up back at CRUD. http:/ /www.michaelnygard.com/blog/2018/01/services-by-lifecycle/

Slide 64

Slide 64 text

"When you think about what a service knows, you always end up back at CRUD. (...) Once you know what a service does, you can figure out what it needs to know. " http:/ /www.michaelnygard.com/blog/2018/01/services-by-lifecycle/

Slide 65

Slide 65 text

Database becomes just an implementation detail.

Slide 66

Slide 66 text

SalesService API Stable interface Hidden implementation detail

Slide 67

Slide 67 text

All great, right?

Slide 68

Slide 68 text

Architecture is about taking a step back, looking at a bigger picture and being aware of trade-offs when making decisions.

Slide 69

Slide 69 text

Architecture is about taking a step back, looking at a bigger picture and being aware of trade-offs when making decisions.

Slide 70

Slide 70 text

SalesService EventService • ticket_price • name • date • organizer { "name": "My Event", "organizer": {  }, "date": " ", "ticket_price": 200 }

Slide 71

Slide 71 text

SalesService EventService • ticket_price • name • date • organizer { "name": "My Event", "organizer": {  }, "date": " ", "ticket_price": 200 }

Slide 72

Slide 72 text

• Fetch both entities • Keep ticket price in both places • Keep everything only in events table • Use joins • ...

Slide 73

Slide 73 text

Maintainability Performance

Slide 74

Slide 74 text

Easy to understand Easy to write

Slide 75

Slide 75 text

Summary

Slide 76

Slide 76 text

This is hard

Slide 77

Slide 77 text

‣Trade-offs everywhere ‣There's no right answer ‣There's no way to predict the future ‣A lot of attributes to optimise for ‣It's easy to over-engineer ‣Not so much resources This is hard

Slide 78

Slide 78 text

‣Small pieces ‣Loose coupling ‣Strong, well-defined, stable interfaces ‣Focusing on behaviour, not data ‣Knowing common problems & patterns What helps

Slide 79

Slide 79 text

Resources ‣ The art of destroying software - Greg Young ‣ Architecture the Lost Years - Robert C. Martin ‣ Modular monoliths - Simon Brown ‣ https:/ /medium.com/@dan_manges/the-modular-monolith-rails- architecture-fb1023826fc4 ‣ https:/ /m.signalvnoise.com/the-majestic-monolith-29166d022228 ‣ https:/ /medium.com/appunite-edu-collection/whats-wrong-with-a- global-user-module-ed7ed013a519 ‣ http:/ /www.michaelnygard.com/blog/2018/01/services-by- lifecycle/

Slide 80

Slide 80 text

Resources

Slide 81

Slide 81 text

‣ "OO" patterns (SoC, SRP, SOLID, "Tell, don't ask") ‣ Early Object Oriented ideas ‣ Microservices ‣ Domain Driven Design ‣ CQS, CQRS, Event Sourcing Extra

Slide 82

Slide 82 text

Thanks! Poznan Elixir Meetup #6 mkaszubowski94