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

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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...
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

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