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
PDF
ソーシャルゲーム案件におけるDB分割のPHP実装
PDF
Vacuum徹底解説
PPTX
WiredTigerを詳しく説明
PDF
How netflix manages petabyte scale apache cassandra in the cloud
PPTX
Apache Avro vs Protocol Buffers
PDF
FIDO認証によるパスワードレスログイン実装入門
PPTX
[135] 오픈소스 데이터베이스, 은행 서비스에 첫발을 내밀다.
Scaling Twitter
ソーシャルゲーム案件におけるDB分割のPHP実装
Vacuum徹底解説
WiredTigerを詳しく説明
How netflix manages petabyte scale apache cassandra in the cloud
Apache Avro vs Protocol Buffers
FIDO認証によるパスワードレスログイン実装入門
[135] 오픈소스 데이터베이스, 은행 서비스에 첫발을 내밀다.

What's hot (20)

PDF
[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유 (2부)
PPTX
DDD 그게 뭔데 (개념 찍먹편)
PDF
ProxySQL on Kubernetes
PDF
実践 NestJS
PDF
RDBNoSQLの基礎と組み合わせDB構成をちょっとよくする話
PDF
Evolution of MongoDB Replicaset and Its Best Practices
PDF
eBPFを用いたトレーシングについて
PPT
インフラエンジニアのためのcassandra入門
PDF
Dci vs aggregate_dddtw_2021-0.3-16-9
PDF
twMVC#44 讓我們用 k6 來進行壓測吧
PDF
Cloud Foundryは何故動くのか
PDF
REST: From GET to HATEOAS
PDF
Salvatore Sanfilippo – How Redis Cluster works, and why - NoSQL matters Barce...
PDF
5.6 以前の InnoDB Flushing
PDF
スマートフォン向けサービスにおけるサーバサイド設計入門
PDF
MongoDB WiredTiger Internals
PDF
All You Need is One - A ClickOnce Love Story - Secure360 2015
PDF
使いこなそうGUC
PDF
こんなに使える!今どきのAPIドキュメンテーションツール
PPTX
設計をする上で役にたった制約について
[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유 (2부)
DDD 그게 뭔데 (개념 찍먹편)
ProxySQL on Kubernetes
実践 NestJS
RDBNoSQLの基礎と組み合わせDB構成をちょっとよくする話
Evolution of MongoDB Replicaset and Its Best Practices
eBPFを用いたトレーシングについて
インフラエンジニアのためのcassandra入門
Dci vs aggregate_dddtw_2021-0.3-16-9
twMVC#44 讓我們用 k6 來進行壓測吧
Cloud Foundryは何故動くのか
REST: From GET to HATEOAS
Salvatore Sanfilippo – How Redis Cluster works, and why - NoSQL matters Barce...
5.6 以前の InnoDB Flushing
スマートフォン向けサービスにおけるサーバサイド設計入門
MongoDB WiredTiger Internals
All You Need is One - A ClickOnce Love Story - Secure360 2015
使いこなそうGUC
こんなに使える!今どきのAPIドキュメンテーションツール
設計をする上で役にたった制約について
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)

PPTX
Module 1 Introduction to Web Programming .pptx
PDF
Improvisation in detection of pomegranate leaf disease using transfer learni...
PDF
Advancing precision in air quality forecasting through machine learning integ...
PDF
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
PPTX
Microsoft User Copilot Training Slide Deck
PDF
giants, standing on the shoulders of - by Daniel Stenberg
PDF
Co-training pseudo-labeling for text classification with support vector machi...
PDF
Statistics on Ai - sourced from AIPRM.pdf
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
Early detection and classification of bone marrow changes in lumbar vertebrae...
PPTX
Build Your First AI Agent with UiPath.pptx
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
Flame analysis and combustion estimation using large language and vision assi...
PPTX
Internet of Everything -Basic concepts details
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
Module 1 Introduction to Web Programming .pptx
Improvisation in detection of pomegranate leaf disease using transfer learni...
Advancing precision in air quality forecasting through machine learning integ...
“A New Era of 3D Sensing: Transforming Industries and Creating Opportunities,...
Microsoft User Copilot Training Slide Deck
giants, standing on the shoulders of - by Daniel Stenberg
Co-training pseudo-labeling for text classification with support vector machi...
Statistics on Ai - sourced from AIPRM.pdf
Enhancing plagiarism detection using data pre-processing and machine learning...
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
Custom Battery Pack Design Considerations for Performance and Safety
Training Program for knowledge in solar cell and solar industry
Early detection and classification of bone marrow changes in lumbar vertebrae...
Build Your First AI Agent with UiPath.pptx
Convolutional neural network based encoder-decoder for efficient real-time ob...
The influence of sentiment analysis in enhancing early warning system model f...
Flame analysis and combustion estimation using large language and vision assi...
Internet of Everything -Basic concepts details
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf

How Shopify Scales Rails