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

The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes BackBurke Libbey
 
Hadoop: A Hands-on Introduction
Hadoop: A Hands-on IntroductionHadoop: A Hands-on Introduction
Hadoop: A Hands-on IntroductionClaudio Martella
 
DataMapper on Infinispan
DataMapper on InfinispanDataMapper on Infinispan
DataMapper on InfinispanLance Ball
 
Developing with WordPress and Git
Developing with WordPress and GitDeveloping with WordPress and Git
Developing with WordPress and GitRob Miller
 
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 varnish90kts
 
NDH2k12 Cloud Computing Security
NDH2k12 Cloud Computing SecurityNDH2k12 Cloud Computing Security
NDH2k12 Cloud Computing SecurityMatthieu Bouthors
 
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_manyBlazing Cloud
 
Charla ruby nscodermad
Charla ruby nscodermadCharla ruby nscodermad
Charla ruby nscodermadnscoder_mad
 
Yet Another Replication Tool: RubyRep
Yet Another Replication Tool: RubyRepYet Another Replication Tool: RubyRep
Yet Another Replication Tool: RubyRepDenish Patel
 
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)Martijn Verburg
 
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-javascriptDaum DNA
 
Enterprise javascriptsession3
Enterprise javascriptsession3Enterprise javascriptsession3
Enterprise javascriptsession3Troy Miles
 
2012-11-30-scalable game servers
2012-11-30-scalable game servers2012-11-30-scalable game servers
2012-11-30-scalable game serversWooga
 

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

Nix: What even is it though?
Nix: What even is it though?Nix: What even is it though?
Nix: What even is it though?Burke Libbey
 
Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the SourceBurke Libbey
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes BackBurke Libbey
 
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 MagicBurke Libbey
 

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

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 

Recently uploaded (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 

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