Successfully reported this slideshow.

Actors and Threads

20,781 views

Published on

Safer Concurrency for Ruby

Rubyconf 2011

Published in: Technology, Education
  • CHENNAI GST ROAD - ON ROAD PROPERTY - WITHSTOOD THE ONSLAUGHT. FOR SALE - MAIN ROAD PROPERTY - IN CHENNAI, TAMIL NADU - ENTRY FROM NH-45. Prime Vacant Land 5.8 Grounds (13940 sq.ft.) in Singaperumal Koil, Chennai, India on Main GST Road with direct entry from GST Road. Mahindra World City is 1.2 Kms. on one side and Ford Motor Co. is 3.2 Kms. on the other side. Plot with direct entrance from Wide National Highway NH-45. Frontage Width is 46 feet, Rear Width is 56 feet and length is 286 feet. Companies like BMW, Nissan-Renault, Daimler, Enfield, Nokia, Siemens, Hyundai, Ford are in close proximity to this place. The Property has a Security Room with 3-Phase Power Supply and has a Compound Wall of about 11 feet on all sides with a 15 feet gate in the front. Since the Land is located amidst various International Companies, it will be ideally suited for Offices, IT/ITES/BPO Companies, Residential Apartments, etc. Very Ideally suited for Investment Purposes, Immediate Construction of Residential Apartments, Show Rooms, Departmental Stores, Hospitals, Logistics, etc. Appreciation Guaranteed on Investment. In case of interest, please contact:- Mr. K.Aravamudan, Mob:- 0 – 94440 12056. e.mail : hiraytech@yahoo.co.in VERY IMPORTANT NOTE:- The above Site is not affected by the heavy Rains and Thunder Storms that lashed Chennai just recently.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

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)• https://github.com/tarcieri/celluloid• 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• http://github.com/mperham/girl_friday• 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.comhttp://blog.carbonfive.com PS We’re hiring!

×