TorqueBox
Enterprise-Grade Ruby Application Server
Bob McWhirter




                                 Asheville.rb
       ...
Bob McWhirter

•   Started in C++
•   Moved to Java with JDK 1.1.8
•   Began Ruby and Rails in 2005
•   Lived in Asheville...
Open-Source




        3
Currently
                Linux
                 +
                Java
                 +
                Bob
           ...
== Ruby?
   5
Actual R&D

I get to actually research things,
and develop new stuff. Honest-
to-goodness technology R&D
investment.

    ...
TorqueBox
...in a nutshell




                   7
What is it?


A merging of the Ruby
language with the Java
Virtual Machine.

           8
Why bother?

As Ruby matures, it’s quickly
being used to solve larger, more
difficult problems.
Problems we’ve solved in t...
Such as...?

•   Scalability
•   Efficient & asynchronous messaging
•   Scheduled jobs
•   Telephony



                  ...
Scalability
...because “Rails doesn’t scale”




               11
Web Scalability




         12
mongrel-style

               Ruby

              mongrel




         13
mongrel-style
                      Ruby

                     mongrel



                      Ruby

                    ...
mongrel-style
                      Ruby

                     mongrel



                      Ruby

                    ...
passenger style
                         Conservative Spawning



                                     Rails


           ...
passenger style
                                    Smart Spawning



                                                Work...
TorqueBox style

       TorqueBox

          http



         queues       MyApp   Ruby   Rails



        scheduler




 ...
Clustering
Multiplicity rocks.




                19
Concerns


When clustering, we care
about sessions and load
balancing.

            20
Sessions

With multiple application
servers, sessions must be
shared, or session affinity
must be used.

             21
Session Sharing

Sharing sessions requires
using a central store, such as
memcached or ActiveRecord
session stores.

     ...
Session Sharing
    Worker




    Worker

                  memcached

    Worker




    Worker




             23
Session Sharing

But what if the central session
store is unavailable, or
overwhelmed?
Single point of failure!

         ...
Session Sharing
    Worker




    Worker

                  memcached

    Worker




    Worker




             25
Session Affinity

Session affinity is making sure
the same back-end server
handles all of Todd’s requests.


             ...
Session Affinity
                    Worker

                             Todd’s
                    Worker
              ...
Session Affinity

But what if the back-end server
handling Todd’s requests
becomes unavailable?


                28
Session Affinity
                    Worker

                             Todd’s
                    Worker
              ...
Use Both!

Use session affinity when you
can, but allow workers to
replicate session information
when necessary.

        ...
Use Both!

                   Worker Cache




                   Worker Cache

       Balancer

                   Worker...
Spanning Hosts
                   Host


                    Worker Cache



                   Host


                   ...
Balancer in the cluster

Using mod_cluster for
Apache httpd, the balancer
participates in the cluster
view.

            33
mod_cluster

As server nodes join and
leave the cluster, the
balancer (mod_cluster) is
aware of these changes.

          ...
mod_proxy_ajp

The Apache JServ Protocol
(AJP) is an efficient pipeline for
shuffling HTTP requests to a Java
application-...
Oh yeah...

• Multiple apps per server or cluster.
• Virtual-host support baked in.
• Context-path support.
• Full Rack su...
And bundles


  myapp.rails
  myapp.rack

        37
And bundles
Single ZIP file bundle of an app.
Easily move specific artifact
between staging and production.
Required for c...
Rails scales!
...but there’s more than just port 80.




                  39
Task Queues
Simply install Erlang, set up
RabbitMQ, use an AMQP gem...




               40
Or just use TorqueBox
...and just write a Ruby class.




               41
app/queues/**_queue.rb


• A class per queue.
• A method per task handled by the
  queue.
• A simple API to toss stuff aro...
my_first_queue.rb
class MyFirstQueue
  include TorqueBox::Queues::Base

  def task_one(payload={})
    o = Thing.find_by_i...
my_first_queue.rb
class MyFirstQueue
  include TorqueBox::Queues::Base

  def task_one(payload={})
    o = Thing.find_by_i...
my_first_queue.rb
class MyFirstQueue
  include TorqueBox::Queues::Base

  def task_one(payload={})
    o = Thing.find_by_i...
my_first_queue.rb
class MyFirstQueue
  include TorqueBox::Queues::Base

  def task_one(payload={})
    o = Thing.find_by_i...
my_first_queue.rb
class MyFirstQueue
  include TorqueBox::Queues::Base

  def task_one(payload={})
    o = Thing.find_by_i...
Enqueue

TorqueBox::Queues.enqueue(
  ‘MyFirstQueue’,
  :task_one,
  { :thing_id=>42 }
)

            48
Enqueue

TorqueBox::Queues.enqueue(
  ‘MyFirstQueue’,
  :task_one,
  { :thing_id=>42 }
)

            49
Enqueue

TorqueBox::Queues.enqueue(
  ‘MyFirstQueue’,
  :task_one,
  { :thing_id=>42 }
)

            50
Enqueue

TorqueBox::Queues.enqueue(
  ‘MyFirstQueue’,
  :task_one,
  { :thing_id=>42 }
)

            51
Enqueue


Use from your controllers,
ActiveRecord observers, or
other queues.

            52
Enqueue
class User < ActiveRecord::Base
  has_attached_file :avatar, ...
  after_save do |user|
    TorqueBox::Queues.enqu...
Enqueue
class User < ActiveRecord::Base
  has_attached_file :avatar, ...
  after_save do |user|
    TorqueBox::Queues.enqu...
Enqueue
class User < ActiveRecord::Base
  has_attached_file :avatar, ...
  after_save do |user|
    TorqueBox::Queues.enqu...
Enqueue
class User < ActiveRecord::Base
  has_attached_file :avatar, ...
  after_save do |user|
    TorqueBox::Queues.enqu...
Queue Benefits

•   No additional systems or languages to
    setup and maintain.
•   Clustering/HA available (including d...
Scheduled Jobs
Just write a script, and a crontab,
and deploy it with your app. Don’t
forget to undeploy and update it
als...
Or just use TorqueBox
...and just write a Ruby class.




               59
app/jobs/**.rb


• A class per queue.
• A method called run()
• Configure it via jobs.yml

               60
my_first_job.rb
class MyFirstJob
  include TorqueBox::Jobs::Base

  def run()
    subscriptions = Subscription.find(:all)
...
my_first_job.rb
class MyFirstJob
  include TorqueBox::Jobs::Base

  def run()
    subscriptions = Subscription.find(:all)
...
my_first_job.rb
class MyFirstJob
  include TorqueBox::Jobs::Base

  def run()
    subscriptions = Subscription.find(:all)
...
my_first_job.rb
class MyFirstJob
  include TorqueBox::Jobs::Base

  def run()
    subscriptions = Subscription.find(:all)
...
config/jobs.yml


subscription.mailer:
  description: send out newsletters
  job: MyFirstJob
  cron: 20 7 1 * * ?




    ...
config/jobs.yml


subscription.mailer:
  description: send out newsletters
  job: MyFirstJob
  cron: 20 7 1 * * ?




    ...
config/jobs.yml


subscription.mailer:
  description: send out newsletters
  job: MyFirstJob
  cron: 20 7 1 * * ?




    ...
config/jobs.yml


subscription.mailer:
  description: send out newsletters
  job: MyFirstJob
  cron: 20 7 1 * * ?




    ...
config/jobs.yml


subscription.mailer:
  description: send out newsletters
  job: MyFirstJob
  cron: 20 7 1 * * ?




    ...
config/jobs.yml



20 7 1 * * ?
sec         hour         mon
      min          d.o.m.   d.o.y

              70
config/jobs.yml

   20 7 1 * * ?
   Every day,
   at 1:07:20 am
        71
Scheduler Benefits
•   Uses existing Ruby interpreter when jobs
    fire. No waiting for Ruby/Rails to load.
•   No extern...
Telephony
Just... um... something... with
Asterisk?


               73
Or just use TorqueBox
...and just write a Ruby class.




               74
Initiate Calls

By doing SIP magic, your
application can initiate
calls, or connect multiple
parties.

             75
Press 2 to STFU

Using the Media Server,
automated messages,
interactive voice response
(IVR) and other advanced
systems c...
OMGWTFBBQPONIES!


Have your app (for instance, upon
successful job completion) fire off an
SMS.

                  77
Telephony Benefits



•   It’s just freakin’ cool.


                78
Manageability
Okay, sorta speculative, forward-
looking dreaming, but...



               79
Manageable

Everything ultimately maps to a
managed object within the Java
application-server.
We have the JOPR management...
But not yet


But we haven’t exposed
everything in a super nice
fashion. Yet.

             81
All-in-One
Everything’s in the tin.
Everything works together.


              82
More information...
http://torquebox.org/
http://github.com/torquebox
http://twitter.com/torquebox


               83
Thanks
                               +
                              Q&A

Creative Commons BY-SA 3.0
                    ...
Upcoming SlideShare
Loading in...5
×

TorqueBox for Rubyists

1,924

Published on

Presentation of TorqueBox for Rubyists at Asheville.rb

Published in: Technology

TorqueBox for Rubyists

  1. 1. TorqueBox Enterprise-Grade Ruby Application Server Bob McWhirter Asheville.rb Asheville, North Carolina 2 December 2009 Creative Commons BY-SA 3.0
  2. 2. Bob McWhirter • Started in C++ • Moved to Java with JDK 1.1.8 • Began Ruby and Rails in 2005 • Lived in Asheville for 4 years • Did some open-source along the way 2
  3. 3. Open-Source 3
  4. 4. Currently Linux + Java + Bob 4
  5. 5. == Ruby? 5
  6. 6. Actual R&D I get to actually research things, and develop new stuff. Honest- to-goodness technology R&D investment. 6
  7. 7. TorqueBox ...in a nutshell 7
  8. 8. What is it? A merging of the Ruby language with the Java Virtual Machine. 8
  9. 9. Why bother? As Ruby matures, it’s quickly being used to solve larger, more difficult problems. Problems we’ve solved in the enterprise with Java already. 9
  10. 10. Such as...? • Scalability • Efficient & asynchronous messaging • Scheduled jobs • Telephony 10
  11. 11. Scalability ...because “Rails doesn’t scale” 11
  12. 12. Web Scalability 12
  13. 13. mongrel-style Ruby mongrel 13
  14. 14. mongrel-style Ruby mongrel Ruby mongrel httpd Ruby mongrel Ruby mongrel 14
  15. 15. mongrel-style Ruby mongrel Ruby mongrel httpd Ruby mongrel Ruby mongrel 15
  16. 16. passenger style Conservative Spawning Rails Rails httpd Passenger Ruby Rails Rails 16
  17. 17. passenger style Smart Spawning Worker Worker httpd Passenger Ruby Rails Worker Worker 17
  18. 18. TorqueBox style TorqueBox http queues MyApp Ruby Rails scheduler 18
  19. 19. Clustering Multiplicity rocks. 19
  20. 20. Concerns When clustering, we care about sessions and load balancing. 20
  21. 21. Sessions With multiple application servers, sessions must be shared, or session affinity must be used. 21
  22. 22. Session Sharing Sharing sessions requires using a central store, such as memcached or ActiveRecord session stores. 22
  23. 23. Session Sharing Worker Worker memcached Worker Worker 23
  24. 24. Session Sharing But what if the central session store is unavailable, or overwhelmed? Single point of failure! 24
  25. 25. Session Sharing Worker Worker memcached Worker Worker 25
  26. 26. Session Affinity Session affinity is making sure the same back-end server handles all of Todd’s requests. 26
  27. 27. Session Affinity Worker Todd’s Worker server Todd Balancer Worker Worker 27
  28. 28. Session Affinity But what if the back-end server handling Todd’s requests becomes unavailable? 28
  29. 29. Session Affinity Worker Todd’s Worker server Todd Balancer Worker Worker 29
  30. 30. Use Both! Use session affinity when you can, but allow workers to replicate session information when necessary. 30
  31. 31. Use Both! Worker Cache Worker Cache Balancer Worker Cache Worker Cache 31
  32. 32. Spanning Hosts Host Worker Cache Host Worker Cache Balancer Host Worker Cache Host Worker Cache 32
  33. 33. Balancer in the cluster Using mod_cluster for Apache httpd, the balancer participates in the cluster view. 33
  34. 34. mod_cluster As server nodes join and leave the cluster, the balancer (mod_cluster) is aware of these changes. 34
  35. 35. mod_proxy_ajp The Apache JServ Protocol (AJP) is an efficient pipeline for shuffling HTTP requests to a Java application-server for handling. 35
  36. 36. Oh yeah... • Multiple apps per server or cluster. • Virtual-host support baked in. • Context-path support. • Full Rack support (bare, Sinatra, etc). • Full Java-EE stack. 36
  37. 37. And bundles myapp.rails myapp.rack 37
  38. 38. And bundles Single ZIP file bundle of an app. Easily move specific artifact between staging and production. Required for cluster auto-farming deployment. 38
  39. 39. Rails scales! ...but there’s more than just port 80. 39
  40. 40. Task Queues Simply install Erlang, set up RabbitMQ, use an AMQP gem... 40
  41. 41. Or just use TorqueBox ...and just write a Ruby class. 41
  42. 42. app/queues/**_queue.rb • A class per queue. • A method per task handled by the queue. • A simple API to toss stuff around. 42
  43. 43. my_first_queue.rb class MyFirstQueue include TorqueBox::Queues::Base def task_one(payload={}) o = Thing.find_by_id( payload[:thing_id] ) o.frob! o.save! end end 43
  44. 44. my_first_queue.rb class MyFirstQueue include TorqueBox::Queues::Base def task_one(payload={}) o = Thing.find_by_id( payload[:thing_id] ) o.frob! o.save! end end 44
  45. 45. my_first_queue.rb class MyFirstQueue include TorqueBox::Queues::Base def task_one(payload={}) o = Thing.find_by_id( payload[:thing_id] ) o.frob! o.save! end end 45
  46. 46. my_first_queue.rb class MyFirstQueue include TorqueBox::Queues::Base def task_one(payload={}) o = Thing.find_by_id( payload[:thing_id] ) o.frob! o.save! end end 46
  47. 47. my_first_queue.rb class MyFirstQueue include TorqueBox::Queues::Base def task_one(payload={}) o = Thing.find_by_id( payload[:thing_id] ) o.frob! o.save! end end 47
  48. 48. Enqueue TorqueBox::Queues.enqueue( ‘MyFirstQueue’, :task_one, { :thing_id=>42 } ) 48
  49. 49. Enqueue TorqueBox::Queues.enqueue( ‘MyFirstQueue’, :task_one, { :thing_id=>42 } ) 49
  50. 50. Enqueue TorqueBox::Queues.enqueue( ‘MyFirstQueue’, :task_one, { :thing_id=>42 } ) 50
  51. 51. Enqueue TorqueBox::Queues.enqueue( ‘MyFirstQueue’, :task_one, { :thing_id=>42 } ) 51
  52. 52. Enqueue Use from your controllers, ActiveRecord observers, or other queues. 52
  53. 53. Enqueue class User < ActiveRecord::Base has_attached_file :avatar, ... after_save do |user| TorqueBox::Queues.enqueue( :image_processor_queue, :process_user_avatar, user.id ) if user.avatar_needs_processing? end end 53
  54. 54. Enqueue class User < ActiveRecord::Base has_attached_file :avatar, ... after_save do |user| TorqueBox::Queues.enqueue( :image_processor_queue, :process_user_avatar, user.id ) if user.avatar_needs_processing? end end 54
  55. 55. Enqueue class User < ActiveRecord::Base has_attached_file :avatar, ... after_save do |user| TorqueBox::Queues.enqueue( :image_processor_queue, :process_user_avatar, user.id ) if user.avatar_needs_processing? end end 55
  56. 56. Enqueue class User < ActiveRecord::Base has_attached_file :avatar, ... after_save do |user| TorqueBox::Queues.enqueue( :image_processor_queue, :process_user_avatar, user.id ) if user.avatar_needs_processing? end end 56
  57. 57. Queue Benefits • No additional systems or languages to setup and maintain. • Clustering/HA available (including durable messages). • No explicit queue configuration required. 57
  58. 58. Scheduled Jobs Just write a script, and a crontab, and deploy it with your app. Don’t forget to undeploy and update it also. And check cron.log for errors. 58
  59. 59. Or just use TorqueBox ...and just write a Ruby class. 59
  60. 60. app/jobs/**.rb • A class per queue. • A method called run() • Configure it via jobs.yml 60
  61. 61. my_first_job.rb class MyFirstJob include TorqueBox::Jobs::Base def run() subscriptions = Subscription.find(:all) subscriptions.each do |e| send_newsletter( e ) end end end 61
  62. 62. my_first_job.rb class MyFirstJob include TorqueBox::Jobs::Base def run() subscriptions = Subscription.find(:all) subscriptions.each do |e| send_newsletter( e ) end end end 62
  63. 63. my_first_job.rb class MyFirstJob include TorqueBox::Jobs::Base def run() subscriptions = Subscription.find(:all) subscriptions.each do |e| send_newsletter( e ) end end end 63
  64. 64. my_first_job.rb class MyFirstJob include TorqueBox::Jobs::Base def run() subscriptions = Subscription.find(:all) subscriptions.each do |e| send_newsletter( e ) end end end 64
  65. 65. config/jobs.yml subscription.mailer: description: send out newsletters job: MyFirstJob cron: 20 7 1 * * ? 65
  66. 66. config/jobs.yml subscription.mailer: description: send out newsletters job: MyFirstJob cron: 20 7 1 * * ? 66
  67. 67. config/jobs.yml subscription.mailer: description: send out newsletters job: MyFirstJob cron: 20 7 1 * * ? 67
  68. 68. config/jobs.yml subscription.mailer: description: send out newsletters job: MyFirstJob cron: 20 7 1 * * ? 68
  69. 69. config/jobs.yml subscription.mailer: description: send out newsletters job: MyFirstJob cron: 20 7 1 * * ? 69
  70. 70. config/jobs.yml 20 7 1 * * ? sec hour mon min d.o.m. d.o.y 70
  71. 71. config/jobs.yml 20 7 1 * * ? Every day, at 1:07:20 am 71
  72. 72. Scheduler Benefits • Uses existing Ruby interpreter when jobs fire. No waiting for Ruby/Rails to load. • No external system (crontab) to maintain. • No additional daemons to monitor (daemonize) • Full Rails environment (AR models, queues, lib/) 72
  73. 73. Telephony Just... um... something... with Asterisk? 73
  74. 74. Or just use TorqueBox ...and just write a Ruby class. 74
  75. 75. Initiate Calls By doing SIP magic, your application can initiate calls, or connect multiple parties. 75
  76. 76. Press 2 to STFU Using the Media Server, automated messages, interactive voice response (IVR) and other advanced systems can be built. 76
  77. 77. OMGWTFBBQPONIES! Have your app (for instance, upon successful job completion) fire off an SMS. 77
  78. 78. Telephony Benefits • It’s just freakin’ cool. 78
  79. 79. Manageability Okay, sorta speculative, forward- looking dreaming, but... 79
  80. 80. Manageable Everything ultimately maps to a managed object within the Java application-server. We have the JOPR management console. 80
  81. 81. But not yet But we haven’t exposed everything in a super nice fashion. Yet. 81
  82. 82. All-in-One Everything’s in the tin. Everything works together. 82
  83. 83. More information... http://torquebox.org/ http://github.com/torquebox http://twitter.com/torquebox 83
  84. 84. Thanks + Q&A Creative Commons BY-SA 3.0 84
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×