SlideShare une entreprise Scribd logo
1  sur  15
Rails performance:
                      Ruby GC tweaking


     #ParisRB                        Vlad ZLOTEANU
     August 2, 2001                           @vladzloteanu
                                 Software engineer @ Dimelo

Copyright Dimelo SA                                  www.dimelo.com
What we develop


               Frontend products
               -Collaborative platforms, ‘forum-like’, white-labeled, for
               big accounts
               (a kind of GetSatisfaction / UserVoice, but.. Better ;) )



               Backend product
               -SocialAPIs client
               -(a kind of TweetDeck, but for multiple channels, and
               designed for teams/multiple users per account)



Copyright Dimelo SA                                               www.dimelo.com
Stats on Dynamic requests


                      - on HTML/XHTML dynamic requests (we don’t count CSS, JS, images, etc)



                                                           20 req/s
                                                                            Web
                                                 Peaks of 40 req/s



                                                         10 req/s            APIs
                                                 Peaks of 25 req/s




Copyright Dimelo SA                                                             www.dimelo.com
Problem




                Medium response time ~ 400 ms

                                                WTF??




Copyright Dimelo SA                                     www.dimelo.com
Ruby perf toolbox


    Benchmark
                       Server logs Apache Benchmark (ab, ab2)

    Profile            Rack::Bug RailsBench RubyProf RBTrace
                       Memprof Scrap BleakHouse .. Valgrind
                       ltrace

    Std surveillance
                       Cacti Nagios

   Surveilance & profiling
                   NewRelic RPM UnionStation


Copyright Dimelo SA                                        www.dimelo.com
Finding the problem(s)


                 - DB queries (unoptimized queries, n+1)

                 - External services (EG external RSS feeds)

                 - Lots of partials, low or inexistent cache

                 - Slow session storage

                 - Memory issues
                   - Unoptimized GC
                   - Memory leaks



Copyright Dimelo SA                                            www.dimelo.com
Memory / GC issues
                      - How do I know it’s a memory/GC issue?
                         - Server logs
                         - Profilers (memprof)
                         - Monitorization services (newrelic)




Copyright Dimelo SA                                             www.dimelo.com
Ruby Garbage Collector


                 - Conservative


                 - Stop-the-world


                 - Mark-and-sweep process




Copyright Dimelo SA                         www.dimelo.com
Ruby default GC settings

                 RUBY_HEAP_MIN_SLOTS=10000
                 -Initial number of heap slots. It also represents the
                 minimum number of slots, at all times
                 -a new rails app boots up with almost 500k objects
                 on the heap(mostly nodes)

                 RUBY_HEAP_SLOTS_INCREMENT=10000
                 -The number of new slots to allocate when all initial
                 slots are used

                 RUBY_HEAP_SLOTS_GROWTH_FACTOR=1.8
                 -Next time Ruby needs new heap slots it will use a
                 multiplicator
                 -New stab is almost double sized
Copyright Dimelo SA                                                www.dimelo.com
Ruby default GC settings (2)



                 RUBY_GC_MALLOC_LIMIT=8000000
                 -force GC after 8 MB malloc/realloc
                 -High traffic Rails servers can easily allocate more
                 than 8 MB per request

                 RUBY_HEAP_FREE_MIN=4096
                 -The number of free slots that should be present
                 after GC finishes running. If there are fewer slots
                 than those defined it will allocate new ones




Copyright Dimelo SA                                                 www.dimelo.com
Find your numbers



                 - Start with enough memory to hold Rails (Ruby’s
                   default is practically nothing)

                 - Increase it linearly if you need more (Ruby’s
                   default is exponential increase)

                 - Only garbage-collect every XX million malloc
                   calls (Ruby’s default is way too small)

                 - Benchmark, benchmark, benchmark..



Copyright Dimelo SA                                                www.dimelo.com
GitHub numbers


      • RUBY_HEAP_MIN_SLOTS=800000 (10000)
      • RUBY_HEAP_FREE_MIN=100000 (4096)
      • RUBY_HEAP_SLOTS_INCREMENT=300000(100
        00)
      • RUBY_HEAP_SLOTS_GROWTH_FACTOR=1(1.8)
      • RUBY_GC_MALLOC_LIMIT=79000000(8000000
        )


Copyright Dimelo SA                    www.dimelo.com
Demo (using ApacheBench)

           - Frontend app, 15 requests, concurrency 1

           Before optimisations:

           Memory usage: 130 MB

           Execution time(ms):     228 344 322.0 316 4797


           After optimisations:

           Memory usage: 160 MB
           Execution time(ms):     120 181 190.0 150 2256




Copyright Dimelo SA                                         www.dimelo.com
Conclusions



                 - use REE on prod (or Ruby 1.9)

                 - measure, measure, measure (don’t forget to
                   GC.enable_stats)

                 - optimise what matters

                 - And yes, 5 lines of code can make a difference ;)




Copyright Dimelo SA                                              www.dimelo.com
Thank you!


      • Questions?




Copyright Dimelo SA           www.dimelo.com

Contenu connexe

Similaire à Rails performance: Ruby GC tweaking

02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
victor dong
 
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
jackdong
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summary
daniel.mattes
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Kyle Drake
 
Taras Matsyk - Event Driven ML
Taras Matsyk - Event Driven MLTaras Matsyk - Event Driven ML
Taras Matsyk - Event Driven ML
PyCon Odessa
 

Similaire à Rails performance: Ruby GC tweaking (20)

Enhance you APDEX.. naturally!
Enhance you APDEX.. naturally!Enhance you APDEX.. naturally!
Enhance you APDEX.. naturally!
 
Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - Deployment
 
Ruby off Rails (english)
Ruby off Rails (english)Ruby off Rails (english)
Ruby off Rails (english)
 
At&T Interactive: The Many Facets Of Ruby
At&T Interactive: The Many Facets Of RubyAt&T Interactive: The Many Facets Of Ruby
At&T Interactive: The Many Facets Of Ruby
 
Deployment de Rails
Deployment de RailsDeployment de Rails
Deployment de Rails
 
The secret of programming language development and future
The secret of programming  language development and futureThe secret of programming  language development and future
The secret of programming language development and future
 
Ruby on rails
Ruby on railsRuby on rails
Ruby on rails
 
Camel and JBoss
Camel and JBossCamel and JBoss
Camel and JBoss
 
Five cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark fasterFive cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark faster
 
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
 
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋02s0102基于rails的大型b2 b系统架构优化   于冰赵晓锋
02s0102基于rails的大型b2 b系统架构优化 于冰赵晓锋
 
XT Best Practices
XT Best PracticesXT Best Practices
XT Best Practices
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summary
 
PHP at Yahoo!
PHP at Yahoo!PHP at Yahoo!
PHP at Yahoo!
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
 
Apache Sling - The whys and the hows
Apache Sling - The whys and the howsApache Sling - The whys and the hows
Apache Sling - The whys and the hows
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011
 
Concurrency in ruby
Concurrency in rubyConcurrency in ruby
Concurrency in ruby
 
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
 
Taras Matsyk - Event Driven ML
Taras Matsyk - Event Driven MLTaras Matsyk - Event Driven ML
Taras Matsyk - Event Driven ML
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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
 
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...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
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 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...
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 

Rails performance: Ruby GC tweaking

  • 1. Rails performance: Ruby GC tweaking #ParisRB Vlad ZLOTEANU August 2, 2001 @vladzloteanu Software engineer @ Dimelo Copyright Dimelo SA www.dimelo.com
  • 2. What we develop Frontend products -Collaborative platforms, ‘forum-like’, white-labeled, for big accounts (a kind of GetSatisfaction / UserVoice, but.. Better ;) ) Backend product -SocialAPIs client -(a kind of TweetDeck, but for multiple channels, and designed for teams/multiple users per account) Copyright Dimelo SA www.dimelo.com
  • 3. Stats on Dynamic requests - on HTML/XHTML dynamic requests (we don’t count CSS, JS, images, etc) 20 req/s Web Peaks of 40 req/s 10 req/s APIs Peaks of 25 req/s Copyright Dimelo SA www.dimelo.com
  • 4. Problem Medium response time ~ 400 ms WTF?? Copyright Dimelo SA www.dimelo.com
  • 5. Ruby perf toolbox Benchmark Server logs Apache Benchmark (ab, ab2) Profile Rack::Bug RailsBench RubyProf RBTrace Memprof Scrap BleakHouse .. Valgrind ltrace Std surveillance Cacti Nagios Surveilance & profiling NewRelic RPM UnionStation Copyright Dimelo SA www.dimelo.com
  • 6. Finding the problem(s) - DB queries (unoptimized queries, n+1) - External services (EG external RSS feeds) - Lots of partials, low or inexistent cache - Slow session storage - Memory issues - Unoptimized GC - Memory leaks Copyright Dimelo SA www.dimelo.com
  • 7. Memory / GC issues - How do I know it’s a memory/GC issue? - Server logs - Profilers (memprof) - Monitorization services (newrelic) Copyright Dimelo SA www.dimelo.com
  • 8. Ruby Garbage Collector - Conservative - Stop-the-world - Mark-and-sweep process Copyright Dimelo SA www.dimelo.com
  • 9. Ruby default GC settings RUBY_HEAP_MIN_SLOTS=10000 -Initial number of heap slots. It also represents the minimum number of slots, at all times -a new rails app boots up with almost 500k objects on the heap(mostly nodes) RUBY_HEAP_SLOTS_INCREMENT=10000 -The number of new slots to allocate when all initial slots are used RUBY_HEAP_SLOTS_GROWTH_FACTOR=1.8 -Next time Ruby needs new heap slots it will use a multiplicator -New stab is almost double sized Copyright Dimelo SA www.dimelo.com
  • 10. Ruby default GC settings (2) RUBY_GC_MALLOC_LIMIT=8000000 -force GC after 8 MB malloc/realloc -High traffic Rails servers can easily allocate more than 8 MB per request RUBY_HEAP_FREE_MIN=4096 -The number of free slots that should be present after GC finishes running. If there are fewer slots than those defined it will allocate new ones Copyright Dimelo SA www.dimelo.com
  • 11. Find your numbers - Start with enough memory to hold Rails (Ruby’s default is practically nothing) - Increase it linearly if you need more (Ruby’s default is exponential increase) - Only garbage-collect every XX million malloc calls (Ruby’s default is way too small) - Benchmark, benchmark, benchmark.. Copyright Dimelo SA www.dimelo.com
  • 12. GitHub numbers • RUBY_HEAP_MIN_SLOTS=800000 (10000) • RUBY_HEAP_FREE_MIN=100000 (4096) • RUBY_HEAP_SLOTS_INCREMENT=300000(100 00) • RUBY_HEAP_SLOTS_GROWTH_FACTOR=1(1.8) • RUBY_GC_MALLOC_LIMIT=79000000(8000000 ) Copyright Dimelo SA www.dimelo.com
  • 13. Demo (using ApacheBench) - Frontend app, 15 requests, concurrency 1 Before optimisations: Memory usage: 130 MB Execution time(ms): 228 344 322.0 316 4797 After optimisations: Memory usage: 160 MB Execution time(ms): 120 181 190.0 150 2256 Copyright Dimelo SA www.dimelo.com
  • 14. Conclusions - use REE on prod (or Ruby 1.9) - measure, measure, measure (don’t forget to GC.enable_stats) - optimise what matters - And yes, 5 lines of code can make a difference ;) Copyright Dimelo SA www.dimelo.com
  • 15. Thank you! • Questions? Copyright Dimelo SA www.dimelo.com