Top Banner
Large scale Rails applications Florian Dutey
106

RubyConf Taiwan 2016 - Large scale Rails applications

Jan 19, 2017

Download

Engineering

Florian Dutey
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RubyConf Taiwan 2016 - Large scale Rails applications

Large scale Rails applications

Florian Dutey

Page 2: RubyConf Taiwan 2016 - Large scale Rails applications

www.strikingly.com

Page 3: RubyConf Taiwan 2016 - Large scale Rails applications

We hire!

Page 4: RubyConf Taiwan 2016 - Large scale Rails applications

2007

1.2.3

Page 5: RubyConf Taiwan 2016 - Large scale Rails applications

Fat Models Skinny Controllers

Page 6: RubyConf Taiwan 2016 - Large scale Rails applications

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

Page 7: RubyConf Taiwan 2016 - Large scale Rails applications
Page 8: RubyConf Taiwan 2016 - Large scale Rails applications
Page 9: RubyConf Taiwan 2016 - Large scale Rails applications

If the screwdriver can’t solve it, use the hammer.

Page 10: RubyConf Taiwan 2016 - Large scale Rails applications
Page 11: RubyConf Taiwan 2016 - Large scale Rails applications
Page 12: RubyConf Taiwan 2016 - Large scale Rails applications
Page 13: RubyConf Taiwan 2016 - Large scale Rails applications

Business complexity

Page 14: RubyConf Taiwan 2016 - Large scale Rails applications

Technical complexity

Page 15: RubyConf Taiwan 2016 - Large scale Rails applications

Solution = Complexity²

Page 16: RubyConf Taiwan 2016 - Large scale Rails applications

Rails doesn't scale!

Page 17: RubyConf Taiwan 2016 - Large scale Rails applications

NOT Rails!

Page 18: RubyConf Taiwan 2016 - Large scale Rails applications

ApplicationHttp

RequestResponse

Page 19: RubyConf Taiwan 2016 - Large scale Rails applications

Request Router Controller

ResponseView

Models

Page 20: RubyConf Taiwan 2016 - Large scale Rails applications

Request Router Controller

ResponseView

Models

Application

Page 21: RubyConf Taiwan 2016 - Large scale Rails applications

Principles• Modularity

• Single Responsibility Object

• Plain Old Ruby Objects

• Inversion Of Control

• Stateless Objects

• Domain Specific Language

Page 22: RubyConf Taiwan 2016 - Large scale Rails applications

Modularity

Page 23: RubyConf Taiwan 2016 - Large scale Rails applications

• Controller

• Test

• Rake tasks

• Jobs

• Daemons

• …

Page 24: RubyConf Taiwan 2016 - Large scale Rails applications

Single Responsibility Principle (SRP)

Page 25: RubyConf Taiwan 2016 - Large scale Rails applications

Not SRP

Page 26: RubyConf Taiwan 2016 - Large scale Rails applications

SRP

Page 27: RubyConf Taiwan 2016 - Large scale Rails applications

Useremail

password

first_name

last_name

address_1

address_2

zip_code

city

country

Page 28: RubyConf Taiwan 2016 - Large scale Rails applications

User

email

password

User::Profileuser_id

first_name

last_name

User::Addressuser_idaddress_1

address_2

zip_code

city

country

Page 29: RubyConf Taiwan 2016 - Large scale Rails applications

Break things downminimalist objects

Page 30: RubyConf Taiwan 2016 - Large scale Rails applications

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

Albert Einstein

Page 31: RubyConf Taiwan 2016 - Large scale Rails applications

Plain Old Ruby Objects (PORO)

Page 32: RubyConf Taiwan 2016 - Large scale Rails applications

PORO

Page 33: RubyConf Taiwan 2016 - Large scale Rails applications
Page 34: RubyConf Taiwan 2016 - Large scale Rails applications

Not PORO

Page 35: RubyConf Taiwan 2016 - Large scale Rails applications

Inversion of controls

Page 36: RubyConf Taiwan 2016 - Large scale Rails applications

Separate what and when

Page 37: RubyConf Taiwan 2016 - Large scale Rails applications
Page 38: RubyConf Taiwan 2016 - Large scale Rails applications
Page 39: RubyConf Taiwan 2016 - Large scale Rails applications

Why?

• Separate responsibilities

• Remove strong dependencies

• Easier to test

• Write highly abstract processes

Page 40: RubyConf Taiwan 2016 - Large scale Rails applications

Stateless objects

Page 41: RubyConf Taiwan 2016 - Large scale Rails applications

Stateless

Page 42: RubyConf Taiwan 2016 - Large scale Rails applications

Statefull

Page 43: RubyConf Taiwan 2016 - Large scale Rails applications

Why?

• No setup

• Predictable

Page 44: RubyConf Taiwan 2016 - Large scale Rails applications

Domain Specific Language

Page 45: RubyConf Taiwan 2016 - Large scale Rails applications

Router Controller

Domain

API language

Domain Specific Language

View API language

Page 46: RubyConf Taiwan 2016 - Large scale Rails applications

Break down

Page 47: RubyConf Taiwan 2016 - Large scale Rails applications

Request Router Controller

ResponseView

Application

?

??

??

Models

Page 48: RubyConf Taiwan 2016 - Large scale Rails applications

Services

Application

Forms Policies

Queries Adapters Models

Page 49: RubyConf Taiwan 2016 - Large scale Rails applications

Services Forms Policies

Queries Adapters Models

Application

Page 50: RubyConf Taiwan 2016 - Large scale Rails applications

Services (1)

Describe processes

Page 51: RubyConf Taiwan 2016 - Large scale Rails applications

Services (2)

Page 52: RubyConf Taiwan 2016 - Large scale Rails applications

Services (3)

Page 53: RubyConf Taiwan 2016 - Large scale Rails applications

Services (4)

Create != Signup

Page 54: RubyConf Taiwan 2016 - Large scale Rails applications

Services (5)

Page 55: RubyConf Taiwan 2016 - Large scale Rails applications

Services (6)

Page 56: RubyConf Taiwan 2016 - Large scale Rails applications

Services (7)

Page 57: RubyConf Taiwan 2016 - Large scale Rails applications

Services (8)

User::SignupService

Payment::SignupService

user/signup_service_spec

Payment::SignupService

payment/signup_service_spec

Page 58: RubyConf Taiwan 2016 - Large scale Rails applications

Services Forms Policies

Queries Adapters Models

Application

Page 59: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (1)

Convert inputs in Domain language

Page 60: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (2)

USERemail

password

Page 61: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (3)

Page 62: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (4)

Page 63: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (5)

Page 64: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (6)

User

email

password

User::Profileuser_id

first_name

last_name

Useremail

password

first_name

last_name

Page 65: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (7)

accepts_nested_attributes_for

Page 66: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (8)

Page 67: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (9)

Page 68: RubyConf Taiwan 2016 - Large scale Rails applications

Forms (10)

Page 69: RubyConf Taiwan 2016 - Large scale Rails applications

Services Forms Policies

Queries Adapters Models

Application

Page 70: RubyConf Taiwan 2016 - Large scale Rails applications

Policies (1)

Describe permissions

Page 71: RubyConf Taiwan 2016 - Large scale Rails applications

Policies (2)

Page 72: RubyConf Taiwan 2016 - Large scale Rails applications

Policies (3)

Page 73: RubyConf Taiwan 2016 - Large scale Rails applications

Policies (4)

Page 74: RubyConf Taiwan 2016 - Large scale Rails applications

Services Forms Policies

Queries Adapters Models

Application

Page 75: RubyConf Taiwan 2016 - Large scale Rails applications

Queries (1)

Describe how to access data

Page 76: RubyConf Taiwan 2016 - Large scale Rails applications

Queries (2)

Page 77: RubyConf Taiwan 2016 - Large scale Rails applications

Queries (3)

Page 78: RubyConf Taiwan 2016 - Large scale Rails applications

Services Forms Policies

Queries Adapters Models

Application

Page 79: RubyConf Taiwan 2016 - Large scale Rails applications

Adapters (1)

Translate DSL into another

Page 80: RubyConf Taiwan 2016 - Large scale Rails applications

Adapters (2)

Useremail

password

first_name

last_name

Recurly

Page 81: RubyConf Taiwan 2016 - Large scale Rails applications

Adapters (3)

Page 82: RubyConf Taiwan 2016 - Large scale Rails applications

Adapters (4)

It’s about languages

Page 83: RubyConf Taiwan 2016 - Large scale Rails applications

Adapters (5)

3rd party API language

Client

Http request

Ruby method

Page 84: RubyConf Taiwan 2016 - Large scale Rails applications

Adapters (6)

Client

API Language

Adapter

DSL

Application

Page 85: RubyConf Taiwan 2016 - Large scale Rails applications

Adapters (7)

Page 86: RubyConf Taiwan 2016 - Large scale Rails applications

Adapters (8)

PaymentManager

RecurlyAdapter StripeAdapter

Page 87: RubyConf Taiwan 2016 - Large scale Rails applications

Benefits

• Easy to test (mock client responses)

• New provider => new adapter

• Easy to migrate

• Fake adapters for integration servers

Page 88: RubyConf Taiwan 2016 - Large scale Rails applications

Services Forms Policies

Queries Adapters Models

Application

Page 89: RubyConf Taiwan 2016 - Large scale Rails applications

Models (1)

Data (& persistency)

Page 90: RubyConf Taiwan 2016 - Large scale Rails applications

Models (2)

If it doesn’t fit in models …

Page 91: RubyConf Taiwan 2016 - Large scale Rails applications

Models (3)

Then it doesn’t fit in models!

Page 92: RubyConf Taiwan 2016 - Large scale Rails applications

Models (4)

Page 93: RubyConf Taiwan 2016 - Large scale Rails applications

Conclusion

Page 94: RubyConf Taiwan 2016 - Large scale Rails applications

It’s not a perfect solution, just a guide

Page 95: RubyConf Taiwan 2016 - Large scale Rails applications

Pros

• Easier to browse and discover

• More flexible

• Easier to test

• Easier to debug

• Agnostic (from ActiveRecord)

Page 96: RubyConf Taiwan 2016 - Large scale Rails applications

after_commit

Page 97: RubyConf Taiwan 2016 - Large scale Rails applications

Cons

• Harder to design

• More time on code architecture

• Write more code, more tests

• Write integration tests (IOC)

Page 98: RubyConf Taiwan 2016 - Large scale Rails applications

Small apps

Page 99: RubyConf Taiwan 2016 - Large scale Rails applications

Soft transition

• Code convention

• Focus on data and API

• Express everything in DSL

• Write adapters early

• Drop assets pipeline

• Split FE / BE in different projects

Page 100: RubyConf Taiwan 2016 - Large scale Rails applications

Big apps

Page 101: RubyConf Taiwan 2016 - Large scale Rails applications

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

Page 102: RubyConf Taiwan 2016 - Large scale Rails applications

Rails is a fantastic prototyping tool!

Page 103: RubyConf Taiwan 2016 - Large scale Rails applications

Rails is NOT an application framework

Page 104: RubyConf Taiwan 2016 - Large scale Rails applications

Rails is a fantastic web framework!

Page 105: RubyConf Taiwan 2016 - Large scale Rails applications

Thank you!

Page 106: RubyConf Taiwan 2016 - Large scale Rails applications

Q & A