淺談Ruby process fork應用

Y
YC LingEngineer at HTC
淺談Ruby	
  Process	
  Fork應用	
  

                  大貓	
  	
  
      (YC	
  Ling,	
  @miaout17)
Applica@on	
  Concurrency
•  Event-­‐Driven	
  I/O	
  (Ex.	
  Eventmachine,	
  goliath)	
  
       –  Everything	
  need	
  to	
  be	
  non-­‐blocking.	
  	
  
•  Worker	
  Threads	
  (Ex.	
  Rainbows	
  with	
  proper	
  config)	
  
       –  Everything	
  need	
  to	
  be	
  thread-­‐safe.	
  	
  
•  Worker	
  Processes	
  (Ex.	
  unicorn)	
  
       –  OS	
  process	
  is	
  expensive	
  
       –  Reality:	
  You	
  are	
  using	
  a	
  lot	
  of	
  3rd	
  party	
  libraries,	
  some	
  
          of	
  them	
  are	
  blocking	
  and	
  not	
  thread-­‐safe	
  
       –  S@ll	
  mainstream	
  today	
  
       –  I	
  don’t	
  like	
  it	
  very	
  much,	
  but	
  it	
  just	
  works	
  
	
  
Worker	
  Processes	
  
               Memory	
  Usage
80MB




Process	
  
   1
Worker	
  Processes	
  
                             Memory	
  Usage
80MB
         80MB
          80MB
        80MB
         80MB
         80MB
         80MB
         80MB




Process	
     Process	
     Process	
     Process	
     Process	
     Process	
     Process	
     Process	
  
   1
            2
            3
            4
            5
            6
            7
            8
Forked	
  Worker	
  Processes	
  (Ex.	
  Unicorn)	
  
     Memory	
  Usage:	
  Copy	
  on	
  Write
God	
  
Monit




Master	
                Process	
     Process	
     Process	
     Process	
     Process	
     Process	
     Process	
     Process	
  
Process
                   1
            2
            3
            4
            5
            6
            7
            8


             Normal	
  Memory	
  Page
                                (CoW)Read-­‐Only	
  Page
Worker	
  Processes	
  
                               Startup	
  Time
Process	
  1
Process	
  2
Process	
  3
Process	
  4
Process	
  5
Process	
  6
Process	
  7
Process	
  8
                                                                                         Time


          •  Assuming	
  2	
  cores	
  
          •  The	
  8	
  processes	
  could	
  be	
  started	
  simultaneously	
  and	
  the	
  
             startup	
  will	
  be	
  execute	
  concurrently	
  with	
  content	
  switching.	
  
Forked	
  Worker	
  Processes	
  (Ex.	
  Unicorn)	
  
                   Startup	
  Time
Master	
  Process
                       Loading	
  environment
    Process	
  1
    Process	
  2
    Process	
  3
    Process	
  4
                            Forking
    Process	
  5
    Process	
  6
    Process	
  7
    Process	
  8
                                                  Time


              •  Load	
  the	
  applica@on	
  ONCE	
  
              •  Fork	
  is	
  FAST	
  
Resque
•    The	
  master	
  process	
  loads	
  the	
  environment.	
  	
  
•    Whenever	
  get	
  a	
  new	
  job,	
  fork	
  a	
  worker	
  process.	
  	
  
•    Whenever	
  the	
  job	
  is	
  done,	
  the	
  worker	
  process	
  exits.	
  	
  
•    For	
  isola@on:	
  Resque	
  assumes	
  chaos

                                             Worker	
  
                        Got	
  a	
  job!
    Process
      Job	
  done	
  
                        Fork	
  Process
                   Worker	
  process	
  terminates	
  


                                             Master	
  
                                             Process
50	
  resque	
  workers?
•  Don’t	
  do	
  this:	
  
    –  COUNT=50	
  QUEUE=*	
  rake	
  resque:workers	
  
•  80MB*50	
  =	
  4GB

                Worker	
                   Worker	
     Worker	
  
                Process
                   Process
     Process



   Master	
     Master	
      Master	
     Master	
     Master	
     Master	
     …	
  
   Process
     Process
      Process
     Process
     Process
     Process
   80	
  MB
    80	
  MB
     80	
  MB
    80	
  MB
    80	
  MB
    80	
  MB
resque-­‐pool
•  hgps://github.com/nevans/resque-­‐pool	
  



                       Worker	
                         Worker	
     Worker	
  
                       Process
                         Process
     Process

        Fork
  Master	
             Master	
     Master	
            Master	
     Master	
     Master	
  
  Process
             Process
     Process
            Process
     Process
     Process


               Fork
                                             Pool	
  
                                            Manager
Summary
•  Process	
  forking	
  in	
  Ruby	
  
    –  For	
  isola@on	
  
        •  Prevent	
  process	
  status	
  corrup@on	
  
        •  See	
  also:	
  spork-­‐rails	
  
    –  For	
  concurrency	
  
        •  Faster	
  startup	
  
        •  Smaller	
  memory	
  footprint	
  
1 of 11

Recommended

JVM Memory Management Details by
JVM Memory Management DetailsJVM Memory Management Details
JVM Memory Management DetailsAzul Systems Inc.
3.5K views47 slides
JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScript by
JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScriptJS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScript
JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScriptJSFestUA
573 views38 slides
Steamlining your puppet development workflow by
Steamlining your puppet development workflowSteamlining your puppet development workflow
Steamlining your puppet development workflowTomas Doran
4.5K views36 slides
Gearman by
GearmanGearman
GearmanBrian Moon
2.1K views40 slides
Gevent at TellApart by
Gevent at TellApartGevent at TellApart
Gevent at TellApartKevin Ballard
646 views17 slides
Cooking a rabbit pie by
Cooking a rabbit pieCooking a rabbit pie
Cooking a rabbit pieTomas Doran
1.1K views46 slides

More Related Content

What's hot

20140419 oedo rubykaigi04 by
20140419 oedo rubykaigi0420140419 oedo rubykaigi04
20140419 oedo rubykaigi04Hiroshi SHIBATA
790 views85 slides
Distributed Queue System using Gearman by
Distributed Queue System using GearmanDistributed Queue System using Gearman
Distributed Queue System using GearmanEric Cho
20.5K views32 slides
Queue your work by
Queue your workQueue your work
Queue your workJurian Sluiman
5.9K views39 slides
Sensu and Sensibility - Puppetconf 2014 by
Sensu and Sensibility - Puppetconf 2014Sensu and Sensibility - Puppetconf 2014
Sensu and Sensibility - Puppetconf 2014Tomas Doran
5.2K views58 slides
20140425 ruby conftaiwan2014 by
20140425 ruby conftaiwan201420140425 ruby conftaiwan2014
20140425 ruby conftaiwan2014Hiroshi SHIBATA
1.4K views85 slides
I know why your Java is slow by
I know why your Java is slowI know why your Java is slow
I know why your Java is slowaragozin
1.2K views27 slides

What's hot(12)

Distributed Queue System using Gearman by Eric Cho
Distributed Queue System using GearmanDistributed Queue System using Gearman
Distributed Queue System using Gearman
Eric Cho20.5K views
Sensu and Sensibility - Puppetconf 2014 by Tomas Doran
Sensu and Sensibility - Puppetconf 2014Sensu and Sensibility - Puppetconf 2014
Sensu and Sensibility - Puppetconf 2014
Tomas Doran5.2K views
I know why your Java is slow by aragozin
I know why your Java is slowI know why your Java is slow
I know why your Java is slow
aragozin1.2K views
Dockersh and a brief intro to the docker internals by Tomas Doran
Dockersh and a brief intro to the docker internalsDockersh and a brief intro to the docker internals
Dockersh and a brief intro to the docker internals
Tomas Doran4K views
ZendCon 2015 - DevOps for Small Teams by Joe Ferguson
ZendCon 2015 - DevOps for Small TeamsZendCon 2015 - DevOps for Small Teams
ZendCon 2015 - DevOps for Small Teams
Joe Ferguson949 views
Messaging, interoperability and log aggregation - a new framework by Tomas Doran
Messaging, interoperability and log aggregation - a new frameworkMessaging, interoperability and log aggregation - a new framework
Messaging, interoperability and log aggregation - a new framework
Tomas Doran3K views
Live migrating a container: pros, cons and gotchas by Docker, Inc.
Live migrating a container: pros, cons and gotchasLive migrating a container: pros, cons and gotchas
Live migrating a container: pros, cons and gotchas
Docker, Inc.13K views
AVA - a futuristic test runner by andreaslubbe
AVA - a futuristic test runnerAVA - a futuristic test runner
AVA - a futuristic test runner
andreaslubbe974 views

Similar to 淺談Ruby process fork應用

Storm distributed processing by
Storm distributed processingStorm distributed processing
Storm distributed processingducquoc_vn
6.1K views34 slides
Parallel batch processing with spring batch slideshare by
Parallel batch processing with spring batch   slideshareParallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch slideshareMorten Andersen-Gott
15.9K views77 slides
Caching your rails application by
Caching your rails applicationCaching your rails application
Caching your rails applicationArrrrCamp
1.7K views55 slides
Lessons Learnt in 2009 by
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009pratiknaik
1.8K views98 slides
Ruby and Distributed Storage Systems by
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage SystemsSATOSHI TAGOMORI
13.3K views43 slides
ruby test-all parallel running by
ruby test-all parallel runningruby test-all parallel running
ruby test-all parallel runningShota Fukumori
1.8K views42 slides

Similar to 淺談Ruby process fork應用(20)

Storm distributed processing by ducquoc_vn
Storm distributed processingStorm distributed processing
Storm distributed processing
ducquoc_vn6.1K views
Parallel batch processing with spring batch slideshare by Morten Andersen-Gott
Parallel batch processing with spring batch   slideshareParallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch slideshare
Morten Andersen-Gott15.9K views
Caching your rails application by ArrrrCamp
Caching your rails applicationCaching your rails application
Caching your rails application
ArrrrCamp1.7K views
Lessons Learnt in 2009 by pratiknaik
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009
pratiknaik1.8K views
Ruby and Distributed Storage Systems by SATOSHI TAGOMORI
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
SATOSHI TAGOMORI13.3K views
ruby test-all parallel running by Shota Fukumori
ruby test-all parallel runningruby test-all parallel running
ruby test-all parallel running
Shota Fukumori1.8K views
Alluxio - Scalable Filesystem Metadata Services by Alluxio, Inc.
Alluxio - Scalable Filesystem Metadata ServicesAlluxio - Scalable Filesystem Metadata Services
Alluxio - Scalable Filesystem Metadata Services
Alluxio, Inc.2.2K views
Azug - successfully breeding rabits by Yves Goeleven
Azug - successfully breeding rabitsAzug - successfully breeding rabits
Azug - successfully breeding rabits
Yves Goeleven518 views
Keeping MongoDB Data Safe by Tony Tam
Keeping MongoDB Data SafeKeeping MongoDB Data Safe
Keeping MongoDB Data Safe
Tony Tam6K views
BTV PHP - Building Fast Websites by Jonathan Klein
BTV PHP - Building Fast WebsitesBTV PHP - Building Fast Websites
BTV PHP - Building Fast Websites
Jonathan Klein1.6K views
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire) by Tech in Asia ID
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
Tech in Asia ID657 views
Ship It ! with Ruby/ Rails Ecosystem by Yi-Ting Cheng
Ship It ! with Ruby/ Rails EcosystemShip It ! with Ruby/ Rails Ecosystem
Ship It ! with Ruby/ Rails Ecosystem
Yi-Ting Cheng1.8K views
Machine Learning With H2O vs SparkML by Arnab Biswas
Machine Learning With H2O vs SparkMLMachine Learning With H2O vs SparkML
Machine Learning With H2O vs SparkML
Arnab Biswas239 views
My site is slow by hernanibf
My site is slowMy site is slow
My site is slow
hernanibf2.2K views
Gearman - Northeast PHP 2012 by Mike Willbanks
Gearman - Northeast PHP 2012Gearman - Northeast PHP 2012
Gearman - Northeast PHP 2012
Mike Willbanks2.5K views
Queick: A Simple Job Queue System for Python by Ryota Suenaga
Queick: A Simple Job Queue System for PythonQueick: A Simple Job Queue System for Python
Queick: A Simple Job Queue System for Python
Ryota Suenaga564 views
My Site is slow - Drupal Camp London 2013 by hernanibf
My Site is slow - Drupal Camp London 2013My Site is slow - Drupal Camp London 2013
My Site is slow - Drupal Camp London 2013
hernanibf4.3K views
Host and Boast: Best Practices for Magento Hosting | Imagine 2013 Technolog… by Atwix
Host and Boast: Best Practices for Magento Hosting | Imagine 2013 Technolog…Host and Boast: Best Practices for Magento Hosting | Imagine 2013 Technolog…
Host and Boast: Best Practices for Magento Hosting | Imagine 2013 Technolog…
Atwix1K views
Hands-on Performance Tuning Lab - Devoxx Poland by C2B2 Consulting
Hands-on Performance Tuning Lab - Devoxx PolandHands-on Performance Tuning Lab - Devoxx Poland
Hands-on Performance Tuning Lab - Devoxx Poland
C2B2 Consulting2K views

More from YC Ling

那些函數語言Tutorial沒有教我的事 by
那些函數語言Tutorial沒有教我的事那些函數語言Tutorial沒有教我的事
那些函數語言Tutorial沒有教我的事YC Ling
6.7K views22 slides
Learn Haskell The Easy Way by
Learn Haskell The Easy WayLearn Haskell The Easy Way
Learn Haskell The Easy WayYC Ling
5.7K views26 slides
[OSDC12]相依性管理 - 以Ruby開發為例 by
[OSDC12]相依性管理 - 以Ruby開發為例[OSDC12]相依性管理 - 以Ruby開發為例
[OSDC12]相依性管理 - 以Ruby開發為例YC Ling
5.5K views41 slides
Lol Programming by
Lol ProgrammingLol Programming
Lol ProgrammingYC Ling
447 views16 slides
Introduction of Reverse Engineering by
Introduction of Reverse EngineeringIntroduction of Reverse Engineering
Introduction of Reverse EngineeringYC Ling
1.1K views33 slides
Lightning Talk: RichRC & Hirb-Unicode by
Lightning Talk: RichRC & Hirb-UnicodeLightning Talk: RichRC & Hirb-Unicode
Lightning Talk: RichRC & Hirb-UnicodeYC Ling
618 views8 slides

More from YC Ling(7)

那些函數語言Tutorial沒有教我的事 by YC Ling
那些函數語言Tutorial沒有教我的事那些函數語言Tutorial沒有教我的事
那些函數語言Tutorial沒有教我的事
YC Ling6.7K views
Learn Haskell The Easy Way by YC Ling
Learn Haskell The Easy WayLearn Haskell The Easy Way
Learn Haskell The Easy Way
YC Ling5.7K views
[OSDC12]相依性管理 - 以Ruby開發為例 by YC Ling
[OSDC12]相依性管理 - 以Ruby開發為例[OSDC12]相依性管理 - 以Ruby開發為例
[OSDC12]相依性管理 - 以Ruby開發為例
YC Ling5.5K views
Lol Programming by YC Ling
Lol ProgrammingLol Programming
Lol Programming
YC Ling447 views
Introduction of Reverse Engineering by YC Ling
Introduction of Reverse EngineeringIntroduction of Reverse Engineering
Introduction of Reverse Engineering
YC Ling1.1K views
Lightning Talk: RichRC & Hirb-Unicode by YC Ling
Lightning Talk: RichRC & Hirb-UnicodeLightning Talk: RichRC & Hirb-Unicode
Lightning Talk: RichRC & Hirb-Unicode
YC Ling618 views
Learning Ruby with RubyWarrior by YC Ling
Learning Ruby with RubyWarriorLearning Ruby with RubyWarrior
Learning Ruby with RubyWarrior
YC Ling1.5K views

淺談Ruby process fork應用

  • 1. 淺談Ruby  Process  Fork應用   大貓     (YC  Ling,  @miaout17)
  • 2. Applica@on  Concurrency •  Event-­‐Driven  I/O  (Ex.  Eventmachine,  goliath)   –  Everything  need  to  be  non-­‐blocking.     •  Worker  Threads  (Ex.  Rainbows  with  proper  config)   –  Everything  need  to  be  thread-­‐safe.     •  Worker  Processes  (Ex.  unicorn)   –  OS  process  is  expensive   –  Reality:  You  are  using  a  lot  of  3rd  party  libraries,  some   of  them  are  blocking  and  not  thread-­‐safe   –  S@ll  mainstream  today   –  I  don’t  like  it  very  much,  but  it  just  works    
  • 3. Worker  Processes   Memory  Usage 80MB Process   1
  • 4. Worker  Processes   Memory  Usage 80MB 80MB 80MB 80MB 80MB 80MB 80MB 80MB Process   Process   Process   Process   Process   Process   Process   Process   1 2 3 4 5 6 7 8
  • 5. Forked  Worker  Processes  (Ex.  Unicorn)   Memory  Usage:  Copy  on  Write God   Monit Master   Process   Process   Process   Process   Process   Process   Process   Process   Process 1 2 3 4 5 6 7 8 Normal  Memory  Page (CoW)Read-­‐Only  Page
  • 6. Worker  Processes   Startup  Time Process  1 Process  2 Process  3 Process  4 Process  5 Process  6 Process  7 Process  8 Time •  Assuming  2  cores   •  The  8  processes  could  be  started  simultaneously  and  the   startup  will  be  execute  concurrently  with  content  switching.  
  • 7. Forked  Worker  Processes  (Ex.  Unicorn)   Startup  Time Master  Process Loading  environment Process  1 Process  2 Process  3 Process  4 Forking Process  5 Process  6 Process  7 Process  8 Time •  Load  the  applica@on  ONCE   •  Fork  is  FAST  
  • 8. Resque •  The  master  process  loads  the  environment.     •  Whenever  get  a  new  job,  fork  a  worker  process.     •  Whenever  the  job  is  done,  the  worker  process  exits.     •  For  isola@on:  Resque  assumes  chaos Worker   Got  a  job! Process Job  done   Fork  Process Worker  process  terminates   Master   Process
  • 9. 50  resque  workers? •  Don’t  do  this:   –  COUNT=50  QUEUE=*  rake  resque:workers   •  80MB*50  =  4GB Worker   Worker   Worker   Process Process Process Master   Master   Master   Master   Master   Master   …   Process Process Process Process Process Process 80  MB 80  MB 80  MB 80  MB 80  MB 80  MB
  • 10. resque-­‐pool •  hgps://github.com/nevans/resque-­‐pool   Worker   Worker   Worker   Process Process Process Fork Master   Master   Master   Master   Master   Master   Process Process Process Process Process Process Fork Pool   Manager
  • 11. Summary •  Process  forking  in  Ruby   –  For  isola@on   •  Prevent  process  status  corrup@on   •  See  also:  spork-­‐rails   –  For  concurrency   •  Faster  startup   •  Smaller  memory  footprint