SlideShare a Scribd company logo
1 of 21
Download to read offline
Rails Performance
                                   Tuning
                              Steven Soroka and Burke Libbey




Wednesday, 23 November, 11
Overview

                   • Database performance problems
                   • (basic) Caching
                   • Application performance
                   • Helpful tools

Wednesday, 23 November, 11
Database performance



Wednesday, 23 November, 11
The two big offenders


                   • n+1 queries
                   • missing indexes


Wednesday, 23 November, 11
n+1 queries

                   • Rails makes it easy to fall into this trap.
                    • @users = User.where(...)
                    • @users.each { |u| u.posts.... }
                   • Use #includes to fix

Wednesday, 23 November, 11
Missing indexes

                   • Databases are not magic
                   • Rule of thumb: If you’re searching/sorting by
                             a column, index it.
                   • This includes (especially!) foreign keys
                   • Cardinality

Wednesday, 23 November, 11
Other database tweaks
                   • composite indexes
                   • primary key range instead of limit/offset
                   • explain & force index
                   • avoiding subselects
                   • carefully sizing columns
                   • using NOT NULL / :null => false
                   • INSERT faster than UPDATE
Wednesday, 23 November, 11
Also Handy:

                   • IdentityMap (Rails 3.1)
                   • config.active_record.identity_map = true
                   • IdentityMap is disabled by default and still in
                             development
                   • use with care

Wednesday, 23 November, 11
Caching

                   • memoization
                   • fragment caching
                   • action caching
                   • page caching
                   • others...

Wednesday, 23 November, 11
Memoization


                   • For expensive method calls with a finite set
                             of return values
                   • ActiveSupport::Memoizable is deprecated. :(


Wednesday, 23 November, 11
Fragment Caching
                   • http://guides.rubyonrails.org/
                             caching_with_rails.html
                   • config.action_controller.perform_caching =
                             true
                   • <% cache ‘slow_view’ do %> ... <% end %>
                   • expire_fragment('slow_view')

Wednesday, 23 November, 11
Application Performance

                   • Object allocations
                   • Tuning the GC
                   • Background jobs
                   • Avoid disk access at all costs
                   • Don’t be stupid

Wednesday, 23 November, 11
Don’t be Stupid




Wednesday, 23 November, 11
Object Allocations

                   • Ruby’s Garbage Collector is BAD.
                   • Don’t create objects you don’t need
                   • Reuse objects to save mem alloc?


Wednesday, 23 November, 11
Tuning the GC

                   • Use REE.
                   • Use these settings:
                    • https://gist.github.com/841168
                             RUBY_HEAP_MIN_SLOTS=500000
                             RUBY_HEAP_SLOTS_INCREMENT=250000
                             RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
                             RUBY_GC_MALLOC_LIMIT=50000000




Wednesday, 23 November, 11
Avoid Disk Access

                              L1 Cache          3
                                 L2            14
                                RAM           250
                                Disk      41,000,000
                              Network    240,000,000




Wednesday, 23 November, 11
Background Jobs


                   • Delayed::Job (db-driven, slowish)
                   • Resque (Redis-driven, fast)


Wednesday, 23 November, 11
Tools

                   • rack-bug
                   • rack-perftools
                   • NewRelic RPM
                   • ...and many more:                        lsof, tcpdump, strace, ltrace, rbtrace,
                             perftools, perftools.rb, gdb, gdb.rb, memprof, memprof.com, hotspots..




Wednesday, 23 November, 11
Rack::Bug
                   • Handy toolbar for devmode
                   • https://github.com/brynary/rack-bug




Wednesday, 23 November, 11
Rack::Perftools


                   • Demo!



Wednesday, 23 November, 11
Questions?



Wednesday, 23 November, 11

More Related Content

Similar to Rails Performance Tuning

DataMapper on Infinispan
DataMapper on InfinispanDataMapper on Infinispan
DataMapper on Infinispan
Lance Ball
 
Performance Schema in MySQL (Danil Zburivsky)
Performance Schema in MySQL (Danil Zburivsky)Performance Schema in MySQL (Danil Zburivsky)
Performance Schema in MySQL (Danil Zburivsky)
Ontico
 
Caching with varnish
Caching with varnishCaching with varnish
Caching with varnish
90kts
 
Devon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascriptDevon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascript
Daum DNA
 
Enterprise javascriptsession3
Enterprise javascriptsession3Enterprise javascriptsession3
Enterprise javascriptsession3
Troy Miles
 
2012-11-30-scalable game servers
2012-11-30-scalable game servers2012-11-30-scalable game servers
2012-11-30-scalable game servers
Wooga
 

Similar to Rails Performance Tuning (20)

The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes Back
 
Hadoop: A Hands-on Introduction
Hadoop: A Hands-on IntroductionHadoop: A Hands-on Introduction
Hadoop: A Hands-on Introduction
 
DataMapper on Infinispan
DataMapper on InfinispanDataMapper on Infinispan
DataMapper on Infinispan
 
Developing with WordPress and Git
Developing with WordPress and GitDeveloping with WordPress and Git
Developing with WordPress and Git
 
Performance Schema in MySQL (Danil Zburivsky)
Performance Schema in MySQL (Danil Zburivsky)Performance Schema in MySQL (Danil Zburivsky)
Performance Schema in MySQL (Danil Zburivsky)
 
Scala profiling
Scala profilingScala profiling
Scala profiling
 
Caching with varnish
Caching with varnishCaching with varnish
Caching with varnish
 
Infinispan for Dummies
Infinispan for DummiesInfinispan for Dummies
Infinispan for Dummies
 
Ruby-on-Infinispan
Ruby-on-InfinispanRuby-on-Infinispan
Ruby-on-Infinispan
 
NDH2k12 Cloud Computing Security
NDH2k12 Cloud Computing SecurityNDH2k12 Cloud Computing Security
NDH2k12 Cloud Computing Security
 
Rails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_manyRails ORM De-mystifying Active Record has_many
Rails ORM De-mystifying Active Record has_many
 
JRuby and You
JRuby and YouJRuby and You
JRuby and You
 
Charla ruby nscodermad
Charla ruby nscodermadCharla ruby nscodermad
Charla ruby nscodermad
 
JRubyConf 2009
JRubyConf 2009JRubyConf 2009
JRubyConf 2009
 
Yet Another Replication Tool: RubyRep
Yet Another Replication Tool: RubyRepYet Another Replication Tool: RubyRep
Yet Another Replication Tool: RubyRep
 
Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)Introduction to Java 7 (Devoxx Nov/2011)
Introduction to Java 7 (Devoxx Nov/2011)
 
Devon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascriptDevon 2011-f-4-improve your-javascript
Devon 2011-f-4-improve your-javascript
 
cassandra
cassandracassandra
cassandra
 
Enterprise javascriptsession3
Enterprise javascriptsession3Enterprise javascriptsession3
Enterprise javascriptsession3
 
2012-11-30-scalable game servers
2012-11-30-scalable game servers2012-11-30-scalable game servers
2012-11-30-scalable game servers
 

More from Burke Libbey (7)

Nix: What even is it though?
Nix: What even is it though?Nix: What even is it though?
Nix: What even is it though?
 
Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the Source
 
Ruby Internals
Ruby InternalsRuby Internals
Ruby Internals
 
Coffeescript
CoffeescriptCoffeescript
Coffeescript
 
Fuck Yeah Nouns
Fuck Yeah NounsFuck Yeah Nouns
Fuck Yeah Nouns
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes Back
 
Ruby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other MagicRuby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other Magic
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Rails Performance Tuning

  • 1. Rails Performance Tuning Steven Soroka and Burke Libbey Wednesday, 23 November, 11
  • 2. Overview • Database performance problems • (basic) Caching • Application performance • Helpful tools Wednesday, 23 November, 11
  • 4. The two big offenders • n+1 queries • missing indexes Wednesday, 23 November, 11
  • 5. n+1 queries • Rails makes it easy to fall into this trap. • @users = User.where(...) • @users.each { |u| u.posts.... } • Use #includes to fix Wednesday, 23 November, 11
  • 6. Missing indexes • Databases are not magic • Rule of thumb: If you’re searching/sorting by a column, index it. • This includes (especially!) foreign keys • Cardinality Wednesday, 23 November, 11
  • 7. Other database tweaks • composite indexes • primary key range instead of limit/offset • explain & force index • avoiding subselects • carefully sizing columns • using NOT NULL / :null => false • INSERT faster than UPDATE Wednesday, 23 November, 11
  • 8. Also Handy: • IdentityMap (Rails 3.1) • config.active_record.identity_map = true • IdentityMap is disabled by default and still in development • use with care Wednesday, 23 November, 11
  • 9. Caching • memoization • fragment caching • action caching • page caching • others... Wednesday, 23 November, 11
  • 10. Memoization • For expensive method calls with a finite set of return values • ActiveSupport::Memoizable is deprecated. :( Wednesday, 23 November, 11
  • 11. Fragment Caching • http://guides.rubyonrails.org/ caching_with_rails.html • config.action_controller.perform_caching = true • <% cache ‘slow_view’ do %> ... <% end %> • expire_fragment('slow_view') Wednesday, 23 November, 11
  • 12. Application Performance • Object allocations • Tuning the GC • Background jobs • Avoid disk access at all costs • Don’t be stupid Wednesday, 23 November, 11
  • 13. Don’t be Stupid Wednesday, 23 November, 11
  • 14. Object Allocations • Ruby’s Garbage Collector is BAD. • Don’t create objects you don’t need • Reuse objects to save mem alloc? Wednesday, 23 November, 11
  • 15. Tuning the GC • Use REE. • Use these settings: • https://gist.github.com/841168 RUBY_HEAP_MIN_SLOTS=500000 RUBY_HEAP_SLOTS_INCREMENT=250000 RUBY_HEAP_SLOTS_GROWTH_FACTOR=1 RUBY_GC_MALLOC_LIMIT=50000000 Wednesday, 23 November, 11
  • 16. Avoid Disk Access L1 Cache 3 L2 14 RAM 250 Disk 41,000,000 Network 240,000,000 Wednesday, 23 November, 11
  • 17. Background Jobs • Delayed::Job (db-driven, slowish) • Resque (Redis-driven, fast) Wednesday, 23 November, 11
  • 18. Tools • rack-bug • rack-perftools • NewRelic RPM • ...and many more: lsof, tcpdump, strace, ltrace, rbtrace, perftools, perftools.rb, gdb, gdb.rb, memprof, memprof.com, hotspots.. Wednesday, 23 November, 11
  • 19. Rack::Bug • Handy toolbar for devmode • https://github.com/brynary/rack-bug Wednesday, 23 November, 11
  • 20. Rack::Perftools • Demo! Wednesday, 23 November, 11