Slide 1

Slide 1 text

Large scale Rails applications Florian Dutey

Slide 2

Slide 2 text

www.strikingly.com

Slide 3

Slide 3 text

www.sxl.cn

Slide 4

Slide 4 text

2007 1.2.3

Slide 5

Slide 5 text

Fat Models Skinny Controllers

Slide 6

Slide 6 text

If it’s not a controller job, leave it to the model.

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

If it’s not a controller screwdriver job, leave it to the model hammer.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

User specs (1) • User can signup through site / API • When a user signup, we send an email to confirm it’s email address

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

User specs (2) • Admins can create users through admin tool • When admin create a user, we auto-generate the password and send it through email

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

User specs (3) • We have a partnership with another solution (A) and we need to share our users • We will consume A’s API to collect users and insert them in our DB • When we register a new user this, we don’t send any email (email is assumed confirmed already and we can grab their password from API).

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Rails doesn't scale!

Slide 22

Slide 22 text

Rails is NOT an application framework

Slide 23

Slide 23 text

Rails is a web framework!

Slide 24

Slide 24 text

Application Http Request Response

Slide 25

Slide 25 text

Request Router Controller Response View Models

Slide 26

Slide 26 text

Request Router Controller Response View Models Business logic

Slide 27

Slide 27 text

Request Router Controller Response View Business logic ? ? ? ? ? Models

Slide 28

Slide 28 text

Services Business logic Forms Policies Queries Adapters Models

Slide 29

Slide 29 text

Services Forms Policies Queries Adapters Models Business logic

Slide 30

Slide 30 text

Services (1) Describe processes

Slide 31

Slide 31 text

Services (2)

Slide 32

Slide 32 text

Services (3)

Slide 33

Slide 33 text

Services (4) Create != Signup

Slide 34

Slide 34 text

Services (5)

Slide 35

Slide 35 text

Services (6)

Slide 36

Slide 36 text

Services (7)

Slide 37

Slide 37 text

Services (8) User::SignupService Payment::SignupService user/signup_service_spec Payment::SignupService payment/signup_service_spec

Slide 38

Slide 38 text

Services Forms Policies Queries Adapters Models Business logic

Slide 39

Slide 39 text

Forms (1) Convert inputs in Domain language

Slide 40

Slide 40 text

Forms (2) USER email password

Slide 41

Slide 41 text

Forms (3)

Slide 42

Slide 42 text

Forms (4)

Slide 43

Slide 43 text

Forms (5)

Slide 44

Slide 44 text

Forms (6) User email password User::Profile user_id first_name last_name User email password first_name last_name

Slide 45

Slide 45 text

Forms (7) accepts_nested_attributes_for

Slide 46

Slide 46 text

Forms (8)

Slide 47

Slide 47 text

Forms (9)

Slide 48

Slide 48 text

Forms (10)

Slide 49

Slide 49 text

Services Forms Policies Queries Adapters Models Business logic

Slide 50

Slide 50 text

Policies (1) Describe permissions

Slide 51

Slide 51 text

Policies (2)

Slide 52

Slide 52 text

Policies (3)

Slide 53

Slide 53 text

Policies (4)

Slide 54

Slide 54 text

Services Forms Policies Queries Adapters Models Business logic

Slide 55

Slide 55 text

Queries (1) Describe how to access data

Slide 56

Slide 56 text

Queries (2)

Slide 57

Slide 57 text

Queries (3)

Slide 58

Slide 58 text

Services Forms Policies Queries Adapters Models Business logic

Slide 59

Slide 59 text

Adapters (1) Translate DSL into another

Slide 60

Slide 60 text

Adapters (2) User email password first_name last_name Recurly

Slide 61

Slide 61 text

Adapters (3)

Slide 62

Slide 62 text

Adapters (4) It’s about languages

Slide 63

Slide 63 text

Adapters (5) 3rd party API language Client Http request Ruby method

Slide 64

Slide 64 text

Adapters (6) Client API Language Adapter DSL Application

Slide 65

Slide 65 text

Adapters (7)

Slide 66

Slide 66 text

Adapters (8) PaymentManager RecurlyAdapter StripeAdapter RecurlyClient StripeClient

Slide 67

Slide 67 text

Benefits • Easy to test (mock client responses) • New provider => new adapter • Easy to migrate • Fake adapters for integration servers

Slide 68

Slide 68 text

Services Forms Policies Queries Adapters Models Business logic

Slide 69

Slide 69 text

Models (1) Data (& persistency)

Slide 70

Slide 70 text

Models (2) If it doesn’t fit in models …

Slide 71

Slide 71 text

Models (3) Then it doesn’t fit in models!

Slide 72

Slide 72 text

Models (4)

Slide 73

Slide 73 text

What if it doesn’t fit anywhere?

Slide 74

Slide 74 text

It’s not a perfect solution, just a guide

Slide 75

Slide 75 text

Principles • Single Responsibility Object • Plain Old Ruby Objects • Inversion Of Control • Stateless Objects • Domain Specific Modeling

Slide 76

Slide 76 text

Single Responsibility Principle (SRP)

Slide 77

Slide 77 text

Not SRP

Slide 78

Slide 78 text

SRP

Slide 79

Slide 79 text

User email password first_name last_name address_1 address_2 zip_code city country

Slide 80

Slide 80 text

User email password User::Profile user_id first_name last_name User::Address user_id address_1 address_2 zip_code city country

Slide 81

Slide 81 text

“Make everything as simple as possible, but not simpler.” Albert Einstein

Slide 82

Slide 82 text

Plain Old Ruby Objects (PORO)

Slide 83

Slide 83 text

PORO

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

Not PORO

Slide 86

Slide 86 text

Inversion of controls

Slide 87

Slide 87 text

Separate what and when

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

Why? • Separate responsibilities (SPR) • Remove strong dependencies • Easier to test and debug

Slide 91

Slide 91 text

Stateless objects

Slide 92

Slide 92 text

Stateless

Slide 93

Slide 93 text

Statefull

Slide 94

Slide 94 text

Statefull Stateless

Slide 95

Slide 95 text

Domain Specific Modeling

Slide 96

Slide 96 text

Router Controller Domain API language Domain Specific Modeling View API language

Slide 97

Slide 97 text

Conclusion

Slide 98

Slide 98 text

Rails is a prototyping framework!

Slide 99

Slide 99 text

Pros • Easier to browse and discover • More flexible • Easier to test • Easier to debug • Agnostic (from Rails)

Slide 100

Slide 100 text

Cons • Harder to design • More time spent on code architecture • Write more code, more tests • Write integration tests (IOC)

Slide 101

Slide 101 text

Small apps

Slide 102

Slide 102 text

Soft transition • Code convention • Focus on data and API • Express everything in Domain Language • Write adapters early • Prepare to drop Rails prototyping tools ASAP

Slide 103

Slide 103 text

Big apps

Slide 104

Slide 104 text

More tips • If it doesn’t fit anywhere, think twice • If it still doesn’t fit anywhere, you need a new layer • Check what Java / React community does

Slide 105

Slide 105 text

Thank you!

Slide 106

Slide 106 text

Q & A