SlideShare a Scribd company logo
shopify
How Shopify Scales Rails
John Duff
• The Shopify stack
• Knowing what to scale
• How we cache
• Scaling beyond caching
• Splitting things up
Overview
What is Shopify?
How Shopify Scales Rails
How Shopify Scales Rails
How Shopify Scales Rails
The Stack
• Ruby 1.9.3-p385
• Rails 3.2
• Percona MySQL 5.5
• Unicorn 4.5
• Memcached 1.4.14
• Redis 2.6
The Stack
The Stack
• 53 App Servers
• 1590 Unicorn Workers
• 5 Job Servers
• 370 Job Workers
Nginx
Unicorn
Rails 3.2
Ruby 1.9.3-p385
The Stack
Firewall
Load Balancer
App Servers
Redis
Job Servers
Database
Memcached Search
• 55,873 Lines of application code
• 15,968 Lines of CoffeeScript application code
• 81,892 Lines of test code
• 211 Controllers
• 468 Models
The Stack
Current Scale
9.9 M Orders
An order every 3.2 seconds
2,008 Sales per Minute
Cyber Monday
50,000 RPM
45 ms response time
13.3 billion requests
Looking Back, to Look Ahead
• First line of code written in 2004
• Shopify released June, 2006
• Same codebase
• Over 9 years of Rails upgrades, improvements and changes
Looking Back, to Look Ahead
Looking Back, to Look Ahead
• 6,702 Lines of application code (55,873)
• 4,386 Lines of test code (81,892)
• 38 Controllers (211)
• 77 Models (468)
Looking Back, to Look Ahead
• Ruby 1.8.2
• Rails 0.13.1
• MySQL 4.1
• Lighttpd
• Memcached
Know The System
One Request, One Process
RPM = W * 1/R
RPM = 1590 * 60 / 0.072
1,325,000 = 1172 * 60 / 0.072
↑ Workers
↓ Response Time
Know The System
• Avoid network calls during requests
• Speed up unavoidable network calls
• The Storefront and Checkout
• The Chive
Chive Flash Sale
Measure ALL THE THINGS
Measure ALL THE THINGS
• New Relic
• Splunk
• StatsD
• Cacti
• Conan
New Relic
Splunk
How Shopify Scales Rails
How Shopify Scales Rails
Caching
cacheable
cacheable
• https://github.com/Shopify/cacheable
• serve gzip’d content
• ETag and 304 Not Modified
• generational caching
• no explicit expiry
cacheable
class PostsController < ApplicationController
def show
response_cache do
@post = @shop.posts.find(params[:id])
respond_with(@post)
end
end
def cache_key_data
{
:action => action_name,
:format => request.format,
:params => params.slice(:id),
:shop_version => @shop.version
}
end
end
requests
Caching Dynamic 404s
Identity Cache
Identity Cache
• https://github.com/Shopify/identity_cache
• cache full model objects in memcached
• can include associated objects in cache
• must opt in to the cache
• explicit, but automatic expiry
Identity Cache
class Product < ActiveRecord::Base
include IdentityCache
has_many :images
cache_index [:shop_id, :id]
cache_has_many :images, :embed => true
end
@product = Product.fetch_by_shop_id_and_id(shop_id, id)
@images = @product.fetch_images
Identity Cache
Get Out of My Process
Delayed Job
• Jobs stored in the db
• Workers run in their own process
• Workers poll for jobs periodically
• https://github.com/collectiveidea/delayed_job
Resque
• Redis backed
• O(1) operation to pop jobs
• Faster (300 jobs/sec vs 120 jobs/sec)
• Extensible
• https://github.com/defunkt/resque
Resque
• Sending Email
• Processing Payments
• Geolocation
• Import / Export
• Indexing for Search
• 86 Other things...
Background Payment Processing
ms
Resque
class AddressGeolocationJob
max_retries 3
def self.perform(params)
object = params[:model].constantize.find(params[:id])
object.latitude, object.longitude = Geocoder.geocode(object)
object.save!
end
end
Resque.enqueue(AddressGeolocationJob, :id => 1, :model => 'Address')
Redis
• Inventory reservation system
• Sessions
• Theme uploads
• Throttling
• Carts
All Roads Lead To MySQL
MySQL Hardware
• 4 x 8 Core Processor
• SSD
• 256 GB Ram
• Full working set in memory
MySQL Query Optimization
• pt-query-digest
• Avoid queries that generate temp tables
• Adding the right indexes
• Forcing / Ignoring Indexes
MySQL Tuning
• disable innodb_stats_on_metadata
• increase table_open_cache
• replace glibc memory allocator with tcmalloc
• innodb_autoinc_lock_mode=‘interleaved’
after_commit
db transactions best friend
after_commit
• After transaction has been committed
• Webhooks
• Cache expiry
• Update associated objects
after_commit
class OrderObserver < ActiveRecord::Observer
observe :order
def after_save(order)
if order.changes.keys.include?(:financial_status)
order.flag_for_after_commit(:update_customer)
end
end
def after_commit(order)
if order.flagged_for_after_commit?(:update_customer)
Resque.enqueue(UpdateCustomerJob, :id => order.id)
end
end
end
Services
Services
• Split out standalone services as needed
• Independently scaled
• Segmented metrics
• Overall system is more complex
• Limit to what is necessary
Imagery
Adapt and Evolve as Needed
Using data and knowledge of the system to drive decisions
Summary
• Know your application and infrastructure.
• Keep slow IO or CPU tasks out of the main process.
• Measure your optimizations. You can make it worse.
Thanks.
@johnduff | john.duff@shopify.com

More Related Content

PDF
Scaling Twitter
PPTX
PDF
Serving ML easily with FastAPI - meme version
PPTX
Building Cloud-Native Applications with Helidon
PPTX
How I become Go GDE
PDF
Building a Streaming Microservice Architecture: with Apache Spark Structured ...
KEY
New relic
PPT
Java 8 Streams
Scaling Twitter
Serving ML easily with FastAPI - meme version
Building Cloud-Native Applications with Helidon
How I become Go GDE
Building a Streaming Microservice Architecture: with Apache Spark Structured ...
New relic
Java 8 Streams

What's hot (20)

PDF
Java8 features
PPTX
java 8 new features
PPTX
Java 8 streams
PDF
Redis + Apache Spark = Swiss Army Knife Meets Kitchen Sink
PPT
Multithread & shared_ptr
PDF
Vectorized Query Execution in Apache Spark at Facebook
PDF
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
PPTX
JS Event Loop
PDF
REST API and CRUD
PPTX
Minimal Viable Architecture - Silicon Slopes 2020
PDF
Towards Functional Programming through Hexagonal Architecture
PPTX
A Brief Introduction to React.js
PPTX
Maven Basics - Explained
PPTX
Build RESTful API Using Express JS
PDF
Introduction to RxJS
PDF
React JS and Redux
PDF
Rxjava 介紹與 Android 中的 RxJava
PDF
JJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty
PDF
TypeScript Best Practices
PDF
USENIX ATC 2017: Visualizing Performance with Flame Graphs
Java8 features
java 8 new features
Java 8 streams
Redis + Apache Spark = Swiss Army Knife Meets Kitchen Sink
Multithread & shared_ptr
Vectorized Query Execution in Apache Spark at Facebook
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
JS Event Loop
REST API and CRUD
Minimal Viable Architecture - Silicon Slopes 2020
Towards Functional Programming through Hexagonal Architecture
A Brief Introduction to React.js
Maven Basics - Explained
Build RESTful API Using Express JS
Introduction to RxJS
React JS and Redux
Rxjava 介紹與 Android 中的 RxJava
JJUG CCC 2018 Spring - I-7 (俺が)はじめての Netty
TypeScript Best Practices
USENIX ATC 2017: Visualizing Performance with Flame Graphs
Ad

Viewers also liked (20)

PDF
Ruby performance - The low hanging fruit
PPT
Twitter - Architecture and Scalability lessons
PDF
RubyConf Taiwan 2016 - Large scale Rails applications
PPTX
Shopify (An e-Commerce) PPT
PDF
20 Shopify landing pages that will inspire your next redesign
PPTX
Brisbane Shopify Meetup - 1st December 2016
PDF
Blazing Performance with Flame Graphs
PDF
Shopify Online Store Presentation – Setup Your Online Store in Minutes
PDF
Red Box Commerce Shopping Cart
PDF
Shopping Cart Optimization for eCommerce Web Sites
PDF
code.talks 2016 Hamburg - Plesk - AutoScaling WordPress with Docker & AWS - b...
PDF
Shopify Theme Development for Web Designers and Developers
PDF
Riding rails for 10 years
PPTX
EscConf - Deep Dive Frontend Optimization
PDF
UXFest - RUM Distillation 101
PDF
Edge Conf Rendering Performance Panel
PDF
JSDay 2013 - Practical Responsive Web Design
PDF
Evolution of The Twitter Stack
PDF
DIY Synthetic: Private WebPagetest Magic
PDF
PHP On Steroids
Ruby performance - The low hanging fruit
Twitter - Architecture and Scalability lessons
RubyConf Taiwan 2016 - Large scale Rails applications
Shopify (An e-Commerce) PPT
20 Shopify landing pages that will inspire your next redesign
Brisbane Shopify Meetup - 1st December 2016
Blazing Performance with Flame Graphs
Shopify Online Store Presentation – Setup Your Online Store in Minutes
Red Box Commerce Shopping Cart
Shopping Cart Optimization for eCommerce Web Sites
code.talks 2016 Hamburg - Plesk - AutoScaling WordPress with Docker & AWS - b...
Shopify Theme Development for Web Designers and Developers
Riding rails for 10 years
EscConf - Deep Dive Frontend Optimization
UXFest - RUM Distillation 101
Edge Conf Rendering Performance Panel
JSDay 2013 - Practical Responsive Web Design
Evolution of The Twitter Stack
DIY Synthetic: Private WebPagetest Magic
PHP On Steroids
Ad

Similar to How Shopify Scales Rails (20)

PPTX
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
PPTX
In-browser storage and me
PDF
Building & Testing Scalable Rails Applications
PDF
Building Advanced RESTFul services
PDF
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
PDF
DrupalSouth 2015 - Performance: Not an Afterthought
PDF
Top ten-list
PPTX
Magento performance feat. core Hacks
PPTX
OrigoDB - take the red pill
PPTX
Introducing Venice
PDF
Oracle Commerce Performance and ROI Maximization (Caching)
PPTX
Webinar - Macy’s: Why Your Database Decision Directly Impacts Customer Experi...
PPTX
Accelerating Rails with edge caching
PDF
MariaDB 初学者指南
PDF
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
PDF
Improve WordPress performance with caching and deferred execution of code
PPTX
DOTNET8.pptx
PPT
5 Common Mistakes You are Making on your Website
PDF
The Complete MariaDB Server tutorial
PDF
WebObjects Optimization
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
In-browser storage and me
Building & Testing Scalable Rails Applications
Building Advanced RESTFul services
MariaDB 10.1 what's new and what's coming in 10.2 - Tokyo MariaDB Meetup
DrupalSouth 2015 - Performance: Not an Afterthought
Top ten-list
Magento performance feat. core Hacks
OrigoDB - take the red pill
Introducing Venice
Oracle Commerce Performance and ROI Maximization (Caching)
Webinar - Macy’s: Why Your Database Decision Directly Impacts Customer Experi...
Accelerating Rails with edge caching
MariaDB 初学者指南
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
Improve WordPress performance with caching and deferred execution of code
DOTNET8.pptx
5 Common Mistakes You are Making on your Website
The Complete MariaDB Server tutorial
WebObjects Optimization

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Empathic Computing: Creating Shared Understanding
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Approach and Philosophy of On baking technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
A Presentation on Artificial Intelligence
PDF
Encapsulation theory and applications.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
Empathic Computing: Creating Shared Understanding
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Approach and Philosophy of On baking technology
20250228 LYD VKU AI Blended-Learning.pptx
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
A Presentation on Artificial Intelligence
Encapsulation theory and applications.pdf
A comparative analysis of optical character recognition models for extracting...
The Rise and Fall of 3GPP – Time for a Sabbatical?
The AUB Centre for AI in Media Proposal.docx
Chapter 3 Spatial Domain Image Processing.pdf
Spectroscopy.pptx food analysis technology
Spectral efficient network and resource selection model in 5G networks
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Programs and apps: productivity, graphics, security and other tools

How Shopify Scales Rails