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

Ruby Theater

Ruby Theater

the Actor model for concurrent applications

Transcript

  1. Ruby Theater The Actor model for Concurrent Applications Friday, March

    22, 13
  2. starring MARCOS MATOS @ marcosccm Friday, March 22, 13

  3. developer at THOUGHTWORKS Friday, March 22, 13

  4. Concurrency Friday, March 22, 13

  5. Doing multiple things simultaneously Friday, March 22, 13

  6. concurrency parallelism Friday, March 22, 13

  7. How do you deal with it? Friday, March 22, 13

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

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

    application server Friday, March 22, 13
  10. That mindset can lead to slow or unscalable apps Friday,

    March 22, 13
  11. Whatever, I can just spin more Dynos Friday, March 22,

    13
  12. Why we are still treating concurrency as a secondary concern?

    Friday, March 22, 13
  13. Concurrency remembers us of Threads Friday, March 22, 13

  14. No sane person likes Threads Friday, March 22, 13

  15. Racing Conditions! Deadlocks! Starvation! Friday, March 22, 13

  16. 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
  17. Threads are evil incarnate! Friday, March 22, 13

  18. But merely starting a Thread you not leave you in

    a deadlock hell Friday, March 22, 13
  19. things start to break when you have... Friday, March 22,

    13
  20. Mutable Shared State Friday, March 22, 13

  21. Mutable shared state is the root of all evil (together

    with money, premature optimizations, and semicolons) Friday, March 22, 13
  22. We can deal with concurrency issues by controlling mutability Friday,

    March 22, 13
  23. There are many techniques for that Friday, March 22, 13

  24. STM Atomic Operations Pure Functions Channels Friday, March 22, 13

  25. and also... Friday, March 22, 13

  26. Actors Friday, March 22, 13

  27. Think of concurrent Objects Friday, March 22, 13

  28. All communication is done with async immutable messages Friday, March

    22, 13
  29. No shared state! Friday, March 22, 13

  30. Actor Actor Actor Text message State message message Friday, March

    22, 13
  31. messages async Actor mailbox mutable state Friday, March 22, 13

  32. Each actor acts as a single threaded program, but they

    exist concurrently Friday, March 22, 13
  33. Actors and OO, a long story... Friday, March 22, 13

  34. 1973 - Actors 1972 - SmallTalk Friday, March 22, 13

  35. “OOP means only messaging, local retention and protection and hiding

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

    of state processing” Me Friday, March 22, 13
  37. Actors OO before anyone heard of that! Friday, March 22,

    13
  38. Popular in functional languages, like Erlang and Scala Friday, March

    22, 13
  39. what about Ruby? Friday, March 22, 13

  40. Ruby has a somewhat shaky relationship with concurrency Friday, March

    22, 13
  41. “Ruby does not scale! Parallelism is impossible! NodeJS is so

    much faster” The Internet Friday, March 22, 13
  42. Well, not really! It’s complicated Friday, March 22, 13

  43. Recent emphasis on concurrency, specially by alternative implementations Friday, March

    22, 13
  44. many different Actors implementations for Ruby Friday, March 22, 13

  45. Celluloid Actors.rb Girl-Friday Friday, March 22, 13

  46. Celluloid Friday, March 22, 13

  47. Concurrency + Object Orientation Friday, March 22, 13

  48. class MyActor include Celluloid def hard_work sleep 100 “job done!”

    end end Friday, March 22, 13
  49. “Wait a sec! It’s just a normal looking class!” Friday,

    March 22, 13
  50. Give me my Mailbox! Friday, March 22, 13

  51. The secret is the use of Proxies and Futures Friday,

    March 22, 13
  52. 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
  53. Normal Call another Proxy mailbox Actor mailbox Friday, March 22,

    13
  54. actor.async.hard_work (instantaneous) => nil # Proxy fires the message immediately

    # and returns nil Async Call Friday, March 22, 13
  55. Async Call another Proxy mailbox Actor mailbox Nil Friday, March

    22, 13
  56. 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
  57. Calls with Future another Actor mailbox Proxy Future Friday, March

    22, 13
  58. Everything is underlined by Ruby Fibers and Threads Friday, March

    22, 13
  59. What if an actor fails? Friday, March 22, 13

  60. Fault tolerance model inspired by Erlang Friday, March 22, 13

  61. Turn it off and on again! Friday, March 22, 13

  62. Linking Supervisor Supervisor Groups Friday, March 22, 13

  63. 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
  64. 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
  65. Very cool this Actor thing, but does it makes any

    difference? Friday, March 22, 13
  66. 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
  67. concluding Friday, March 22, 13

  68. Concurrency is hard Friday, March 22, 13

  69. but ignoring it won’t make it go away Friday, March

    22, 13
  70. There are many available models and solutions Friday, March 22,

    13
  71. Ruby is lagging behind on that front Friday, March 22,

    13
  72. Look out of the Box! Friday, March 22, 13

  73. The End Friday, March 22, 13