SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
淺談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	
  

Contenu connexe

Tendances

20140419 oedo rubykaigi04
20140419 oedo rubykaigi0420140419 oedo rubykaigi04
20140419 oedo rubykaigi04
Hiroshi SHIBATA
 
20140425 ruby conftaiwan2014
20140425 ruby conftaiwan201420140425 ruby conftaiwan2014
20140425 ruby conftaiwan2014
Hiroshi SHIBATA
 
Messaging, interoperability and log aggregation - a new framework
Messaging, interoperability and log aggregation - a new frameworkMessaging, interoperability and log aggregation - a new framework
Messaging, interoperability and log aggregation - a new framework
Tomas Doran
 

Tendances (12)

20140419 oedo rubykaigi04
20140419 oedo rubykaigi0420140419 oedo rubykaigi04
20140419 oedo rubykaigi04
 
Distributed Queue System using Gearman
Distributed Queue System using GearmanDistributed Queue System using Gearman
Distributed Queue System using Gearman
 
Queue your work
Queue your workQueue your work
Queue your work
 
Sensu and Sensibility - Puppetconf 2014
Sensu and Sensibility - Puppetconf 2014Sensu and Sensibility - Puppetconf 2014
Sensu and Sensibility - Puppetconf 2014
 
20140425 ruby conftaiwan2014
20140425 ruby conftaiwan201420140425 ruby conftaiwan2014
20140425 ruby conftaiwan2014
 
I know why your Java is slow
I know why your Java is slowI know why your Java is slow
I know why your Java is slow
 
Rebooting a Cloud
Rebooting a CloudRebooting a Cloud
Rebooting a Cloud
 
Dockersh and a brief intro to the docker internals
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
 
ZendCon 2015 - DevOps for Small Teams
ZendCon 2015 - DevOps for Small TeamsZendCon 2015 - DevOps for Small Teams
ZendCon 2015 - DevOps for Small Teams
 
Messaging, interoperability and log aggregation - a new framework
Messaging, interoperability and log aggregation - a new frameworkMessaging, interoperability and log aggregation - a new framework
Messaging, interoperability and log aggregation - a new framework
 
Live migrating a container: pros, cons and gotchas
Live migrating a container: pros, cons and gotchasLive migrating a container: pros, cons and gotchas
Live migrating a container: pros, cons and gotchas
 
AVA - a futuristic test runner
AVA - a futuristic test runnerAVA - a futuristic test runner
AVA - a futuristic test runner
 

Similaire à 淺談Ruby process fork應用

Storm distributed processing
Storm distributed processingStorm distributed processing
Storm distributed processing
ducquoc_vn
 
Parallel batch processing with spring batch slideshare
Parallel batch processing with spring batch   slideshareParallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch slideshare
Morten Andersen-Gott
 
Azug - successfully breeding rabits
Azug - successfully breeding rabitsAzug - successfully breeding rabits
Azug - successfully breeding rabits
Yves Goeleven
 
My site is slow
My site is slowMy site is slow
My site is slow
hernanibf
 
Gearman - Northeast PHP 2012
Gearman - Northeast PHP 2012Gearman - Northeast PHP 2012
Gearman - Northeast PHP 2012
Mike Willbanks
 
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…
Host and Boast: Best Practices for Magento Hosting | Imagine 2013 Technolog…
Atwix
 

Similaire à 淺談Ruby process fork應用 (20)

Storm distributed processing
Storm distributed processingStorm distributed processing
Storm distributed processing
 
Parallel batch processing with spring batch slideshare
Parallel batch processing with spring batch   slideshareParallel batch processing with spring batch   slideshare
Parallel batch processing with spring batch slideshare
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails application
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
 
ruby test-all parallel running
ruby test-all parallel runningruby test-all parallel running
ruby test-all parallel running
 
Alluxio - Scalable Filesystem Metadata Services
Alluxio - Scalable Filesystem Metadata ServicesAlluxio - Scalable Filesystem Metadata Services
Alluxio - Scalable Filesystem Metadata Services
 
Azug - successfully breeding rabits
Azug - successfully breeding rabitsAzug - successfully breeding rabits
Azug - successfully breeding rabits
 
Keeping MongoDB Data Safe
Keeping MongoDB Data SafeKeeping MongoDB Data Safe
Keeping MongoDB Data Safe
 
BTV PHP - Building Fast Websites
BTV PHP - Building Fast WebsitesBTV PHP - Building Fast Websites
BTV PHP - Building Fast Websites
 
"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)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
 
Ship It ! with Ruby/ Rails Ecosystem
Ship It ! with Ruby/ Rails EcosystemShip It ! with Ruby/ Rails Ecosystem
Ship It ! with Ruby/ Rails Ecosystem
 
Machine Learning With H2O vs SparkML
Machine Learning With H2O vs SparkMLMachine Learning With H2O vs SparkML
Machine Learning With H2O vs SparkML
 
My site is slow
My site is slowMy site is slow
My site is slow
 
Gearman - Northeast PHP 2012
Gearman - Northeast PHP 2012Gearman - Northeast PHP 2012
Gearman - Northeast PHP 2012
 
Queick: A Simple Job Queue System for Python
Queick: A Simple Job Queue System for PythonQueick: A Simple Job Queue System for Python
Queick: A Simple Job Queue System for Python
 
My Site is slow - Drupal Camp London 2013
My Site is slow - Drupal Camp London 2013My Site is slow - Drupal Camp London 2013
My Site is slow - Drupal Camp London 2013
 
近未来的並列 LL
近未来的並列 LL近未来的並列 LL
近未来的並列 LL
 
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…
Host and Boast: Best Practices for Magento Hosting | Imagine 2013 Technolog…
 
Hands-on Performance Tuning Lab - Devoxx Poland
Hands-on Performance Tuning Lab - Devoxx PolandHands-on Performance Tuning Lab - Devoxx Poland
Hands-on Performance Tuning Lab - Devoxx Poland
 

Plus de YC Ling (7)

那些函數語言Tutorial沒有教我的事
那些函數語言Tutorial沒有教我的事那些函數語言Tutorial沒有教我的事
那些函數語言Tutorial沒有教我的事
 
Learn Haskell The Easy Way
Learn Haskell The Easy WayLearn Haskell The Easy Way
Learn Haskell The Easy Way
 
[OSDC12]相依性管理 - 以Ruby開發為例
[OSDC12]相依性管理 - 以Ruby開發為例[OSDC12]相依性管理 - 以Ruby開發為例
[OSDC12]相依性管理 - 以Ruby開發為例
 
Lol Programming
Lol ProgrammingLol Programming
Lol Programming
 
Introduction of Reverse Engineering
Introduction of Reverse EngineeringIntroduction of Reverse Engineering
Introduction of Reverse Engineering
 
Lightning Talk: RichRC & Hirb-Unicode
Lightning Talk: RichRC & Hirb-UnicodeLightning Talk: RichRC & Hirb-Unicode
Lightning Talk: RichRC & Hirb-Unicode
 
Learning Ruby with RubyWarrior
Learning Ruby with RubyWarriorLearning Ruby with RubyWarrior
Learning Ruby with RubyWarrior
 

淺談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