Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Actors and Threads

Safer Concurrency for Ruby

Rubyconf 2011

  • Login to see the comments

Actors and Threads

  1. Actors and Threads Safer Concurrency for Ruby Mike Perham @mperham
  2. Who Am I?• Numerous gems (dalli, memcache-client, rack-fiber_pool, connection_pool, etc)• Scalability/Performance guy• Fibers and EventMachine• Technical Lead at Carbon Five
  3. Ruby History• Fun!• Implementation • Performance • Scalability
  4. What This Isn’t• Threads vs Events vs Processes• Threads won’t make you love Java
  5. Definitions• Concurrency - performing two operations in tandem (e.g. two web requests)• Parallelism - performing two operations literally at the same time (requires two cores) Concurrent Parallel
  6. Concurrency• Processes• Threads
  7. The problem is always communication (true in the real world too!)
  8. Communication• Two fundamental mechanisms • Share • Copy
  9. IPC• Pipes• Sockets• Shared Memory• Files• POSIX 1988
  10. IPC
  11. Threads• Much more efficient than processes• Communication mechanism? • Variables!• POSIX 1995
  12. Threads
  13. Threads
  14. End Goals• Runtime Efficiency isn’t the only goal!• Remember Fun?• Ease of Development is huge
  15. Locks• Hard to get right• Non-deterministic• Don’t scale
  16. Locks
  17. Lock Benchmark# Results: Ruby 1.9.2, GIL means no parallel threading# user system total real# single locked 4.310000 0.010000 4.320000 ( 4.311850)# threaded locked 4.230000 0.080000 4.310000 ( 4.307405)## Results: JRuby 1.6.3, parallel threads mean massive lock contention# user system total real# single locked 4.080000 0.000000 4.080000 ( 4.080000)# threaded locked 17.109000 0.000000 17.109000 ( 17.109000) Context Switching!
  18. Most Important Slide!
  19. What can we do?• Look to other languages • Go • Scala • Erlang
  20. Goroutines• Asynchronous function • You cannot get a handle to it • Send messages via a channel
  21. Goroutines• Really like this model• Maps well to a distributed model• Backed by a pool of threads
  22. Actors• unit of execution• has a mailbox• You just send it a message, e.g. • actor << { :amount => 11.99 }
  23. Actors• Thread- or Fiber-backed• No application-level locks
  24. Actor.rb• MRI/JRuby - no actors• Rubinius comes with actor.rb
  25. Actor.rb
  26. Actor.rb• Not idiomatic• Hard to understand, use correctly
  27. Celluloid• Tony Arcieri (Revactor, Reia)•• OO Actors• Asynchronous method invocation• Mix of Threads and Fibers
  28. Celluloid Example
  29. Celluloid Thoughts• Nice, idiomatic Ruby design• One object == one thread• Needs actor pooling
  30. girl_friday• yours truly•• Background processing pools• Parallel batch operations• More functional than OO• Thread-based
  31. girl_friday example
  32. girl_friday example
  33. What if we do need to share data?
  34. I Love the 80s!
  35. I Love the 00s!
  36. STM• Software Transactional Memory• All mutation via transactions• Provide ACI (not ACID) guarantees• See Clojure (and Cloby!)
  37. Atomic Instructions• Since Pentium (1993)• XCHG - swap registers or register/ memory• CPMXCHG - Compare And Set (‘CAS’)
  38. Atomic!
  39. Atomic Benchmark# Results: JRuby 1.6.3# user system total real# single atomic 2.041000 0.000000 2.041000 ( 2.041000)# threaded atomic 0.978000 0.000000 0.978000 ( 0.979000) 2 cores = 2x speedup!
  40. Atomic Structures• java.util.concurrent • ConcurrentHashMap • ConcurrentLinkedQueue
  41. Conclusion
  42. Concurrency It’s always hard! Modern languages should supporttools and APIs that scale well AND are developer-friendly.
  43. To Do• Need standard Actor API• Concurrent data structures• STM impl for Ruby (via JRuby?) • Multiverse
  44. Further Reading• Kilim - Actors for Java• Disruptor - concurrency without locks• Actors in Scala• Concurrency in Erlang
  45. Thank You! Questions? @mperham http://mikeperham.com PS We’re hiring!