Deployment de Rails
         Fabio Akita
     www.akitaonrails.com
Deployment de Rails
+7000 pessoas no WWR
+7000 pessoas no WWR



      +3 anos
+7000 pessoas no WWR



      +3 anos



 +8M hits no Google
“Rails Escala?”
“Sim, Rails Escala!”
Rails 1.0 em 2005
Rails 1.0 em 2005

    MySQL
Rails 1.0 em 2005

      MySQL

LightTPD + Fast CGI
Database/Storage
Database/Storage

  “Rails Stack”
Database/Storage

      “Rails Stack”

HTTP Server/Load Balancer
Database
MySQL


            PostgreSQL


Oracle/SQL Server/Informix/Sybase ...
MySQL


            PostgreSQL


Oracle/SQL Server/Informix/Sybase ...


           MEMCACHED
Rails App   Database
Rails App   Database
MEMCACHED



Rails App



             Database
MEMCACHED



Rails App



            Database          Slave
class Post < ActiveRecord::Base
end
class Post < ActiveRecord::Base
  acts_as_cached
  after_destroy :expire_cache
end
http://errtheblog.com/posts/57-kickin-ass-w-cachefu
HTTP Servers
Maduro
Apache 2.x
             Mais “pesado”
Maduro
Apache 2.x
             Mais “pesado”


Nginx 0.5       Rápido
Maduro
 Apache 2.x
                Mais “pesado”


 Nginx 0.5         Rápido


                  Rápido
Litespeed 3.x
                 Comercial
FastCGI   Instável
FastCGI   Instável


 SAPI     Rápido
FastCGI       Instável


    SAPI        Rápido

    HTTP
                Estável
Proxy Reverso
FastCGI          Instável


    SAPI           Rápido

    HTTP
                   Estável
Proxy Reverso

  mod_rails     recém-chegado
HTTP
         “Rails Stack”
Server
FCGI


HTTP
                “Rails Stack”
Server
FCGI


HTTP
         SAPI   “Rails Stack”
Server
FCGI


HTTP
         SAPI   “Rails Stack”
Server



         HTTP
“Rails Stack”


         FCGI


HTTP
         SAPI   “Rails Stack”
Server



         HTTP


                “Rails Stack”
Rails Stack
“Rails Stack”
“Rails Stack”




Ruby VM (MRI)
“Rails Stack”


 RubyGems


Ruby VM (MRI)
Ruby on Rails


        “Rails Stack”


       RubyGems


     Ruby VM (MRI)
Ruby on Rails

                 Mongrel
        “Rails Stack”


       RubyGems


     Ruby VM (MRI)
Rails, ActiveSupport, ActiveRecord, ActionPack
Rails, ActiveSupport, ActiveRecord, ActionPack


  ActionMailer, ActiveResource, ActionWS
Rails, ActiveSupport, ActiveRecord, ActionPack


  ActionMailer, ActiveResource, ActionWS


       Capistrano, Net-SSH, Net-SFTP
Rails, ActiveSupport, ActiveRecord, ActionPack


  ActionMailer, ActiveResource, ActionWS


       Capistrano, Net-SSH, Net-SFTP


          Mongrel, Mongrel Cluster
Rails, ActiveSupport, ActiveRecord, ActionPack


  ActionMailer, ActiveResource, ActionWS


       Capistrano, Net-SSH, Net-SFTP


          Mongrel, Mongrel Cluster


     Rmagick, Mini Magick, Image Science
Rails, ActiveSupport, ActiveRecord, ActionPack


  ActionMailer, ActiveResource, ActionWS


       Capistrano, Net-SSH, Net-SFTP


          Mongrel, Mongrel Cluster


     Rmagick, Mini Magick, Image Science


               Rspec, ZenTest
Mongrel   Multi-threaded
Mongrel   Multi-threaded




 Rails    Non-concurrent
Mongrel
Ruby on Rails


  Mongrel
Outras Gems


Ruby on Rails


  Mongrel
Sua Applicação


Outras Gems


Ruby on Rails


   Mongrel
“Rails Stack”
Mongrel Cluster
Mongrel Cluster

Load Balancer
Monit


God
objetos

Ruby VM
objetos



objetos

Ruby VM
GC
     objetos



     objetos

     Ruby VM
objetos
objetos

Ruby VM
Memória
     Livre
não desalocada




   objetos
   objetos

  Ruby VM
Monit



Rails App
Rails App   Monit
Monit



Rails App
Load
Balancer
Rails + Mongrel



           Rails + Mongrel



           Rails + Mongrel
 Load
Balancer
           Rails + Mongrel



           Rails + Mongrel



           Rails + Mongrel
Capistrano
capify .


 cap setup


cap deploy


cap rollback
capify .
set :application, "mey_aplicativo"
set :repository, "git://server.com/projeto.git"
set :deploy_to, "/var/www/#{application}"

                    cap setup
default_run_options[:pty] = true
set :repository, "git@github.com:user/rails-app.git"
set :scm, "git"
set :scm_passphrase, "p00p"
set :user, "deployer"
                   cap deploy
set :branch, "origin/master"
set :deploy_via, :remote_cache
set :git_shallow_clone, 1
set :git_enable_submodules, 1

                  cap rollback
capify .


 cap setup


cap deploy


cap rollback
capify .


 cap setup


cap deploy


cap rollback
capify .


 cap setup


cap deploy


cap rollback
Desktop
SCM
 (Git)




Desktop
SCM
 (Git)




Desktop
SCM
 (Git)




Desktop
SCM
                Staging
 (Git)
          Cap




Desktop
SCM
                Staging
 (Git)
          Cap




Desktop
SCM
                Staging
 (Git)
          Cap




Desktop
SCM
                Staging
 (Git)
          Cap




Desktop     Production
          Cap
SCM
                Staging
 (Git)
          Cap




Desktop     Production
          Cap
SCM
                Staging
 (Git)
          Cap




Desktop     Production
          Cap
SCM
Desktop
          (Git)
SCM
           Desktop
                                      (Git)



  Production     Production     Production      Production
Cap            Cap            Cap             Cap




  Production     Production     Production      Production
Cap            Cap            Cap             Cap
http://capify.org/
Quantos Mongrels?
Apache Bench
  • Blog Demo
  • Post resource
  • (1 query, 20 rows)
Apache Bench
Apache Bench

ab -n 1000 http://127.0.0.1:3000/posts
Apache Bench

ab -n 1000 http://127.0.0.1:3000/posts


              1 conexão
           1000 requisições
Apache Bench

ab -n 5000 -c 10 http://127.0.0.1:3000/posts
Apache Bench

ab -n 5000 -c 10 http://127.0.0.1:3000/posts


                10 conexões
              5000 requisições
Nginx - 3 Mongrels
Nginx - 3 Mongrels

req/s (mean)



    1000
  requests
 1 conexão

    5000
  requests
     10
 conexões
Nginx - 3 Mongrels

req/s (mean) Hello World



    1000
  requests     414.56
 1 conexão

    5000
  requests
               701.88
     10
 conexões
Nginx - 3 Mongrels
                            Posts
req/s (mean) Hello World
                           (puro)


    1000
  requests     414.56      24.44
 1 conexão

    5000
  requests
               701.88      39.31
     10
 conexões
Nginx - 3 Mongrels
                                     Posts
                            Posts
req/s (mean) Hello World            (page
                           (puro)
                                    cache)

    1000
  requests     414.56      24.44    1587.31
 1 conexão

    5000
  requests
               701.88      39.31    2220.24
     10
 conexões
Nginx - 3 Mongrels
                                     Posts     Posts
                            Posts
req/s (mean) Hello World            (page     (action
                           (puro)
                                    cache)    cache)

    1000
  requests     414.56      24.44    1587.31   106.29
 1 conexão

    5000
  requests
               701.88      39.31    2220.24   96.81
     10
 conexões
Memória
Memória

 1 Mongrel,
                ~30 Mb
1 Rails vazio
Memória

 1 Mongrel,
                ~30 Mb
1 Rails vazio

1 Mongrel,
                ~70Mb
1 Mephisto
http://www.scribd.com/doc/97641/
   JHoffmanRailsConfMay2007
Estratégias
Rails Cache (Page/Action/Fragment)
Rails Cache (Page/Action/Fragment)



         MEMCACHED
Rails Cache (Page/Action/Fragment)



         MEMCACHED


     Asynchronous Processes
  (BackgrounDRb, cronjobs, etc)
class PostsController < Application
  def action
    # código lento
  end
end

# http://www.site.com/posts
class PostsController < Application
  def action           PERIGO!
    # código lento
  end
end

# http://www.site.com/posts
class PostsController < Application
  def action
    MiddleMan.new_worker(
      :class => :heavy)
  end
end

class Heavy <
BackgrounDRb::Worker::RailsBase
  def do_work(options)
    # código lento
  end
end
Tempo de Execução




Rails Request
Tempo de Execução




   Rails Request



CACHE
Tempo de Execução




        Rails Request
Tempo de Execução




        Rails Request



                    Async
Tempo de Execução




Rails Request




   Outros
920 pageviews/dia

  ~30 req/s pico

  USD 370/mês
Deployment comum
Cap
Nginx




        Cap
MySQL
Nginx




        Cap
MySQL
Nginx
              Memcached


        Cap
MySQL
Nginx
                    Memcached


        Cap   Git
Desafios
Performance


Green Threads x Native Thread + GIL


       Memory Management


           C Extensions


          Interoperability
Performance


Green Threads x Native Thread + GIL


       Memory Management


           C Extensions


          Interoperability
Performance


Green Threads x Native Thread + GIL


       Memory Management


           C Extensions


          Interoperability
Performance


Green Threads x Native Thread + GIL


       Memory Management


           C Extensions


          Interoperability
Performance


Green Threads x Native Thread + GIL


       Memory Management


           C Extensions


          Interoperability
Instalação
Instalação
aptitude install apache2 apache2­mpm­prefork 
       ruby1.8 dev apache2­prefork­dev


        gem install passenger


./passenger­install­apache2­module


          cd /var/www ; rails .
Instalação
aptitude install apache2 apache2­mpm­prefork 
       ruby1.8 dev apache2­prefork­dev


        gem install passenger


./passenger­install­apache2­module


          cd /var/www ; rails .
Instalação
aptitude install apache2 apache2­mpm­prefork 
       ruby1.8 dev apache2­prefork­dev


        gem install passenger


./passenger­install­apache2­module


          cd /var/www ; rails .
Instalação
aptitude install apache2 apache2­mpm­prefork 
       ruby1.8 dev apache2­prefork­dev


        gem install passenger


./passenger­install­apache2­module


          cd /var/www ; rails .
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/
  passenger-1.0.1/ext/apache2/mod_passenger.so
RailsSpawnServer /opt/local/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/
  passenger-spawn-server
RailsRuby /opt/local/bin/ruby

<Directory "/Users/akitaonrails/rails/blog/public">
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /Users/akitaonrails/rails/blog/public
</VirtualHost>
Benchmarks
Mongrel   Thin   Passenger
Benchmarks
Mongrel   Thin   Passenger
Obrigado!


www.akitaonrails.com

Deployment de Rails