SlideShare a Scribd company logo
@skwp @reverbdotcom #wcr14
Yan Pritzker
CTO, Reverb.com

@skwp @reverbdotcom
Domain Driven Rails
https://speakerdeck.com/skwp
Domain Driven Rails
@skwp @reverbdotcom #wcr14
Are you happy with the 

SIZE and COMPLEXITY 

of your models?
@skwp @reverbdotcom #wcr14
Are you happy with the 

SCALABILITY

of your team?
@skwp @reverbdotcom #wcr14
Are you happy with the 

ADAPTABILITY

of your business?
@skwp @reverbdotcom #wcr14
What are you
building?
@skwp @reverbdotcom #wcr14
Rails is a
detail!"
Decouple
all things"
Is the code
better?"
Keep it
Railsy"
@skwp @reverbdotcom #wcr14
Somewhere
in between
Simple

CRUD"
Apps"
Complex

Enterprise

Logic"
@skwp @reverbdotcom #wcr14
I want to discover 

relevant compromises 

rather than defend ideals
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
Over
115

Model
Classes
@skwp @reverbdotcom #wcr14
Over
1000

Total
Classes
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
Not great, but we have only
three chubby models
@skwp @reverbdotcom #wcr14
Are monoliths
bad for business?
@skwp @reverbdotcom #wcr14
Quick Iterations
Low Operational 

Complexity
Refactoring
Monolith
ServiceService
Service
Service
Service
Early 2013 - Startup / Proof of Concept
Monolith
ServiceService
Service
Service
Service
2014 - Growth Phase
@skwp @reverbdotcom #wcr14
http://martinfowler.com/articles/distributed-objects-
microservices.html
“I'm wary of distribution and 

my default inclination is to prefer
a monolithic design”
“While small microservices are certainly simpler to reason
about, I worry that this pushes complexity into the
interconnections between services”
“Refactoring becomes much harder when you have to do it
across remote boundaries.”
@skwp @reverbdotcom #wcr14
Quick Iterations
Low Operational 

Complexity
May lead to a BBOM
@skwp @reverbdotcom #wcr14
Maintainable
Monoliths
Can Be
Achieved
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
Product

400 LOC

~150 LOC non-ActiveRecord
Churn: 49 changes this year"
Order

333 LOC

~200 LOC non-ActiveRecord
Churn: 36 changes this year
User

338 LOC

~200 LOC non-ActiveRecord
Churn: 29 changes this year
CHURN
Product
Order
User
@skwp @reverbdotcom #wcr14
Commonly used
classes are hard
to refactor
@skwp @reverbdotcom #wcr14
Stop 

modifying 

code!
(Open/Closed Principle)
@skwp @reverbdotcom #wcr14
Don’t put
different rates
of change
together"
Kent Beck - Smalltalk Best Practice Patterns
(see also: Single Responsibility Principle)
@skwp @reverbdotcom #wcr14
Don’t put
different rates
of change
together"
Kent Beck - Smalltalk Best Practice Patterns
Data Model
Business Logic
@skwp @reverbdotcom #wcr14
Separate 

what the system is "
from what the
system does"
James Copelien & Trygve Reenskaug
(Data, Context, Interaction)
@skwp @reverbdotcom #wcr14
Where does business
logic go?
@skwp @reverbdotcom #wcr14
Controller
2005
ActiveRecord
Mailers
Services
User
@skwp @reverbdotcom #wcr14
Console?"
Rake task?"
Background jobs?"
API layer?"
Testing?
What about…
@skwp @reverbdotcom #wcr14
Order
Fat models?
@skwp @reverbdotcom #wcr14
Order
Refund
@skwp @reverbdotcom #wcr14
Order
Refund
Ship
@skwp @reverbdotcom #wcr14
Order
Refund
Ship
Check Fraud Risk
@skwp @reverbdotcom #wcr14
“Active Record is a good choice
for domain logic that isn't too
complex, such as creates, reads,
updates, and deletes”
Martin Fowler
@skwp @reverbdotcom #wcr14
If Controller and Model
are all you have then 

one has to be skinny and
one has to be fat
@skwp @reverbdotcom #wcr14
Domain Layer
skinny framework, 

healthy business logic,"
no fat anywhere
http://joncairns.com/2013/04/fat-model-
skinny-controller-is-a-load-of-rubbish/
Model
Controller
View
The Rails Way
The
Way
Active
Record
Use Cases
Grape
API
ControllersCron
Redis
Rake
HTTP
Services
Workers
R
Entities
Roles
DB
Listeners
Events
@skwp @reverbdotcom #wcr14
app/reverb
for app specific
@skwp @reverbdotcom #wcr14
lib/reverb
for Open Source / Generic
@skwp @reverbdotcom #wcr14
Reverb::
Namespace to avoid
collisions with gems
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
This is a

marketplace!
http://blog.
8thlight.com/uncle-
bob/2011/09/30/
Screaming-
Architecture.html
@skwp @reverbdotcom #wcr14
http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-
architecture.html
@skwp @reverbdotcom #wcr14
see also Hexagonal, Ports & Adapters, DCI
@skwp @reverbdotcom #wcr14
Clean 

Enough 

Architecture"
@skwp @reverbdotcom #wcr14
Choosing the
right fight
@skwp @reverbdotcom #wcr14
http://blog.8thlight.com/mike-ebert/2013/03/23/the-
repository-pattern.html
@skwp @reverbdotcom #wcr14
http://blog.8thlight.com/mike-ebert/2013/03/23/the-
repository-pattern.html
(but you should still read this)
@skwp @reverbdotcom #wcr14
User.where(…)
User.active
User.find(1)
This is easy to replace with

something other than AR. 

Repository not required.
Don’t leak SQL outside of AR
@skwp @reverbdotcom #wcr14
Domain Logic in
ActiveRecord and
Controllers

leads to Churn
@skwp @reverbdotcom #wcr14
Use Cases
Reify complex business logic

into classes
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
This is not"
Rails
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
Explicitly Require Dependency
http://myronmars.to/n/dev-blog/2012/12/5-reasons-to-avoid-
bundler-require
Invoke It
@skwp @reverbdotcom #wcr14
Code Reuse!
Thin Shell
@skwp @reverbdotcom #wcr14
Stubby
Happy Path
@skwp @reverbdotcom #wcr14
Testing conditionals and side
effects has nothing to do with Rails
@skwp @reverbdotcom #wcr14
Naming
The hardest problem in computer science
@skwp @reverbdotcom #wcr14
OrderService
@skwp @reverbdotcom #wcr14
Order

Service
@skwp @reverbdotcom #wcr14
Order

Service
NounService Grows Unbounded
@skwp @reverbdotcom #wcr14
Use verbs to
narrow your scope
@skwp @reverbdotcom #wcr14
Order
@skwp @reverbdotcom #wcr14
Order
ShipOrder
@skwp @reverbdotcom #wcr14
Order
ShipOrder
Cancel

Order
@skwp @reverbdotcom #wcr14
Order
ShipOrder
Cancel

Order
Refund

Order
@skwp @reverbdotcom #wcr14
Order
ShipOrder
Cancel

Order
Refund

Order
Does not

grow over time
@skwp @reverbdotcom #wcr14
ShipOrder
Cancel

Order
Refund

Order
Don’t change"
once you write them
@skwp @reverbdotcom #wcr14
Ubiquitous Language
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
What is this?
@skwp @reverbdotcom #wcr14
Roles
Add methods to objects on demand
in the context of a Use Case
@skwp @reverbdotcom #wcr14
Decorator
@skwp @reverbdotcom #wcr14
We added these methods
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
Methods related to each"
other but loosely related"
to the parent concept"
and used only in a few"
Use Cases
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
Events
Add behavior with listeners
without modifying code
@skwp @reverbdotcom #wcr14
Order
ActiveRecord Callbacks
Send Email
@skwp @reverbdotcom #wcr14
Order
ActiveRecord Callbacks
Send Email
Call External Service
@skwp @reverbdotcom #wcr14
Order
ActiveRecord Callbacks
Send Email
Call External Service
Conditional

Callbacks
@skwp @reverbdotcom #wcr14
AR callbacks become
more complex as the
system supports more
use cases
@skwp @reverbdotcom #wcr14
Different use cases may
trigger different events
even when working with
the same model
Instead
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
@skwp @reverbdotcom #wcr14
Add another listener to add behavior

The core class doesn’t change
@skwp @reverbdotcom #wcr14
Global listeners for cross-cutting concerns

without littering code
Wisper::GlobalListeners.add(Reverb::Listeners::AnalyticsListener.new)"
@skwp @reverbdotcom #wcr14
Controller is a listener too
@skwp @reverbdotcom #wcr14
Policy Objects
Reify complex business rules
into objects
@skwp @reverbdotcom #wcr14
Likely to change
Unlikely to change
@skwp @reverbdotcom #wcr14
refactored for readability
@skwp @reverbdotcom #wcr14
Injectable, but has a default
Simple code, only need to test one path

for the imperative side effect"
(sending an email)
@skwp @reverbdotcom #wcr14
Distillation Time
@skwp @reverbdotcom #wcr14
Language of codebase 

reflects language of business
@skwp @reverbdotcom #wcr14
Separate behaviors (what the system
does) from models (what the system is)
@skwp @reverbdotcom #wcr14
Rates of Change
@skwp @reverbdotcom #wcr14
Is this all Unicorns
and Rainbows?
@skwp @reverbdotcom #wcr14
Sprawl?"
Onboarding?"
Naming?"
Documentation?"
@skwp @reverbdotcom #wcr14
http://blog.mattwynne.net/category/hexagonal-rails/
http://clean-ruby.com/
Resources
http://confreaks.com/videos/759-rubymidwest2011-keynote-
architecture-the-lost-years
http://blog.8thlight.com/uncle-bob/2011/09/30/Screaming-
Architecture.html
http://www.artima.com/articles/dci_vision.html
http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-
architecture.html
https://speakerdeck.com/skwp
@skwp @reverbdotcom #wcr14
We Are Hiring Always"
jobs.reverb.com
Ruby, ElasticSearch, DevOps,
Designers, Android, and more!
jobs@reverb.com

More Related Content

PDF
SOLID - Những nguyên lí sống còn
Nhật Nguyễn Khắc
 
PDF
Best practices for highly available and large scale SolrCloud
Anshum Gupta
 
PDF
JHipster
Yuen-Kuei Hsueh
 
PDF
PINTOS Operating system homework 2
Gichan Lee
 
PDF
Thiết kế hệ thống E-Commerce yêu cầu mở rộng
Nguyen Minh Quang
 
PPTX
NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...
Felix Gessert
 
PPTX
Zookeeper Architecture
Prasad Wali
 
PDF
MongoDB World 2019: Becoming an Ops Manager Backup Superhero!
MongoDB
 
SOLID - Những nguyên lí sống còn
Nhật Nguyễn Khắc
 
Best practices for highly available and large scale SolrCloud
Anshum Gupta
 
JHipster
Yuen-Kuei Hsueh
 
PINTOS Operating system homework 2
Gichan Lee
 
Thiết kế hệ thống E-Commerce yêu cầu mở rộng
Nguyen Minh Quang
 
NoSQL Data Stores in Research and Practice - ICDE 2016 Tutorial - Extended Ve...
Felix Gessert
 
Zookeeper Architecture
Prasad Wali
 
MongoDB World 2019: Becoming an Ops Manager Backup Superhero!
MongoDB
 

What's hot (20)

PDF
Angular Framework présentation PPT LIGHT
tayebbousfiha1
 
PDF
Angular 11
PapaDjadjigueye
 
PPTX
DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개
태준 문
 
DOCX
Hệ bạch huyết
thomxuan
 
PDF
Clean code - Trở thành một lập trình viên tốt hơn
Nhật Nguyễn Khắc
 
PPTX
Advanced Ops Manager Topics
MongoDB
 
PDF
Best Practices for Becoming an Exceptional Postgres DBA
EDB
 
PPT
Gp sl tietnieu
Phaolo Nguyen
 
PPTX
AngularJS - Présentation (french)
Yacine Rezgui
 
PDF
Building Event Driven (Micro)services with Apache Kafka
Guido Schmutz
 
PDF
Code Review - DevOn2013
호정 이
 
PPT
Soap
wissalll
 
PDF
Git : Deux écoles de pensées, merge vs rebase
jcbaudier
 
PDF
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz
 
PPTX
Mono Repo
Zacky Pickholz
 
PDF
Couchdb + Membase = Couchbase
iammutex
 
PPTX
Sinh lý tuần hoàn
Dam Van Tien
 
DOCX
Chuong 4 chuyen hoa glucid
angTrnHong
 
DOCX
Mau viet tai lieu srs
trinhtv_55
 
PDF
001 - Nova interface do GLPi 10.pdf
Verdanatech Soluções em TI
 
Angular Framework présentation PPT LIGHT
tayebbousfiha1
 
Angular 11
PapaDjadjigueye
 
DEVOPS 에 대한 전반적인 소개 및 자동화툴 소개
태준 문
 
Hệ bạch huyết
thomxuan
 
Clean code - Trở thành một lập trình viên tốt hơn
Nhật Nguyễn Khắc
 
Advanced Ops Manager Topics
MongoDB
 
Best Practices for Becoming an Exceptional Postgres DBA
EDB
 
Gp sl tietnieu
Phaolo Nguyen
 
AngularJS - Présentation (french)
Yacine Rezgui
 
Building Event Driven (Micro)services with Apache Kafka
Guido Schmutz
 
Code Review - DevOn2013
호정 이
 
Soap
wissalll
 
Git : Deux écoles de pensées, merge vs rebase
jcbaudier
 
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz
 
Mono Repo
Zacky Pickholz
 
Couchdb + Membase = Couchbase
iammutex
 
Sinh lý tuần hoàn
Dam Van Tien
 
Chuong 4 chuyen hoa glucid
angTrnHong
 
Mau viet tai lieu srs
trinhtv_55
 
001 - Nova interface do GLPi 10.pdf
Verdanatech Soluções em TI
 
Ad

Similar to Domain Driven Rails (20)

PDF
Domain Driven Design and Hexagonal Architecture with Rails
Declan Whelan
 
PDF
Code refactor strategy part #1
Tracy LOISEL
 
ODP
RailswayCon 2010 - Command Your Domain
Lourens Naudé
 
PDF
Rails antipattern-public
Chul Ju Hong
 
PDF
Rails antipatterns
Chul Ju Hong
 
PPTX
Microservices with .Net - NDC Sydney, 2016
Richard Banks
 
PDF
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Justin Gordon
 
PDF
Best Practice in Development
mirrec
 
PPT
Creating a World-Class RESTful Web Services API
David Keener
 
PDF
War between Tools and Design 2016
Mark Windholtz
 
PDF
Microservices for Java Architects (Chicago, April 21, 2015)
Derek Ashmore
 
PDF
Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)
Derek Ashmore
 
PPTX
Pragmatic Approach to Microservices and Cell-based Architecture
Andrew Blades
 
PDF
DDD Strategic Patterns and Microservices by Example
Erik Ashepa
 
PDF
Microservices for java architects schamburg-2015-05-19
Derek Ashmore
 
PDF
How to stop being Rails Developer
Ivan Nemytchenko
 
PDF
How I ended up contributing to Magento core
Alessandro Ronchi
 
PDF
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
Coupa Software
 
PDF
Gourmet Service Object
Brooklyn Zelenka
 
KEY
25 Real Life Tips In Ruby on Rails Development
Belighted
 
Domain Driven Design and Hexagonal Architecture with Rails
Declan Whelan
 
Code refactor strategy part #1
Tracy LOISEL
 
RailswayCon 2010 - Command Your Domain
Lourens Naudé
 
Rails antipattern-public
Chul Ju Hong
 
Rails antipatterns
Chul Ju Hong
 
Microservices with .Net - NDC Sydney, 2016
Richard Banks
 
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Justin Gordon
 
Best Practice in Development
mirrec
 
Creating a World-Class RESTful Web Services API
David Keener
 
War between Tools and Design 2016
Mark Windholtz
 
Microservices for Java Architects (Chicago, April 21, 2015)
Derek Ashmore
 
Microservices for Java Architects (Madison-Milwaukee, April 28-9, 2015)
Derek Ashmore
 
Pragmatic Approach to Microservices and Cell-based Architecture
Andrew Blades
 
DDD Strategic Patterns and Microservices by Example
Erik Ashepa
 
Microservices for java architects schamburg-2015-05-19
Derek Ashmore
 
How to stop being Rails Developer
Ivan Nemytchenko
 
How I ended up contributing to Magento core
Alessandro Ronchi
 
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
Coupa Software
 
Gourmet Service Object
Brooklyn Zelenka
 
25 Real Life Tips In Ruby on Rails Development
Belighted
 
Ad

More from Yan Pritzker (11)

PDF
Bitcoin not Blockchain
Yan Pritzker
 
PDF
CTO - Startup to Growth
Yan Pritzker
 
PDF
Outsource Everything
Yan Pritzker
 
PDF
Building Self Documenting REST APIs
Yan Pritzker
 
PDF
Pasted image-3237
Yan Pritzker
 
PDF
Pasted image-1795
Yan Pritzker
 
PDF
Pasted image-3237
Yan Pritzker
 
PDF
Pasted image-1795
Yan Pritzker
 
PDF
Pasted image-1706
Yan Pritzker
 
PDF
Building Services on and off Rails
Yan Pritzker
 
PDF
Virtualization and Cloud Computing with Elastic Server On Demand
Yan Pritzker
 
Bitcoin not Blockchain
Yan Pritzker
 
CTO - Startup to Growth
Yan Pritzker
 
Outsource Everything
Yan Pritzker
 
Building Self Documenting REST APIs
Yan Pritzker
 
Pasted image-3237
Yan Pritzker
 
Pasted image-1795
Yan Pritzker
 
Pasted image-3237
Yan Pritzker
 
Pasted image-1795
Yan Pritzker
 
Pasted image-1706
Yan Pritzker
 
Building Services on and off Rails
Yan Pritzker
 
Virtualization and Cloud Computing with Elastic Server On Demand
Yan Pritzker
 

Recently uploaded (20)

PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 

Domain Driven Rails