SlideShare a Scribd company logo
1 of 63
The 7 main actions we took to improve
the Rails stack performance at Justin.tv



        Guillaume Luccisano - June 2011
Guillaume Luccisano

• Work with ruby for about 4 years now
• Arrived from France 8 months ago
• Ran the migration from Rails 2.1 to 2.3..
• ... and to Rails 3 more recently
In the meantine....


 I was able to work on
improving performance
I love it

And I have learn ton of stuff to
    share with you today
First lesson



Guess what?
They lied to us


Rails 3 is slower
  than Rails 2
Ok, troll is over


So what’s making
 our app slow ?
First one is obvious



    (no)   SQL
Less obvious



Worker queue wait
External dependencies
Yes, it can



Memcached
That one, we love it, so ...




     Ruby
And its dark side




The garbage collector
Cool, we have made some
good guesses, now what ?
The Justin.tv Rails stack




            12 frontend servers



Nginx + cache layer with a magic conf from our experts
Talking to 24 app servers
24

23: one is currently dead
R.I.P app2




Unicorn running with 21 app workers per box
                    504 workers, yeah!

Running Ruby Entreprise Edition with GC tuning
Running at 10% of capacity during normal time


      We can hit 80K+ Rails requests
     per minute easily during peak time
One beefy master DB with 7 slaves
             (not used only by rails)




And only 2 memcached boxes for rails
+ some mongo, rabbit, etc...

  And haproxy everywhere
to make us failure resistant...
Cool, so are we finally going
 to optimize stuff or not?
Yes, the real first step is:



Monitoring
You can’t improve
performance in the dark
Newrelic
   =
Awesome
Ganglia
   =
Awesome
Make yourself happy by
improving things and seeing
    instantly the result
This is the kind of graph we are looking for




  Still work to do, but it was going down!
Great, we are all set

But eh, what is fast btw?
Fast
  =
Nothing
Less code
         =
Easier to maintain
         =
       (often)


     Faster
I’m curious
What is a good average response time?


               300ms
               200ms
               100ms
               50ms
we were at 250+ms

We are now at 80ms

And there is still ton to do!
How did we do that?
But keep in mind that
 Every app is unique
1) SQL

• Tracked down slow queries, added Indexes
• Refactored bad queries
  •   Sometime, 2 queries is faster than a big one


• Retrievednetwork and lesscolumns from the db
            only needed
  •Less db                  ruby object creation!
2) C libraries - why?

• We added a bunch of C libraries
• Bypass ruby memory management
 • less garbage collection of ruby objects
• Raw C speed!
• Easy to drop in
2) C libraries - which?

• Curb for HTTP (libcurl) (with real timeout)
  •   Support pipelining for mutli requests at the same time

• Yajl, the fastest JSON library
• Nokogiri, the fastest XML library
• Snappy, super fast compression tool by google
Yajl: the facts
   ~3.5x faster than JSON.generate
    ~1.9x faster than JSON.parse
     ~4.5x faster than YAML.load
~377.5x faster than YAML.dump
    ~1.5x faster than Marshal.load
 ~2x faster than Marshal.dump

All of this while taking less memory!
Snappy: the facts
ruby-1.9.2-p180 :061 > f = File.read('Gemfile')
ruby-1.9.2-p180 :066 > f.length
 => 2504

GC.start; Benchmark.measure { 1000.times { ActiveSupport::Gzip.compress(f) } }
=> 1.840000 0.010000 1.850000 ( 1.842741)
GC.start; Benchmark.measure { 1000.times { Snappy.deflate(f) } }
=> 0.020000 0.000000 0.020000 ( 0.019659)

ruby-1.9.2-p180 :064 > ActiveSupport::Gzip.compress(f).length
 => 971
ruby-1.9.2-p180 :065 > Snappy.deflate(f).length
 => 1398
3) Memcache

•   Upgraded our memcached to the last version!
    •   (Not the gem, the real memcached)
    •   We got a x3 improvements!!



•   Switched everything to the memcached gem
    •   used and made by twitter, use the C libmemcached
    •   3.5 times faster than Dalli on a simple get (ruby equivalent)
3) Memcache
• Used more raw memcache objects
 •   Avoid useless marshal dump
 •   Yajl + Snappy + raw memcache = Win Combo


• Removed huge get_multi         (100+ items)

 •   It can be slower than the sql query equivalent!


• Tuned memcached options
4) Cache expiration


• Removed a ton of after_save cache expiration
 •   Using correct expiration time
 •   Or using auto changing cache_key
5) Switched to Unicorn

•   Like a Boss! Twitter and Github use it.
•   Fast bootup
•   Graceful restart
•   Reduced our queue wait to 0
    •   Our previous round robin dispatch on our mongrels cluster
        added up to 40ms delay on average to each request.
6) More GC tuning

•   Memory vs performance trade off
        •   export RUBY_HEAP_FREE_MIN=100000
        •   export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
        •   export RUBY_HEAP_MIN_SLOTS=800000
        •   export RUBY_HEAP_SLOTS_INCREMENT=200000



•   We added a GC run after expensive requests:
    •   We divided by 3 our time spent in GC during request
7) Regain memory
•   Less objects = Faster garbage collection = happiness
•   Cleaned up our Gemfile and removed unused
    dependencies
    •   aws-s3 gem = 30k ruby objects in your stack
    •   A blank Rails project (2.3 or 3.0) is =~ 100K objects



•   Cleaned up our codebase! Removing tons of old
    controllers/views
Regain memory: the facts

   We refactored our translations system:

          we saved 50k of useless objects:
          10% garbage collection speed up

Enough memory saving to add one more unicorn worker
To go further...
• Create or find a lighter aws s3 gem! using Curb!
• Starting using extra light controller ala Metal for
  some critical actions
• Use snappy to compress fragment caching

• Give a try to kiji, ruby fork of REE (from twitter)
• Or switch the stack to ruby 1.9 or to jRuby
•   Do more memory profiling, with tools like memprof


•   Get a real nonblocking stack to handle several requests
    per worker

    •   Try Goliath: a non blocking ruby framework


•   Try the MySQL nosql plugin (if only we were using MySQL!)
Bonus - Extra slides
removed to save time
Curb: the facts
Memcache - Tune it!
•   Memcached has a bunch of options:
    •   Auto failover and recovery

    •   Noreply, Noblock

    •   Tcp nodelay, UDP

        •   UDP for set and TCP for get?

    •   Key verification

    •   Binary protocol (but slower in ruby, don’t use it :p)

    •   and more.... Play with them!
Clean up your before_filters
     We created a speed_up! method
to skip all before_filters on critical actions

      speed_up! :only => [‘critical’, ‘action’]
find_in_batches
Set.include?
    instead of


Array.include?
url helpers are slow
store them in a variable when you can to avoid multiple calls
use the bang!
like gsub! and avoid new object creation
beware of symbol leak


Every symbol and every string converted to a symbol
      stay forever in memory => memory leak
The Cloud computing era

Cloud is great, but dedicated hardware is still a way faster

      We monitored a x3 when switching socialcam
           from h****u to our own cluster.
FIN
If you are awesome and want to tackle challenges on
         awesome products and systems used
            by millions of users every day:

      We are currently hiring awesome people


              http://jobs.justin.tv

   guillaume@justin.tv     Fork me on Github: @kwi

Recruiting coordinator at JTV: Brooke (brooke@justin.tv)
Links / References

•   https://github.com/miyucy/snappy

•   http://toevolve.org/2011/04/03/http-request-performance.html

•   https://github.com/brianmario/yajl-ruby

•   https://github.com/fauna/memcached

•   http://unicorn.bogomips.org/

•   http://apidock.com/ruby/Set

•   http://engineering.twitter.com/2011/05/faster-ruby-kiji-update.html

•   https://github.com/postrank-labs/goliath

•   http://blogs.innodb.com/wp/2011/04/nosql-to-innodb-with-memcached/

•   https://github.com/ice799/memprof

More Related Content

What's hot

Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage SystemsSATOSHI TAGOMORI
 
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG TorinoDistributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG TorinoCodemotion Tel Aviv
 
Tallinn Estonia Advanced Java Meetup Spark + TensorFlow = TensorFrames Oct 24...
Tallinn Estonia Advanced Java Meetup Spark + TensorFlow = TensorFrames Oct 24...Tallinn Estonia Advanced Java Meetup Spark + TensorFlow = TensorFrames Oct 24...
Tallinn Estonia Advanced Java Meetup Spark + TensorFlow = TensorFrames Oct 24...Chris Fregly
 
Scaling Django for X Factor - DJUGL Oct 2012
Scaling Django for X Factor - DJUGL Oct 2012Scaling Django for X Factor - DJUGL Oct 2012
Scaling Django for X Factor - DJUGL Oct 2012Malcolm Box
 
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...Paolo Negri
 
Introduction to Storm
Introduction to Storm Introduction to Storm
Introduction to Storm Chandler Huang
 
大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会
大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会
大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会Takayuki Kyowa
 
Realtime Statistics based on Apache Storm and RocketMQ
Realtime Statistics based on Apache Storm and RocketMQRealtime Statistics based on Apache Storm and RocketMQ
Realtime Statistics based on Apache Storm and RocketMQXin Wang
 
PHP Backends for Real-Time User Interaction using Apache Storm.
PHP Backends for Real-Time User Interaction using Apache Storm.PHP Backends for Real-Time User Interaction using Apache Storm.
PHP Backends for Real-Time User Interaction using Apache Storm.DECK36
 
Scaling Apache Storm (Hadoop Summit 2015)
Scaling Apache Storm (Hadoop Summit 2015)Scaling Apache Storm (Hadoop Summit 2015)
Scaling Apache Storm (Hadoop Summit 2015)Robert Evans
 
Fluentd at Bay Area Kubernetes Meetup
Fluentd at Bay Area Kubernetes MeetupFluentd at Bay Area Kubernetes Meetup
Fluentd at Bay Area Kubernetes MeetupSadayuki Furuhashi
 
Rapid Application Design in Financial Services
Rapid Application Design in Financial ServicesRapid Application Design in Financial Services
Rapid Application Design in Financial ServicesAerospike
 
[212] large scale backend service develpment
[212] large scale backend service develpment[212] large scale backend service develpment
[212] large scale backend service develpmentNAVER D2
 
Analysis big data by use php with storm
Analysis big data by use php with stormAnalysis big data by use php with storm
Analysis big data by use php with storm毅 吕
 
ELK: Moose-ively scaling your log system
ELK: Moose-ively scaling your log systemELK: Moose-ively scaling your log system
ELK: Moose-ively scaling your log systemAvleen Vig
 
(ARC348) Seagull: How Yelp Built A System For Task Execution
(ARC348) Seagull: How Yelp Built A System For Task Execution(ARC348) Seagull: How Yelp Built A System For Task Execution
(ARC348) Seagull: How Yelp Built A System For Task ExecutionAmazon Web Services
 
Multi-Tenant Storm Service on Hadoop Grid
Multi-Tenant Storm Service on Hadoop GridMulti-Tenant Storm Service on Hadoop Grid
Multi-Tenant Storm Service on Hadoop GridDataWorks Summit
 
Build a Complex, Realtime Data Management App with Postgres 14!
Build a Complex, Realtime Data Management App with Postgres 14!Build a Complex, Realtime Data Management App with Postgres 14!
Build a Complex, Realtime Data Management App with Postgres 14!Jonathan Katz
 
RxJS Animations Talk - 2017
RxJS Animations Talk - 2017RxJS Animations Talk - 2017
RxJS Animations Talk - 2017Ben Lesh
 
Advanced technic for OS upgrading in 3 minutes
Advanced technic for OS upgrading in 3 minutesAdvanced technic for OS upgrading in 3 minutes
Advanced technic for OS upgrading in 3 minutesHiroshi SHIBATA
 

What's hot (20)

Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
 
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG TorinoDistributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
Distributed Systems explained (with NodeJS) - Bruno Bossola, JUG Torino
 
Tallinn Estonia Advanced Java Meetup Spark + TensorFlow = TensorFrames Oct 24...
Tallinn Estonia Advanced Java Meetup Spark + TensorFlow = TensorFrames Oct 24...Tallinn Estonia Advanced Java Meetup Spark + TensorFlow = TensorFrames Oct 24...
Tallinn Estonia Advanced Java Meetup Spark + TensorFlow = TensorFrames Oct 24...
 
Scaling Django for X Factor - DJUGL Oct 2012
Scaling Django for X Factor - DJUGL Oct 2012Scaling Django for X Factor - DJUGL Oct 2012
Scaling Django for X Factor - DJUGL Oct 2012
 
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
Distributed and concurrent programming with RabbitMQ and EventMachine Rails U...
 
Introduction to Storm
Introduction to Storm Introduction to Storm
Introduction to Storm
 
大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会
大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会
大規模環境でRailsと4年間付き合ってきて@ クックパッド * 食べログ合同勉強会
 
Realtime Statistics based on Apache Storm and RocketMQ
Realtime Statistics based on Apache Storm and RocketMQRealtime Statistics based on Apache Storm and RocketMQ
Realtime Statistics based on Apache Storm and RocketMQ
 
PHP Backends for Real-Time User Interaction using Apache Storm.
PHP Backends for Real-Time User Interaction using Apache Storm.PHP Backends for Real-Time User Interaction using Apache Storm.
PHP Backends for Real-Time User Interaction using Apache Storm.
 
Scaling Apache Storm (Hadoop Summit 2015)
Scaling Apache Storm (Hadoop Summit 2015)Scaling Apache Storm (Hadoop Summit 2015)
Scaling Apache Storm (Hadoop Summit 2015)
 
Fluentd at Bay Area Kubernetes Meetup
Fluentd at Bay Area Kubernetes MeetupFluentd at Bay Area Kubernetes Meetup
Fluentd at Bay Area Kubernetes Meetup
 
Rapid Application Design in Financial Services
Rapid Application Design in Financial ServicesRapid Application Design in Financial Services
Rapid Application Design in Financial Services
 
[212] large scale backend service develpment
[212] large scale backend service develpment[212] large scale backend service develpment
[212] large scale backend service develpment
 
Analysis big data by use php with storm
Analysis big data by use php with stormAnalysis big data by use php with storm
Analysis big data by use php with storm
 
ELK: Moose-ively scaling your log system
ELK: Moose-ively scaling your log systemELK: Moose-ively scaling your log system
ELK: Moose-ively scaling your log system
 
(ARC348) Seagull: How Yelp Built A System For Task Execution
(ARC348) Seagull: How Yelp Built A System For Task Execution(ARC348) Seagull: How Yelp Built A System For Task Execution
(ARC348) Seagull: How Yelp Built A System For Task Execution
 
Multi-Tenant Storm Service on Hadoop Grid
Multi-Tenant Storm Service on Hadoop GridMulti-Tenant Storm Service on Hadoop Grid
Multi-Tenant Storm Service on Hadoop Grid
 
Build a Complex, Realtime Data Management App with Postgres 14!
Build a Complex, Realtime Data Management App with Postgres 14!Build a Complex, Realtime Data Management App with Postgres 14!
Build a Complex, Realtime Data Management App with Postgres 14!
 
RxJS Animations Talk - 2017
RxJS Animations Talk - 2017RxJS Animations Talk - 2017
RxJS Animations Talk - 2017
 
Advanced technic for OS upgrading in 3 minutes
Advanced technic for OS upgrading in 3 minutesAdvanced technic for OS upgrading in 3 minutes
Advanced technic for OS upgrading in 3 minutes
 

Similar to Rails performance at Justin.tv - Guillaume Luccisano

Cooking a rabbit pie
Cooking a rabbit pieCooking a rabbit pie
Cooking a rabbit pieTomas Doran
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009pratiknaik
 
Leveraging Databricks for Spark Pipelines
Leveraging Databricks for Spark PipelinesLeveraging Databricks for Spark Pipelines
Leveraging Databricks for Spark PipelinesRose Toomey
 
Leveraging Databricks for Spark pipelines
Leveraging Databricks for Spark pipelinesLeveraging Databricks for Spark pipelines
Leveraging Databricks for Spark pipelinesRose Toomey
 
2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users GroupNitay Joffe
 
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?DATAVERSITY
 
Scaling an ELK stack at bol.com
Scaling an ELK stack at bol.comScaling an ELK stack at bol.com
Scaling an ELK stack at bol.comRenzo Tomà
 
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
"Leveraging the Event Loop for Blazing-Fast Applications!",  Michael Di Prisco"Leveraging the Event Loop for Blazing-Fast Applications!",  Michael Di Prisco
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di PriscoFwdays
 
2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwordsNitay Joffe
 
Scality S3 Server: Node js Meetup Presentation
Scality S3 Server: Node js Meetup PresentationScality S3 Server: Node js Meetup Presentation
Scality S3 Server: Node js Meetup PresentationScality
 
Keeping MongoDB Data Safe
Keeping MongoDB Data SafeKeeping MongoDB Data Safe
Keeping MongoDB Data SafeTony Tam
 
Puppet Camp New York 2014: Streamlining Puppet Development Workflow
Puppet Camp New York 2014: Streamlining Puppet Development Workflow Puppet Camp New York 2014: Streamlining Puppet Development Workflow
Puppet Camp New York 2014: Streamlining Puppet Development Workflow Puppet
 
Steamlining your puppet development workflow
Steamlining your puppet development workflowSteamlining your puppet development workflow
Steamlining your puppet development workflowTomas Doran
 
Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...
Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...
Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...Danielle Womboldt
 
Ceph Day Beijing - Our Journey to High Performance Large Scale Ceph Cluster a...
Ceph Day Beijing - Our Journey to High Performance Large Scale Ceph Cluster a...Ceph Day Beijing - Our Journey to High Performance Large Scale Ceph Cluster a...
Ceph Day Beijing - Our Journey to High Performance Large Scale Ceph Cluster a...Ceph Community
 
Ruby performance - The low hanging fruit
Ruby performance - The low hanging fruitRuby performance - The low hanging fruit
Ruby performance - The low hanging fruitBruce Werdschinski
 
Chirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterChirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterJohn Adams
 
[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary
[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary
[DBA]_HiramFleitas_SQL_PASS_Summit_2017_SummaryHiram Fleitas León
 

Similar to Rails performance at Justin.tv - Guillaume Luccisano (20)

Cooking a rabbit pie
Cooking a rabbit pieCooking a rabbit pie
Cooking a rabbit pie
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009
 
Leveraging Databricks for Spark Pipelines
Leveraging Databricks for Spark PipelinesLeveraging Databricks for Spark Pipelines
Leveraging Databricks for Spark Pipelines
 
Leveraging Databricks for Spark pipelines
Leveraging Databricks for Spark pipelinesLeveraging Databricks for Spark pipelines
Leveraging Databricks for Spark pipelines
 
2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group
 
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
 
Scaling an ELK stack at bol.com
Scaling an ELK stack at bol.comScaling an ELK stack at bol.com
Scaling an ELK stack at bol.com
 
Dibi Conference 2012
Dibi Conference 2012Dibi Conference 2012
Dibi Conference 2012
 
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
"Leveraging the Event Loop for Blazing-Fast Applications!",  Michael Di Prisco"Leveraging the Event Loop for Blazing-Fast Applications!",  Michael Di Prisco
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
 
2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords
 
Scality S3 Server: Node js Meetup Presentation
Scality S3 Server: Node js Meetup PresentationScality S3 Server: Node js Meetup Presentation
Scality S3 Server: Node js Meetup Presentation
 
Keeping MongoDB Data Safe
Keeping MongoDB Data SafeKeeping MongoDB Data Safe
Keeping MongoDB Data Safe
 
Puppet Camp New York 2014: Streamlining Puppet Development Workflow
Puppet Camp New York 2014: Streamlining Puppet Development Workflow Puppet Camp New York 2014: Streamlining Puppet Development Workflow
Puppet Camp New York 2014: Streamlining Puppet Development Workflow
 
Steamlining your puppet development workflow
Steamlining your puppet development workflowSteamlining your puppet development workflow
Steamlining your puppet development workflow
 
Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...
Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...
Ceph Day Beijing - Our journey to high performance large scale Ceph cluster a...
 
Ceph Day Beijing - Our Journey to High Performance Large Scale Ceph Cluster a...
Ceph Day Beijing - Our Journey to High Performance Large Scale Ceph Cluster a...Ceph Day Beijing - Our Journey to High Performance Large Scale Ceph Cluster a...
Ceph Day Beijing - Our Journey to High Performance Large Scale Ceph Cluster a...
 
Ruby performance - The low hanging fruit
Ruby performance - The low hanging fruitRuby performance - The low hanging fruit
Ruby performance - The low hanging fruit
 
Chirp 2010: Scaling Twitter
Chirp 2010: Scaling TwitterChirp 2010: Scaling Twitter
Chirp 2010: Scaling Twitter
 
Compression talk
Compression talkCompression talk
Compression talk
 
[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary
[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary
[DBA]_HiramFleitas_SQL_PASS_Summit_2017_Summary
 

Recently uploaded

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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.pdfEnterprise Knowledge
 
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 interpreternaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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.pptxKatpro Technologies
 
[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.pdfhans926745
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
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 2024The Digital Insurer
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
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 RobisonAnna Loughnan Colquhoun
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
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 2024Results
 
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 WorkerThousandEyes
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
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...Drew Madelung
 

Recently uploaded (20)

Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
[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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
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 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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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...
 

Rails performance at Justin.tv - Guillaume Luccisano

  • 1. The 7 main actions we took to improve the Rails stack performance at Justin.tv Guillaume Luccisano - June 2011
  • 2. Guillaume Luccisano • Work with ruby for about 4 years now • Arrived from France 8 months ago • Ran the migration from Rails 2.1 to 2.3.. • ... and to Rails 3 more recently
  • 3. In the meantine.... I was able to work on improving performance
  • 4. I love it And I have learn ton of stuff to share with you today
  • 6. They lied to us Rails 3 is slower than Rails 2
  • 7. Ok, troll is over So what’s making our app slow ?
  • 8. First one is obvious (no) SQL
  • 12. That one, we love it, so ... Ruby
  • 13. And its dark side The garbage collector
  • 14. Cool, we have made some good guesses, now what ?
  • 15. The Justin.tv Rails stack 12 frontend servers Nginx + cache layer with a magic conf from our experts
  • 16. Talking to 24 app servers
  • 17. 24 23: one is currently dead
  • 18. R.I.P app2 Unicorn running with 21 app workers per box 504 workers, yeah! Running Ruby Entreprise Edition with GC tuning
  • 19. Running at 10% of capacity during normal time We can hit 80K+ Rails requests per minute easily during peak time
  • 20. One beefy master DB with 7 slaves (not used only by rails) And only 2 memcached boxes for rails
  • 21. + some mongo, rabbit, etc... And haproxy everywhere to make us failure resistant...
  • 22. Cool, so are we finally going to optimize stuff or not?
  • 23. Yes, the real first step is: Monitoring
  • 25. Newrelic = Awesome
  • 26. Ganglia = Awesome
  • 27. Make yourself happy by improving things and seeing instantly the result
  • 28. This is the kind of graph we are looking for Still work to do, but it was going down!
  • 29. Great, we are all set But eh, what is fast btw?
  • 31. Less code = Easier to maintain = (often) Faster
  • 32. I’m curious What is a good average response time? 300ms 200ms 100ms 50ms
  • 33. we were at 250+ms We are now at 80ms And there is still ton to do!
  • 34. How did we do that?
  • 35. But keep in mind that Every app is unique
  • 36. 1) SQL • Tracked down slow queries, added Indexes • Refactored bad queries • Sometime, 2 queries is faster than a big one • Retrievednetwork and lesscolumns from the db only needed •Less db ruby object creation!
  • 37. 2) C libraries - why? • We added a bunch of C libraries • Bypass ruby memory management • less garbage collection of ruby objects • Raw C speed! • Easy to drop in
  • 38. 2) C libraries - which? • Curb for HTTP (libcurl) (with real timeout) • Support pipelining for mutli requests at the same time • Yajl, the fastest JSON library • Nokogiri, the fastest XML library • Snappy, super fast compression tool by google
  • 39. Yajl: the facts ~3.5x faster than JSON.generate ~1.9x faster than JSON.parse ~4.5x faster than YAML.load ~377.5x faster than YAML.dump ~1.5x faster than Marshal.load ~2x faster than Marshal.dump All of this while taking less memory!
  • 40. Snappy: the facts ruby-1.9.2-p180 :061 > f = File.read('Gemfile') ruby-1.9.2-p180 :066 > f.length => 2504 GC.start; Benchmark.measure { 1000.times { ActiveSupport::Gzip.compress(f) } } => 1.840000 0.010000 1.850000 ( 1.842741) GC.start; Benchmark.measure { 1000.times { Snappy.deflate(f) } } => 0.020000 0.000000 0.020000 ( 0.019659) ruby-1.9.2-p180 :064 > ActiveSupport::Gzip.compress(f).length => 971 ruby-1.9.2-p180 :065 > Snappy.deflate(f).length => 1398
  • 41. 3) Memcache • Upgraded our memcached to the last version! • (Not the gem, the real memcached) • We got a x3 improvements!! • Switched everything to the memcached gem • used and made by twitter, use the C libmemcached • 3.5 times faster than Dalli on a simple get (ruby equivalent)
  • 42. 3) Memcache • Used more raw memcache objects • Avoid useless marshal dump • Yajl + Snappy + raw memcache = Win Combo • Removed huge get_multi (100+ items) • It can be slower than the sql query equivalent! • Tuned memcached options
  • 43. 4) Cache expiration • Removed a ton of after_save cache expiration • Using correct expiration time • Or using auto changing cache_key
  • 44. 5) Switched to Unicorn • Like a Boss! Twitter and Github use it. • Fast bootup • Graceful restart • Reduced our queue wait to 0 • Our previous round robin dispatch on our mongrels cluster added up to 40ms delay on average to each request.
  • 45. 6) More GC tuning • Memory vs performance trade off • export RUBY_HEAP_FREE_MIN=100000 • export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1 • export RUBY_HEAP_MIN_SLOTS=800000 • export RUBY_HEAP_SLOTS_INCREMENT=200000 • We added a GC run after expensive requests: • We divided by 3 our time spent in GC during request
  • 46. 7) Regain memory • Less objects = Faster garbage collection = happiness • Cleaned up our Gemfile and removed unused dependencies • aws-s3 gem = 30k ruby objects in your stack • A blank Rails project (2.3 or 3.0) is =~ 100K objects • Cleaned up our codebase! Removing tons of old controllers/views
  • 47. Regain memory: the facts We refactored our translations system: we saved 50k of useless objects: 10% garbage collection speed up Enough memory saving to add one more unicorn worker
  • 49. • Create or find a lighter aws s3 gem! using Curb! • Starting using extra light controller ala Metal for some critical actions • Use snappy to compress fragment caching • Give a try to kiji, ruby fork of REE (from twitter) • Or switch the stack to ruby 1.9 or to jRuby
  • 50. Do more memory profiling, with tools like memprof • Get a real nonblocking stack to handle several requests per worker • Try Goliath: a non blocking ruby framework • Try the MySQL nosql plugin (if only we were using MySQL!)
  • 51. Bonus - Extra slides removed to save time
  • 53. Memcache - Tune it! • Memcached has a bunch of options: • Auto failover and recovery • Noreply, Noblock • Tcp nodelay, UDP • UDP for set and TCP for get? • Key verification • Binary protocol (but slower in ruby, don’t use it :p) • and more.... Play with them!
  • 54. Clean up your before_filters We created a speed_up! method to skip all before_filters on critical actions speed_up! :only => [‘critical’, ‘action’]
  • 56. Set.include? instead of Array.include?
  • 57. url helpers are slow store them in a variable when you can to avoid multiple calls
  • 58. use the bang! like gsub! and avoid new object creation
  • 59. beware of symbol leak Every symbol and every string converted to a symbol stay forever in memory => memory leak
  • 60. The Cloud computing era Cloud is great, but dedicated hardware is still a way faster We monitored a x3 when switching socialcam from h****u to our own cluster.
  • 61. FIN
  • 62. If you are awesome and want to tackle challenges on awesome products and systems used by millions of users every day: We are currently hiring awesome people http://jobs.justin.tv guillaume@justin.tv Fork me on Github: @kwi Recruiting coordinator at JTV: Brooke (brooke@justin.tv)
  • 63. Links / References • https://github.com/miyucy/snappy • http://toevolve.org/2011/04/03/http-request-performance.html • https://github.com/brianmario/yajl-ruby • https://github.com/fauna/memcached • http://unicorn.bogomips.org/ • http://apidock.com/ruby/Set • http://engineering.twitter.com/2011/05/faster-ruby-kiji-update.html • https://github.com/postrank-labs/goliath • http://blogs.innodb.com/wp/2011/04/nosql-to-innodb-with-memcached/ • https://github.com/ice799/memprof

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n