Slide 1

Slide 1 text

Building an Umbrella Project Wojtek Mach @wojtekmach @wojtekmach

Slide 2

Slide 2 text

Me • Ruby • GitHub API adapter for Ecto • OOP library for Elixir • Acme Bank

Slide 3

Slide 3 text

Me • Ruby • GitHub API adapter for Ecto • OOP library for Elixir • Acme Bank

Slide 4

Slide 4 text

Me • Ruby • GitHub API adapter for Ecto • OOP library for Elixir • Acme Bank

Slide 5

Slide 5 text

Me • Ruby • GitHub API adapter for Ecto • OOP library for Elixir • Acme Bank

Slide 6

Slide 6 text

What’s an Umbrella Project?

Slide 7

Slide 7 text

Inspiration

Slide 8

Slide 8 text

Inspiration Architecture: The Lost Years
 Robert C. Martin, Midwest RubyConf 2011
 • Typical Rails app • app/{models,controllers,view} • Top-level architecture should scream it’s intent

Slide 9

Slide 9 text

Inspiration Architecture: The Lost Years
 Robert C. Martin, Midwest RubyConf 2011
 • Typical Rails app • app/{models,controllers,view} • Top-level architecture should scream it’s intent

Slide 10

Slide 10 text

Inspiration Architecture: The Lost Years
 Robert C. Martin, Midwest RubyConf 2011
 • Typical Rails app • app/{models,controllers,views,…} • Top-level architecture should scream it’s intent

Slide 11

Slide 11 text

Inspiration Architecture: The Lost Years
 Robert C. Martin, Midwest RubyConf 2011
 • Typical Rails app • app/{models,controllers,views,…} • Top-level architecture should scream it’s intent

Slide 12

Slide 12 text

Inspiration Wrangling Large Rails Codebases
 Stephan Hagemann, Rocky Mountain Ruby 2012
 • Advice about writing large apps • Components

Slide 13

Slide 13 text

Inspiration Wrangling Large Rails Codebases
 Stephan Hagemann, Rocky Mountain Ruby 2012
 • Advice about writing a large app • Components

Slide 14

Slide 14 text

Inspiration Wrangling Large Rails Codebases
 Stephan Hagemann, Rocky Mountain Ruby 2012
 • Don’t write a large app • Components

Slide 15

Slide 15 text

Inspiration Wrangling Large Rails Codebases
 Stephan Hagemann, Rocky Mountain Ruby 2012
 • Don’t write a large app • Components: gems & engines

Slide 16

Slide 16 text

Inspiration The Art of Destroying Software
 Greg Young, Leetspeek 2014 
 • organize your project in a way that makes it easy to delete code.

Slide 17

Slide 17 text

Inspiration The Art of Destroying Software
 Greg Young, Leetspeek 2014 
 • organize your project in a way that makes it easy to delete code.

Slide 18

Slide 18 text

What’s an Umbrella Project?

Slide 19

Slide 19 text

What’s an Umbrella Project? • Mixfile • apps/ directory • OTP applications

Slide 20

Slide 20 text

OTP application „In Elixir (actually, in Erlang/OTP), an application is a component implementing some specific functionality, that can be started and stopped as a unit, and which can be re-used in other systems.” — Elixir API docs

Slide 21

Slide 21 text

OTP application • application dependencies • configuration • start/stop behaviour • regular applications & library applications

Slide 22

Slide 22 text

OTP application • application dependencies • configuration: config.exs, Application.put_env, mix.exs • start/stop behaviour • regular applications & library applications

Slide 23

Slide 23 text

OTP application • application dependencies • configuration • start/stop behaviour • regular applications & library applications

Slide 24

Slide 24 text

OTP application • application dependencies • configuration • start/stop behaviour • regular applications & library applications

Slide 25

Slide 25 text

OTP application • application dependencies • configuration • start/stop behaviour • regular applications & library applications • standard

Slide 26

Slide 26 text

OTP application „Application”

Slide 27

Slide 27 text

OTP application Server

Slide 28

Slide 28 text

OTP application Service

Slide 29

Slide 29 text

OTP application Microservice

Slide 30

Slide 30 text

Building Acme Bank

Slide 31

Slide 31 text

$ mix new bank_platform --umbrella

Slide 32

Slide 32 text

# mix.exs defmodule BankPlatform.Mixfile do use Mix.Project def project do [apps_path: "apps", deps: deps(), aliases: aliases()] end end Bank Platform

Slide 33

Slide 33 text

First OTP App

Slide 34

Slide 34 text

Bank

Slide 35

Slide 35 text

$ cd apps/
 $ mix new bank --sup Bank

Slide 36

Slide 36 text

# apps/bank/mix.exs defmodule Bank.Mixfile do use Mix.Project def project do [app: :bank, version: "0.1.0", build_path: "../../_build", config_path: "../../config/config.exs", deps_path: "../../deps", lockfile: "../../mix.lock", deps: deps()] end end Bank

Slide 37

Slide 37 text

First Feature

Slide 38

Slide 38 text

First Feature: Account Balance

Slide 39

Slide 39 text

defmodule Bank.Account do defstruct name: "", balance: 0 end Naive

Slide 40

Slide 40 text

defmodule Bank.Account do defstruct name: "", balance: 0 def deposit(account, amount) do %Account{account | balance: account.balance + amount} end end Naive

Slide 41

Slide 41 text

Accounting 101

Slide 42

Slide 42 text

Accounting 101 • Any movement of money happens between at least 2 accounts: one account is debited and the other account is credited • Example: Alice transfers $100 to Bob

Slide 43

Slide 43 text

Accounting 101 • Any movement of money happens between at least 2 accounts: one account is debited and the other account is credited • Example: Alice transfers $100 to Bob

Slide 44

Slide 44 text

Alice transfers $100 to Bob | Bob | :credit | 100.00 | Alice | :debit | 100.00 Accounting 101

Slide 45

Slide 45 text

Bob transfers $20 to Alice | Bob | :credit | 100.00 | Alice | :debit | 100.00 | Alice | :credit | 20.00 | Bob | :debit | 20.00 Accounting 101

Slide 46

Slide 46 text

Bob transfers $10 to Alice | Bob | :credit | 100.00 | Alice | :debit | 100.00 | Alice | :credit | 20.00 | Bob | :debit | 20.00 | Bob | :debit | 10.00 | Alice | :credit | 10.00 Accounting 101

Slide 47

Slide 47 text

Bob’s balance = $100 - $20 - $10 = $70 | Bob | :credit | 100.00 | Alice | :debit | 100.00 | Alice | :credit | 20.00 | Bob | :debit | 20.00 | Bob | :debit | 10.00 | Alice | :credit | 10.00 Accounting 101

Slide 48

Slide 48 text

Bob’s balance = $100 - $20 - $10 = $70 | Bob | :credit | 100.00 | Alice | :debit | 100.00 | Alice | :credit | 20.00 | Bob | :debit | 20.00 | Bob | :debit | 10.00 | Alice | :credit | 10.00 Accounting 101

Slide 49

Slide 49 text

Alice’s balance = -$100 + $20 + $10 = -$70 | Bob | :credit | 100.00 | Alice | :debit | 100.00 | Alice | :credit | 20.00 | Bob | :debit | 20.00 | Bob | :debit | 10.00 | Alice | :credit | 10.00 Accounting 101

Slide 50

Slide 50 text

Alice’s balance = -$100 + $20 + $10 = -$70 | Bob | :credit | 100.00 | Alice | :debit | 100.00 | Alice | :credit | 20.00 | Bob | :debit | 20.00 | Bob | :debit | 10.00 | Alice | :credit | 10.00 Double-entry accounting system Accounting 101

Slide 51

Slide 51 text

Accounting 101 Why mention this? • it’s a Bank • it’s functional • mathematical properties • complexity

Slide 52

Slide 52 text

Accounting 101 Why mention this? • it’s functional • mathematical properties • complexity

Slide 53

Slide 53 text

Accounting 101 Why mention this? • it’s functional • mathematical properties • complexity

Slide 54

Slide 54 text

Accounting 101 Why mention this? • it’s functional • associativity • complexity

Slide 55

Slide 55 text

Accounting 101 Why mention this? • it’s functional • associativity, commutativity • complexity

Slide 56

Slide 56 text

Accounting 101 Why mention this? • it’s functional • associativity, commutativity, idempotence • complexity

Slide 57

Slide 57 text

Accounting 101 Why mention this? • it’s functional • CRDT • complexity

Slide 58

Slide 58 text

Accounting 101 Why mention this? • it’s functional • eventual consistency [1] • complexity [1] http://highscalability.com/blog/2013/5/1/myth-eric-brewer-on-why-banks-are-base-not-acid-availability.html

Slide 59

Slide 59 text

Accounting 101 Why mention this? • it’s functional • eventual consistency [1] • complexity [1] http://highscalability.com/blog/2013/5/1/myth-eric-brewer-on-why-banks-are-base-not-acid-availability.html

Slide 60

Slide 60 text

Accounting 101 Why mention this? • it’s functional • eventual consistency [1] • complexity • complexity [1] http://highscalability.com/blog/2013/5/1/myth-eric-brewer-on-why-banks-are-base-not-acid-availability.html

Slide 61

Slide 61 text

Bank: Entities

Slide 62

Slide 62 text

Bank: Entities • Account • Entry • Customer

Slide 63

Slide 63 text

Bank: Entities • Account: web/models/account.ex • Entry: web/models/entry.ex • Customer: web/models/customer.ex

Slide 64

Slide 64 text

Bank: Entities • Account: lib/bank/account.ex • Entry: lib/bank/entry.ex • Customer: lib/bank/customer.ex

Slide 65

Slide 65 text

Bank: Entities • Account: lib/bank/account.ex • Entry: lib/bank/entry.ex • Customer: lib/bank/customer.ex Different rates of change

Slide 66

Slide 66 text

Bank: Entities • Account: lib/bank/ledger/account.ex • Entry: lib/bank/ledger/entry.ex • Customer: lib/bank/customer.ex Different rates of change

Slide 67

Slide 67 text

Bank: Persistence

Slide 68

Slide 68 text

Bank: Persistence • Ecto • Where to put Repo access?

Slide 69

Slide 69 text

Bank: Persistence • Ecto • Where to put Repo access?

Slide 70

Slide 70 text

Bank.Ledger # lib/bank/ledger.ex defmodule Bank.Ledger do def balance(account) do Repo.one("SELECT SUM(...)") end def write(entries) do # ... end end

Slide 71

Slide 71 text

Bank.Ledger # lib/bank/ledger.ex defmodule Bank.Ledger do def balance(account) do Repo.one("SELECT SUM(...)") end def write(entries) do # ... end end

Slide 72

Slide 72 text

Bank.Ledger # lib/bank/ledger.ex defmodule Bank.Ledger do def balance(account) do Repo.one("SELECT SUM(...)") end def write(entries) do # ... end end

Slide 73

Slide 73 text

Bank # lib/bank.ex defmodule Bank do def create_customer(params) do Bank.Customer.changeset(params) |> Repo.insert end end

Slide 74

Slide 74 text

Bank # lib/bank.ex defmodule Bank do def create_customer(params) do Bank.Customer.changeset(params) |> Repo.insert end end

Slide 75

Slide 75 text

Bank # lib/bank.ex defmodule Bank do def create_customer(params) do Bank.Customer.changeset(params) |> Repo.insert end end

Slide 76

Slide 76 text

Bank Bank.create_customer/1
 Bank.Ledger.balance/1
 Bank.Ledger.write/1 • Use cases • Use in controllers, tests, iex, seeds

Slide 77

Slide 77 text

Bank Bank.create_customer/1
 Bank.Ledger.balance/1
 Bank.Ledger.write/1 • Use cases • Use in controllers, tests, iex, seeds

Slide 78

Slide 78 text

Bank Bank.create_customer/1
 Bank.Ledger.balance/1
 Bank.Ledger.write/1 • Use cases • Use in controllers, tests, iex, seeds

Slide 79

Slide 79 text

Money

Slide 80

Slide 80 text

Money • Amount + currency • Operations • Persistence • Isolation • OSS • Productivity

Slide 81

Slide 81 text

Money • Amount + currency • Operations • Persistence • Isolation • OSS • Productivity

Slide 82

Slide 82 text

Money • Amount + currency • Operations • Persistence • Isolation • OSS • Productivity

Slide 83

Slide 83 text

Money • Amount + currency • Operations • Persistence • Isolation • OSS • Productivity

Slide 84

Slide 84 text

Money • Amount + currency • Operations • Persistence • Isolation • OSS • Productivity

Slide 85

Slide 85 text

Money • Amount + currency • Operations • Persistence • Isolation • OSS • Productivity

Slide 86

Slide 86 text

Money # apps/bank/mix.exs defp deps do [ {:money, in_umbrella: true} ] end

Slide 87

Slide 87 text

Money # apps/bank/mix.exs def application do [applications: [..., :money]] end

Slide 88

Slide 88 text

BankWeb

Slide 89

Slide 89 text

BankWeb $ cd apps/ $ mix phoenix.new bank_web # add bank as dependency

Slide 90

Slide 90 text

BankWeb $ cd apps/ $ mix phoenix.new bank_web # add bank as dependency

Slide 91

Slide 91 text

BankWeb $ cd apps/ $ mix phoenix.new bank_web # add bank as dependency

Slide 92

Slide 92 text

BankWeb • boring! • connect controllers to business logic

Slide 93

Slide 93 text

BankWeb • boring! • separation of concerns

Slide 94

Slide 94 text

Backoffice

Slide 95

Slide 95 text

Backoffice • ExAdmin. Works with umbrellas out of the box • ports setup • deployment • Phoenix is Not Your Application

Slide 96

Slide 96 text

Backoffice • ExAdmin. Works with umbrellas out of the box • setup different ports in development • deployment • Phoenix is Not Your Application

Slide 97

Slide 97 text

Backoffice • ExAdmin. Works with umbrellas out of the box • setup different ports in development • deployment • Phoenix is Not Your Application

Slide 98

Slide 98 text

Backoffice • ExAdmin. Works with umbrellas out of the box • setup different ports in development • deployment • Phoenix is Not Your Application

Slide 99

Slide 99 text

Backoffice • ExAdmin. Works with umbrellas out of the box • setup different ports in development • deployment • Phoenix is Not Your Application

Slide 100

Slide 100 text

Deployment

Slide 101

Slide 101 text

Deployment • Heroku • MasterProxy.

Slide 102

Slide 102 text

Deployment • Heroku • MasterProxy.

Slide 103

Slide 103 text

Deployment • Heroku • MasterProxy. Thanks @TheGazler! • distillery, exrm replacement, has 1st class support for umbrellas

Slide 104

Slide 104 text

Deployment • Heroku • MasterProxy. Thanks @TheGazler! • distillery, exrm replacement, has 1st class support for umbrellas

Slide 105

Slide 105 text

Auth

Slide 106

Slide 106 text

Auth • Upfront design? • Features: audit logs

Slide 107

Slide 107 text

Auth • Upfront design? • Features: strategies

Slide 108

Slide 108 text

Auth • Upfront design? • Features: strategies, 2FA

Slide 109

Slide 109 text

Auth • Upfront design? • Features: strategies, 2FA, tracking

Slide 110

Slide 110 text

Auth • Upfront design? • Features: strategies, 2FA, tracking

Slide 111

Slide 111 text

Auth • Upfront design? • Features: strategies, 2FA, tracking • Separate Repo, separate DB

Slide 112

Slide 112 text

Auth • Upfront design? • Features: strategies, 2FA, tracking • Separate Repo, separate DB • Thanks @chris_mccord!

Slide 113

Slide 113 text

Messenger

Slide 114

Slide 114 text

Bank Platform

Slide 115

Slide 115 text

BankPlatform • auth • bank • bank_web • backoffice • master_proxy • messenger • money

Slide 116

Slide 116 text

BankPlatform

Slide 117

Slide 117 text

BankPlatform

Slide 118

Slide 118 text

BankPlatform

Slide 119

Slide 119 text

Microservices

Slide 120

Slide 120 text

Microservices Benefits: • Strong Module Boundaries • Independent Deployments • Technology Diversity http://martinfowler.com/articles/microservice-trade-offs.html

Slide 121

Slide 121 text

Microservices Benefits: • Strong Module Boundaries • Independent Deployments • Technology Diversity http://martinfowler.com/articles/microservice-trade-offs.html Drawbacks: • Distribution • Eventual Consistency • Operational Complexity

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

Conclusion

Slide 125

Slide 125 text

Conclusion • keep an eye on the big picture

Slide 126

Slide 126 text

Conclusion • keep an eye on the big picture • don’t write a large app

Slide 127

Slide 127 text

Conclusion • keep an eye on the big picture • don’t write a large app • isolate by functionality and rate of change

Slide 128

Slide 128 text

Conclusion • keep an eye on the big picture • don’t write a large app • isolate by functionality and rate of change • delete code liberally

Slide 129

Slide 129 text

Thanks! wojtekmach/acme_bank