Ruby Concurrency Realities

  • 2,110 views
Uploaded on

Mike Subelsky presents the basics of Ruby's various concurrency models

Mike Subelsky presents the basics of Ruby's various concurrency models

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,110
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
20
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Ruby ConcurrencySlides & links posted to
  • 2. Goal = Efficiency
  • 3. At the Expense of
  • 4. What Is Inefficient? CPU Idling While You
  • 5. What Is Inefficient? Not Using All of Your
  • 6. Types of Ruby•Processes•Threads•Actors•Fibers•Events
  • 7. Completely Hadoop
  • 8. Completely Hadoop
  • 9. Forked Processes http://tomayko.com/writings/unicorn-is-unix Ruby subprocess
  • 10. Forked Processes http://tomayko.com/writings/unicorn-is-unix Ruby subprocess
  • 11. forkoff gem
  • 12. Many Ways to Startsystem, exec, IO.popen,
  • 13. Complex Fork Spork, Unicorn,
  • 14. Copy-on-Write Rubinius, Enterprise
  • 15. Copy-on-Write Rubinius, Enterprise
  • 16. Process Concurrency•Simple•Easy to debug
  • 17. Process Concurrency•Requires you to start/monitor each process•Requires some type of RPC (dRb, Resque, etc)•Can’t share resources (like connection pools)•Memory inefficient w/o copy-on-write•One process per core
  • 18. Thread Concurrency http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/ Concurrency within a
  • 19. Thread Concurrency http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/ Concurrency within a
  • 20. Concurrent vs Parallel
  • 21. Concurrent vs Parallel
  • 22. Simple Thread Example
  • 23. Complex Thread Mongrel
  • 24. Green Threads•VM-managed•Lightweight•1 per core•Blocking I/O blocks all threads
  • 25. Native Threads•Scheduled by OS•Do not block other threads during I/O•Can run on multiple cores*
  • 26. *Global Interpreter Lock•Only allows one thread to run at a time•Ruby C code not threadsafe•Released during I/O
  • 27. Released During I/O http://yehudakatz.com/2010/08/14/threads-in-ruby-enough-already/
  • 28. Rubies without GIL http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/
  • 29. Thread Safety http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/
  • 30. Thread Safety http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/
  • 31. Thread Safety ruby thread.rb = 200000 jruby thread.rb = 1067198 http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/
  • 32. Thread Safety
  • 33. Thread Safety Queue, SizedQueue,
  • 34. Thread Pros•More intuitive than fibers/events•Sans GIL: most efficient/performant* technique•Single process to monitor•Shared resources, no RPC•Fine-grained control of thread lifecycle *http://teddziuba.com/2011/10/straight-talk-on-event-loops.html
  • 35. Thread Cons•Increased complexity (vs processes)•Notoriously hard to debug•Potential for deadlocks and race conditions Text
  • 36. Actor Concurrency Message passing vs.
  • 37. Actor Concurrency Message passing vs.
  • 38. Actor Concurrency “Threads that don’t
  • 39. Actor Concurrency http://mperham.github.com/girl_friday/ girl_friday
  • 40. Where You Can Get•JRuby and Rubinius Actor API•celluloid gem Text
  • 41. Fiber Concurrency Manually scheduled
  • 42. Fiber Concurrency•Code blocks that can be paused/resumed•Lighter than threads, cannot be preempted•Scheduled by programmer•Can share data without mutexes Manually scheduled
  • 43. Simple Fiber Example http://paulbarry.com/articles/2010/04/01/fibers-in-ruby-1-9
  • 44. Complex Fiber ExampleGoliath, em-synchrony,
  • 45. Complex Fiber ExampleGoliath, em-synchrony,
  • 46. Fiber Pros•Much lighter memory use than threads•Faster to start vs threads•Can share data without mutex•Can simplify asynchronous APIs (em-synchrony) Text
  • 47. Fiber Cons•Limited to one CPU core•Nonintuitive•Syntax can get confusing•Need to use fiber-aware libraries for I/O Text
  • 48. Event Concurrency Single-thread w/
  • 49. Event Concurrency Single-thread w/
  • 50. EM::WebSocket
  • 51. EventMachine Pros•Performs well under heavy network I/O•Zero overhead•Can still use threads via EM.defer
  • 52. EventMachine Cons•Inversion of control can be confusing•Hard to test•Nested callbacks can lead to spaghetti code•Threads can still beat events (all apps willeventually become CPU bound)
  • 53. General Advice: MRI Threads/Actors Threads/Actors Events Threads/Actors Forks/Processes Fibers (synchrony)
  • 54. General Advice: JRuby/ Threads/Actors Threads/Actors Threads/Actors Events
  • 55. Links on subelsky.com Questions? @subelsky