Rails Performance Tuning

1,873 views
1,783 views

Published on

Rails Performance Tuning. Given at in August 2011.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,873
On SlideShare
0
From Embeds
0
Number of Embeds
228
Actions
Shares
0
Downloads
6
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Rails Performance Tuning

  1. 1. Rails Performance Tuning Steven Soroka and Burke LibbeyWednesday, 23 November, 11
  2. 2. Overview • Database performance problems • (basic) Caching • Application performance • Helpful toolsWednesday, 23 November, 11
  3. 3. Database performanceWednesday, 23 November, 11
  4. 4. The two big offenders • n+1 queries • missing indexesWednesday, 23 November, 11
  5. 5. n+1 queries • Rails makes it easy to fall into this trap. • @users = User.where(...) • @users.each { |u| u.posts.... } • Use #includes to fixWednesday, 23 November, 11
  6. 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 • CardinalityWednesday, 23 November, 11
  7. 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 UPDATEWednesday, 23 November, 11
  8. 8. Also Handy: • IdentityMap (Rails 3.1) • config.active_record.identity_map = true • IdentityMap is disabled by default and still in development • use with careWednesday, 23 November, 11
  9. 9. Caching • memoization • fragment caching • action caching • page caching • others...Wednesday, 23 November, 11
  10. 10. Memoization • For expensive method calls with a finite set of return values • ActiveSupport::Memoizable is deprecated. :(Wednesday, 23 November, 11
  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. 12. Application Performance • Object allocations • Tuning the GC • Background jobs • Avoid disk access at all costs • Don’t be stupidWednesday, 23 November, 11
  13. 13. Don’t be StupidWednesday, 23 November, 11
  14. 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. 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=50000000Wednesday, 23 November, 11
  16. 16. Avoid Disk Access L1 Cache 3 L2 14 RAM 250 Disk 41,000,000 Network 240,000,000Wednesday, 23 November, 11
  17. 17. Background Jobs • Delayed::Job (db-driven, slowish) • Resque (Redis-driven, fast)Wednesday, 23 November, 11
  18. 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. 19. Rack::Bug • Handy toolbar for devmode • https://github.com/brynary/rack-bugWednesday, 23 November, 11
  20. 20. Rack::Perftools • Demo!Wednesday, 23 November, 11
  21. 21. Questions?Wednesday, 23 November, 11

×