Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Theater

Ruby Theater

the Actor model for concurrent applications

More Decks by Marcos Castilho da Costa Matos

Other Decks in Programming

Transcript

  1. “My app is super concurrent! I use Unicorn!” (or Thin,

    Rainbows, Puma, Passenger...) Friday, March 22, 13
  2. To most rubyists, concurrency stops at the choosing of an

    application server Friday, March 22, 13
  3. mutex = Mutex.new cv = ConditionVariable.new a = Thread.new {

    mutex.synchronize { sleep 200 cv.wait(mutex) sleep 100 } } b = Thread.new { mutex.synchronize { sleep 200 cv.signal sleep 200 } } a.join; b.join Friday, March 22, 13
  4. But merely starting a Thread you not leave you in

    a deadlock hell Friday, March 22, 13
  5. Mutable shared state is the root of all evil (together

    with money, premature optimizations, and semicolons) Friday, March 22, 13
  6. Each actor acts as a single threaded program, but they

    exist concurrently Friday, March 22, 13
  7. “OOP means only messaging, local retention and protection and hiding

    of state processing” Alan Kay Friday, March 22, 13
  8. “Actors means only messaging, local retention and protection and hiding

    of state processing” Me Friday, March 22, 13
  9. “Ruby does not scale! Parallelism is impossible! NodeJS is so

    much faster” The Internet Friday, March 22, 13
  10. Normal Call actor = MyActor.new => #<Celluloid::Actor(MyActor::0x82)> # new gets

    you a proxy object actor.hard_work (100s later...) => “job done!” # that answers normally to method # calls Friday, March 22, 13
  11. future = actor.future.hard_work (instantaneous) => #<Celluloid::Future:0x45f> # Proxy returns a

    future object future.value (blocking call) => “job done!” # only blocks when value is called Calls with Future Friday, March 22, 13
  12. class Worker include Celluloid def initialize(website) @website = website end

    def work Net::HTTP.get(@website) end end Working with Supervisors Friday, March 22, 13
  13. Worker.supervise_as :wk, ‘www.google.com’ =>#<Celluloid::Actor(Celluloid::Supervisi onGroup:0x80c) ...> # returns a supervisor

    and also boots the # actor worker = Celluloid::Actors[:wk] worker.work # supervisor takes care of restarting # actor in case of exceptions Working with Supervisors Friday, March 22, 13
  14. Very cool this Actor thing, but does it makes any

    difference? Friday, March 22, 13
  15. The Sidekiq Case “we replaced 12-15 EC2 medium instances running

    4 delayed_job processes each with a single EC2 medium instance running 4 sidekiq workers with 25 concurrency each. The final tally is something like $2500 less per month in EC2 costs.” Friday, March 22, 13