Ruby Concurrency Realities

2,299
-1

Published on

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

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,299
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
2
Embeds 0
No embeds

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
  • Ruby Concurrency Realities

    1. 1. Ruby ConcurrencySlides & links posted to
    2. 2. Goal = Efficiency
    3. 3. At the Expense of
    4. 4. What Is Inefficient? CPU Idling While You
    5. 5. What Is Inefficient? Not Using All of Your
    6. 6. Types of Ruby•Processes•Threads•Actors•Fibers•Events
    7. 7. Completely Hadoop
    8. 8. Completely Hadoop
    9. 9. Forked Processes http://tomayko.com/writings/unicorn-is-unix Ruby subprocess
    10. 10. Forked Processes http://tomayko.com/writings/unicorn-is-unix Ruby subprocess
    11. 11. forkoff gem
    12. 12. Many Ways to Startsystem, exec, IO.popen,
    13. 13. Complex Fork Spork, Unicorn,
    14. 14. Copy-on-Write Rubinius, Enterprise
    15. 15. Copy-on-Write Rubinius, Enterprise
    16. 16. Process Concurrency•Simple•Easy to debug
    17. 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. 18. Thread Concurrency http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/ Concurrency within a
    19. 19. Thread Concurrency http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/ Concurrency within a
    20. 20. Concurrent vs Parallel
    21. 21. Concurrent vs Parallel
    22. 22. Simple Thread Example
    23. 23. Complex Thread Mongrel
    24. 24. Green Threads•VM-managed•Lightweight•1 per core•Blocking I/O blocks all threads
    25. 25. Native Threads•Scheduled by OS•Do not block other threads during I/O•Can run on multiple cores*
    26. 26. *Global Interpreter Lock•Only allows one thread to run at a time•Ruby C code not threadsafe•Released during I/O
    27. 27. Released During I/O http://yehudakatz.com/2010/08/14/threads-in-ruby-enough-already/
    28. 28. Rubies without GIL http://www.engineyard.com/blog/2011/ruby-concurrency-and-you/
    29. 29. Thread Safety http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/
    30. 30. Thread Safety http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/
    31. 31. Thread Safety ruby thread.rb = 200000 jruby thread.rb = 1067198 http://blog.carbonfive.com/2011/10/11/a-modern-guide-to-threads/
    32. 32. Thread Safety
    33. 33. Thread Safety Queue, SizedQueue,
    34. 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. 35. Thread Cons•Increased complexity (vs processes)•Notoriously hard to debug•Potential for deadlocks and race conditions Text
    36. 36. Actor Concurrency Message passing vs.
    37. 37. Actor Concurrency Message passing vs.
    38. 38. Actor Concurrency “Threads that don’t
    39. 39. Actor Concurrency http://mperham.github.com/girl_friday/ girl_friday
    40. 40. Where You Can Get•JRuby and Rubinius Actor API•celluloid gem Text
    41. 41. Fiber Concurrency Manually scheduled
    42. 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. 43. Simple Fiber Example http://paulbarry.com/articles/2010/04/01/fibers-in-ruby-1-9
    44. 44. Complex Fiber ExampleGoliath, em-synchrony,
    45. 45. Complex Fiber ExampleGoliath, em-synchrony,
    46. 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. 47. Fiber Cons•Limited to one CPU core•Nonintuitive•Syntax can get confusing•Need to use fiber-aware libraries for I/O Text
    48. 48. Event Concurrency Single-thread w/
    49. 49. Event Concurrency Single-thread w/
    50. 50. EM::WebSocket
    51. 51. EventMachine Pros•Performs well under heavy network I/O•Zero overhead•Can still use threads via EM.defer
    52. 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. 53. General Advice: MRI Threads/Actors Threads/Actors Events Threads/Actors Forks/Processes Fibers (synchrony)
    54. 54. General Advice: JRuby/ Threads/Actors Threads/Actors Threads/Actors Events
    55. 55. Links on subelsky.com Questions? @subelsky

    ×