SlideShare a Scribd company logo
1 of 26
Download to read offline
Performance  RailsGirls Advanced Track #3
Derek Kraan  Software Engineer
I've prepared some exercises:  git clone git@github.com:derekkraan/railsgirls_performance.git
How to increase performance?  Do less.
N+1 queries
N+1 queries   • Author.all.each { |author| author.posts }  • ActiveRecord will run 1 query to get all authors  • and N queries for every author's post.
N+1 queries   • How to recognize?  • Look at the logs.
N+1 queries   • How to solve?  • Eager loading:  • Author.includes(:posts).all
exercise
Extra queries
Extra queries   • Author.first.blogposts.first.author  • This will require 3 queries  • Unless you tell ActiveRecord about  • the inverse relationship
exercise
Caching
Caching   • Many caching strategies  • page caching  • fragment caching
View fragment caching   • cache expensive view fragments  • <%= cache 'key' do %>  • ...  • <% end %>
View fragment caching   • What goes in the key?  • Hint: the key must change when  • the content within the fragment  • changes.
View fragment caching   • Rails makes a key from AR objects  • eg, <%= cache @blogpost do %>  • Rails uses @blogpost.cache_key to generate a key  • ActiveRecord::Base#cache_key will return  • "#{model_name}/#{id}-#{updated_at}"
View fragment caching   • Or AR relations  • <%= cache @blogposts do %>  • The key will be a combination of the keys  • from the individual blogposts in the collection.
Russian doll caching   • <% cache @blogposts do %>  • <% @blogposts.each do |blogpost| %>  • <% cache blogpost do %>  • <% end %>  • <% end %>
Caching   • Read more:  • http://guides.rubyonrails.org/caching_with_rails.html
exercise
Background jobs  Sidekiq / Resque / DelayedJob
Background jobs   • Do work out of band  • Complete the request faster  • The user sees the page faster
Background jobs   • class Worker  • def work  • # do work  • end  • end
Background jobs   • Worker.perform_async(*args)
exercise

More Related Content

What's hot

WordPress&映像配信セミナー+さぶみっと!オフ会- 第2回 さぶみっと! WEB制作セミナー Supported by NTTスマートコネクト
WordPress&映像配信セミナー+さぶみっと!オフ会- 第2回 さぶみっと! WEB制作セミナー Supported by NTTスマートコネクトWordPress&映像配信セミナー+さぶみっと!オフ会- 第2回 さぶみっと! WEB制作セミナー Supported by NTTスマートコネクト
WordPress&映像配信セミナー+さぶみっと!オフ会- 第2回 さぶみっと! WEB制作セミナー Supported by NTTスマートコネクトHiromichi Koga
 
Capybara + RSpec - ruby dsl-based web ui qa automation
Capybara + RSpec - ruby dsl-based web ui qa automationCapybara + RSpec - ruby dsl-based web ui qa automation
Capybara + RSpec - ruby dsl-based web ui qa automationCOMAQA.BY
 
Cooking environments with chef
Cooking environments with chefCooking environments with chef
Cooking environments with chefpythonandchips
 
Integration and Acceptance Testing
Integration and Acceptance TestingIntegration and Acceptance Testing
Integration and Acceptance TestingAlan Hecht
 
Web Cache Deception Attack
Web Cache Deception AttackWeb Cache Deception Attack
Web Cache Deception AttackOmer Gil
 
Writing automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsWriting automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsLeticia Rss
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Workhorse Computing
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript IntroductionJainul Musani
 
Story Driven Development With Cucumber
Story Driven Development With CucumberStory Driven Development With Cucumber
Story Driven Development With CucumberSean Cribbs
 
Selenium and Cucumber Selenium Conf 2011
Selenium and Cucumber Selenium Conf 2011Selenium and Cucumber Selenium Conf 2011
Selenium and Cucumber Selenium Conf 2011dimakovalenko
 
React basic by Yoav Amit, Wix
React basic by Yoav Amit, Wix React basic by Yoav Amit, Wix
React basic by Yoav Amit, Wix Chen Lerner
 
Open course(programming languages) 20150318
Open course(programming languages) 20150318Open course(programming languages) 20150318
Open course(programming languages) 20150318JangChulho
 
RSpec and Rails
RSpec and RailsRSpec and Rails
RSpec and RailsAlan Hecht
 
Mobile web-debug
Mobile web-debugMobile web-debug
Mobile web-debugFINN.no
 

What's hot (20)

WordPress&映像配信セミナー+さぶみっと!オフ会- 第2回 さぶみっと! WEB制作セミナー Supported by NTTスマートコネクト
WordPress&映像配信セミナー+さぶみっと!オフ会- 第2回 さぶみっと! WEB制作セミナー Supported by NTTスマートコネクトWordPress&映像配信セミナー+さぶみっと!オフ会- 第2回 さぶみっと! WEB制作セミナー Supported by NTTスマートコネクト
WordPress&映像配信セミナー+さぶみっと!オフ会- 第2回 さぶみっと! WEB制作セミナー Supported by NTTスマートコネクト
 
さぶみっと
さぶみっとさぶみっと
さぶみっと
 
Front-End Tooling
Front-End ToolingFront-End Tooling
Front-End Tooling
 
Capybara + RSpec - ruby dsl-based web ui qa automation
Capybara + RSpec - ruby dsl-based web ui qa automationCapybara + RSpec - ruby dsl-based web ui qa automation
Capybara + RSpec - ruby dsl-based web ui qa automation
 
Cooking environments with chef
Cooking environments with chefCooking environments with chef
Cooking environments with chef
 
Selenium sandwich-2
Selenium sandwich-2Selenium sandwich-2
Selenium sandwich-2
 
Integration and Acceptance Testing
Integration and Acceptance TestingIntegration and Acceptance Testing
Integration and Acceptance Testing
 
Web Cache Deception Attack
Web Cache Deception AttackWeb Cache Deception Attack
Web Cache Deception Attack
 
Writing automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjectsWriting automation tests with python selenium behave pageobjects
Writing automation tests with python selenium behave pageobjects
 
Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium Selenium Sandwich Part 1: Data driven Selenium
Selenium Sandwich Part 1: Data driven Selenium
 
Documentation vs test about cucumber but not only for vegetarians
Documentation vs test about cucumber but not only for vegetariansDocumentation vs test about cucumber but not only for vegetarians
Documentation vs test about cucumber but not only for vegetarians
 
ASP.NET MVC
ASP.NET MVCASP.NET MVC
ASP.NET MVC
 
JavaScript Introduction
JavaScript IntroductionJavaScript Introduction
JavaScript Introduction
 
Story Driven Development With Cucumber
Story Driven Development With CucumberStory Driven Development With Cucumber
Story Driven Development With Cucumber
 
Selenium and Cucumber Selenium Conf 2011
Selenium and Cucumber Selenium Conf 2011Selenium and Cucumber Selenium Conf 2011
Selenium and Cucumber Selenium Conf 2011
 
React basic by Yoav Amit, Wix
React basic by Yoav Amit, Wix React basic by Yoav Amit, Wix
React basic by Yoav Amit, Wix
 
Open course(programming languages) 20150318
Open course(programming languages) 20150318Open course(programming languages) 20150318
Open course(programming languages) 20150318
 
RSpec and Rails
RSpec and RailsRSpec and Rails
RSpec and Rails
 
Intro to Rails
Intro to RailsIntro to Rails
Intro to Rails
 
Mobile web-debug
Mobile web-debugMobile web-debug
Mobile web-debug
 

Viewers also liked

Lyonrb 12feb2014-sidekiq
Lyonrb 12feb2014-sidekiqLyonrb 12feb2014-sidekiq
Lyonrb 12feb2014-sidekiqKurt Sussman
 
#CNX14 - Using Ruby for Reliability, Consistency, and Speed
#CNX14 - Using Ruby for Reliability, Consistency, and Speed#CNX14 - Using Ruby for Reliability, Consistency, and Speed
#CNX14 - Using Ruby for Reliability, Consistency, and SpeedSalesforce Marketing Cloud
 
Joyent's Bryan Cantrill: Experiences Porting KVM to SmartOS at KVM Forum, Aug...
Joyent's Bryan Cantrill: Experiences Porting KVM to SmartOS at KVM Forum, Aug...Joyent's Bryan Cantrill: Experiences Porting KVM to SmartOS at KVM Forum, Aug...
Joyent's Bryan Cantrill: Experiences Porting KVM to SmartOS at KVM Forum, Aug...Peter Tripp
 
Manta: a new internet-facing object storage facility that features compute by...
Manta: a new internet-facing object storage facility that features compute by...Manta: a new internet-facing object storage facility that features compute by...
Manta: a new internet-facing object storage facility that features compute by...Hakka Labs
 
Intro to Joyent's Manta Object Storage Service
Intro to Joyent's Manta Object Storage ServiceIntro to Joyent's Manta Object Storage Service
Intro to Joyent's Manta Object Storage ServiceRod Boothby
 
Zappos at DRS: Why is Social Commerce So Hard? How Zappos is Navigating Social
Zappos at DRS: Why is Social Commerce So Hard? How Zappos is Navigating Social Zappos at DRS: Why is Social Commerce So Hard? How Zappos is Navigating Social
Zappos at DRS: Why is Social Commerce So Hard? How Zappos is Navigating Social Digiday
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsCommand Prompt., Inc
 
Scaling Wanelo.com 100x in Six Months
Scaling Wanelo.com 100x in Six MonthsScaling Wanelo.com 100x in Six Months
Scaling Wanelo.com 100x in Six MonthsKonstantin Gredeskoul
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQLKonstantin Gredeskoul
 
"Closing the Gap Between Developers & Customers," Wanelo >> Deena Varshavskay...
"Closing the Gap Between Developers & Customers," Wanelo >> Deena Varshavskay..."Closing the Gap Between Developers & Customers," Wanelo >> Deena Varshavskay...
"Closing the Gap Between Developers & Customers," Wanelo >> Deena Varshavskay...500 Startups
 
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesWebscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesJonathan Katz
 
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized EnvironmentsBest Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized EnvironmentsJignesh Shah
 
Lessons from silicon valley
Lessons from silicon valleyLessons from silicon valley
Lessons from silicon valleyAriel Poler
 
When rails hits the fan
When rails hits the fanWhen rails hits the fan
When rails hits the fanEric Saxby
 

Viewers also liked (20)

Lyonrb 12feb2014-sidekiq
Lyonrb 12feb2014-sidekiqLyonrb 12feb2014-sidekiq
Lyonrb 12feb2014-sidekiq
 
#CNX14 - Using Ruby for Reliability, Consistency, and Speed
#CNX14 - Using Ruby for Reliability, Consistency, and Speed#CNX14 - Using Ruby for Reliability, Consistency, and Speed
#CNX14 - Using Ruby for Reliability, Consistency, and Speed
 
Joyent's Bryan Cantrill: Experiences Porting KVM to SmartOS at KVM Forum, Aug...
Joyent's Bryan Cantrill: Experiences Porting KVM to SmartOS at KVM Forum, Aug...Joyent's Bryan Cantrill: Experiences Porting KVM to SmartOS at KVM Forum, Aug...
Joyent's Bryan Cantrill: Experiences Porting KVM to SmartOS at KVM Forum, Aug...
 
Like us! Follow us!
Like us! Follow us!Like us! Follow us!
Like us! Follow us!
 
Five steps perform_2013
Five steps perform_2013Five steps perform_2013
Five steps perform_2013
 
Dev Ops without the Ops
Dev Ops without the OpsDev Ops without the Ops
Dev Ops without the Ops
 
Manta: a new internet-facing object storage facility that features compute by...
Manta: a new internet-facing object storage facility that features compute by...Manta: a new internet-facing object storage facility that features compute by...
Manta: a new internet-facing object storage facility that features compute by...
 
Intro to Joyent's Manta Object Storage Service
Intro to Joyent's Manta Object Storage ServiceIntro to Joyent's Manta Object Storage Service
Intro to Joyent's Manta Object Storage Service
 
Yahoo! + Joyent Quickstart
Yahoo! + Joyent QuickstartYahoo! + Joyent Quickstart
Yahoo! + Joyent Quickstart
 
Zappos at DRS: Why is Social Commerce So Hard? How Zappos is Navigating Social
Zappos at DRS: Why is Social Commerce So Hard? How Zappos is Navigating Social Zappos at DRS: Why is Social Commerce So Hard? How Zappos is Navigating Social
Zappos at DRS: Why is Social Commerce So Hard? How Zappos is Navigating Social
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
 
Scaling postgres
Scaling postgresScaling postgres
Scaling postgres
 
Load balancing at tuenti
Load balancing at tuentiLoad balancing at tuenti
Load balancing at tuenti
 
Scaling Wanelo.com 100x in Six Months
Scaling Wanelo.com 100x in Six MonthsScaling Wanelo.com 100x in Six Months
Scaling Wanelo.com 100x in Six Months
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL
 
"Closing the Gap Between Developers & Customers," Wanelo >> Deena Varshavskay...
"Closing the Gap Between Developers & Customers," Wanelo >> Deena Varshavskay..."Closing the Gap Between Developers & Customers," Wanelo >> Deena Varshavskay...
"Closing the Gap Between Developers & Customers," Wanelo >> Deena Varshavskay...
 
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesWebscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
 
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized EnvironmentsBest Practices of HA and Replication of PostgreSQL in Virtualized Environments
Best Practices of HA and Replication of PostgreSQL in Virtualized Environments
 
Lessons from silicon valley
Lessons from silicon valleyLessons from silicon valley
Lessons from silicon valley
 
When rails hits the fan
When rails hits the fanWhen rails hits the fan
When rails hits the fan
 

Similar to Performance

Merb Slices
Merb SlicesMerb Slices
Merb Sliceshassox
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialYi-Ting Cheng
 
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...Jazkarta, Inc.
 
Performance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsPerformance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsSerge Smetana
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?brynary
 
When To Use Ruby On Rails
When To Use Ruby On RailsWhen To Use Ruby On Rails
When To Use Ruby On Railsdosire
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010singingfish
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone InteractivityEric Steele
 
fast prototyping with sinatra sequel w2tags
fast prototyping with sinatra sequel w2tagsfast prototyping with sinatra sequel w2tags
fast prototyping with sinatra sequel w2tagswidi harsojo
 
High Performance Kick Ass Web Apps (JavaScript edition)
High Performance Kick Ass Web Apps (JavaScript edition)High Performance Kick Ass Web Apps (JavaScript edition)
High Performance Kick Ass Web Apps (JavaScript edition)Stoyan Stefanov
 
5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniternicdev
 
Practical project automation
Practical project automationPractical project automation
Practical project automationReinout van Rees
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de RailsFabio Akita
 
Building Web Interface On Rails
Building Web Interface On RailsBuilding Web Interface On Rails
Building Web Interface On RailsWen-Tien Chang
 
Automated Performance Testing With J Meter And Maven
Automated  Performance  Testing With  J Meter And  MavenAutomated  Performance  Testing With  J Meter And  Maven
Automated Performance Testing With J Meter And MavenPerconaPerformance
 
Aspnet2 Overview
Aspnet2 OverviewAspnet2 Overview
Aspnet2 Overviewajitbergi
 
Turbogears Presentation
Turbogears PresentationTurbogears Presentation
Turbogears Presentationdidip
 

Similar to Performance (20)

Merb Slices
Merb SlicesMerb Slices
Merb Slices
 
OSDC 2009 Rails Turtorial
OSDC 2009 Rails TurtorialOSDC 2009 Rails Turtorial
OSDC 2009 Rails Turtorial
 
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
Deliverance: Plone theming without the learning curve from Plone Symposium Ea...
 
Performance Optimization of Rails Applications
Performance Optimization of Rails ApplicationsPerformance Optimization of Rails Applications
Performance Optimization of Rails Applications
 
What's new in Rails 2?
What's new in Rails 2?What's new in Rails 2?
What's new in Rails 2?
 
When To Use Ruby On Rails
When To Use Ruby On RailsWhen To Use Ruby On Rails
When To Use Ruby On Rails
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
 
Plone Interactivity
Plone InteractivityPlone Interactivity
Plone Interactivity
 
fast prototyping with sinatra sequel w2tags
fast prototyping with sinatra sequel w2tagsfast prototyping with sinatra sequel w2tags
fast prototyping with sinatra sequel w2tags
 
High Performance Kick Ass Web Apps (JavaScript edition)
High Performance Kick Ass Web Apps (JavaScript edition)High Performance Kick Ass Web Apps (JavaScript edition)
High Performance Kick Ass Web Apps (JavaScript edition)
 
5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter
 
Practical project automation
Practical project automationPractical project automation
Practical project automation
 
Impacta - Show Day de Rails
Impacta - Show Day de RailsImpacta - Show Day de Rails
Impacta - Show Day de Rails
 
Building Web Interface On Rails
Building Web Interface On RailsBuilding Web Interface On Rails
Building Web Interface On Rails
 
Automated Performance Testing With J Meter And Maven
Automated  Performance  Testing With  J Meter And  MavenAutomated  Performance  Testing With  J Meter And  Maven
Automated Performance Testing With J Meter And Maven
 
Aspnet2 Overview
Aspnet2 OverviewAspnet2 Overview
Aspnet2 Overview
 
Turbogears Presentation
Turbogears PresentationTurbogears Presentation
Turbogears Presentation
 
Qure Tech Presentation
Qure Tech PresentationQure Tech Presentation
Qure Tech Presentation
 
Sphinx on Rails
Sphinx on RailsSphinx on Rails
Sphinx on Rails
 
Please dont touch-3.6-jsday
Please dont touch-3.6-jsdayPlease dont touch-3.6-jsday
Please dont touch-3.6-jsday
 

Performance

  • 1. Performance RailsGirls Advanced Track #3
  • 2. Derek Kraan Software Engineer
  • 3. I've prepared some exercises: git clone git@github.com:derekkraan/railsgirls_performance.git
  • 4. How to increase performance? Do less.
  • 6. N+1 queries • Author.all.each { |author| author.posts } • ActiveRecord will run 1 query to get all authors • and N queries for every author's post.
  • 7. N+1 queries • How to recognize? • Look at the logs.
  • 8. N+1 queries • How to solve? • Eager loading: • Author.includes(:posts).all
  • 11. Extra queries • Author.first.blogposts.first.author • This will require 3 queries • Unless you tell ActiveRecord about • the inverse relationship
  • 14. Caching • Many caching strategies • page caching • fragment caching
  • 15. View fragment caching • cache expensive view fragments • <%= cache 'key' do %> • ... • <% end %>
  • 16. View fragment caching • What goes in the key? • Hint: the key must change when • the content within the fragment • changes.
  • 17. View fragment caching • Rails makes a key from AR objects • eg, <%= cache @blogpost do %> • Rails uses @blogpost.cache_key to generate a key • ActiveRecord::Base#cache_key will return • "#{model_name}/#{id}-#{updated_at}"
  • 18. View fragment caching • Or AR relations • <%= cache @blogposts do %> • The key will be a combination of the keys • from the individual blogposts in the collection.
  • 19. Russian doll caching • <% cache @blogposts do %> • <% @blogposts.each do |blogpost| %> • <% cache blogpost do %> • <% end %> • <% end %>
  • 20. Caching • Read more: • http://guides.rubyonrails.org/caching_with_rails.html
  • 22. Background jobs Sidekiq / Resque / DelayedJob
  • 23. Background jobs • Do work out of band • Complete the request faster • The user sees the page faster
  • 24. Background jobs • class Worker • def work • # do work • end • end
  • 25. Background jobs • Worker.perform_async(*args)