5. TorqueBox
•The power of JBoss with the expressiveness of Ruby
•Rails, Sinatra, Rack and non-web applications running
on top of JBoss AS and JRuby
•Messaging, Asynchronous Tasks, Scheduled Jobs, and
Services built-in
6. Rails
“Ruby on Rails® is an open-source web framework
that’s optimized for programmer happiness and
sustainable productivity. It lets you write beautiful code
by favoring convention over configuration.” - http://
rubyonrails.org
11. Twitter Streaming API Client
•Connect to Twitter’s streaming API and find all tweets
containing “beer”
•Needs to run as a daemon, outside of the web
request / response cycle
12. TorqueBox Services
•Long-running, non-web daemons that share the
runtime environment and deployment lifecycle of your
app
•A class with initialize( Hash ), start(), and stop()
methods
13. Twitter Streaming API Client
class BeerService
def initialize(options)
@client = StreamingTwitter.new(options["username"],
options["password"])
end
def start
Thread.new { find_beer }
end
def stop
@client.disconnect
end
end
14. Twitter Streaming API Client
class BeerService
def find_beer
@client.search('beer') do |tweet|
Beer.create_from_json(tweet)
end
end
end
26. We’re Done, Right?
No way - it’s beer and Twitter! This will be wildly
popular with social alcoholics and we need to plan
accordingly - we need to scale!
32. Add More Servers
As each TorqueBox instance boots it will find the other
TorqueBox and mod_cluster instances and add itself to
the cluster.
33. Sessions
•By default Rails stores all session data in cookies
•Production Rails applications usually use a database or
memcached for session storage
•TorqueBox provides its own session storage
35. TorqueBox Sessions
•Server-side sessions without hitting the database or
requiring an external process (memcached)
•Uses JBoss AS session replication under the hood
36. Are We Scaled Yet?
We’ve taken care of the load balancer and session
replication, but what about our Twitter Streaming API
client? How will it scale?
37. Twitter Streaming API Client
•By default our service will start on every node in the
cluster
•Sometimes that’s what you want, but not in this case;
we only want one copy of each beer tweet to be saved.
38. HASingleton Services
•TorqueBox supports highly-available singleton
services
•The service will only run on one node in the cluster
•If that node goes down, another node will start the
service
40. Twitter Streaming API Client
•Now our service will only run on one node, but what
happens if tweets come in from Twitter faster than that
one node can process them?
41. Backgroundable
•Convert any method call into an asynchronous call
•Uses HornetQ under the hood and calls are load-
balanced across the cluster
43. Asynchronous Tasks
class BeerTask < TorqueBox::Messaging::Task
def create_from_json(payload)
Beer.create_from_json(payload[:tweet])
end
end
44. Asynchronous Tasks
class BeerService
def find_beer
@client.search('beer') do |tweet|
BeerTask.async(:create_from_json, :tweet => tweet)
end
end
end
45. Twitter Streaming API Client
All nodes in our cluster will now process incoming
tweets even though only a single node maintains a http
connection to Twitter.
46. Now We’re Scaled, Right?
Not quite - we still have that pesky job that cleans stale
beer to deal with.
47. Clean Stale Beer
•By default, scheduled jobs will run on all nodes in a
cluster.
•We only want our beer cleaning job to run on a single
node.
50. Caching
•Rails ships with support for multiple cache stores (file-
based, in-memory, memcached)
•None allow multiple nodes to share a common cache
without setting up an external process
52. TorqueBoxStore
module BeerTweets
class Application < Rails::Application
config.cache_store :torque_box_store(:mode => :distributed,
:sync => false)
end
end
53. Are We Scaled?
As we add additional nodes to the cluster they increase
our web request throughput, background job
throughput, and cache capacity. We’re scaled!
simple Rails controller w/ an accompanying view (not shown) to render the HTML\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
normal load balancers just forward requests to the backend server\nwith mod_cluster, TorqueBox and JBoss AS can send load and other information to the load balancer\n
\n
When using multicast discovery - there are some other tricks for networks that don&#x2019;t support multicast\n
\n
\n
Java and Rails applications can share session data\nAS session replication is battle-proven, very configurable\n
\n
\n
\n
\n
\n
\n
\n
If backgroundable is too magic for you, we also support explicit asynchronous tasks\n