Ruby Concurrency Realities

100 %
0 %
Information about Ruby Concurrency Realities

Published on November 10, 2011

Author: subelsky



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

Ruby ConcurrencySlides & links posted to

Goal = Efficiency

At the Expense of

What Is Inefficient? CPU Idling While You

What Is Inefficient? Not Using All of Your

Types of Ruby•Processes•Threads•Actors•Fibers•Events

Completely Hadoop

Completely Hadoop

Forked Processes Ruby subprocess

Forked Processes Ruby subprocess

forkoff gem

Many Ways to Startsystem, exec, IO.popen,

Complex Fork Spork, Unicorn,

Copy-on-Write Rubinius, Enterprise

Copy-on-Write Rubinius, Enterprise

Process Concurrency•Simple•Easy to debug

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

Thread Concurrency Concurrency within a

Thread Concurrency Concurrency within a

Concurrent vs Parallel

Concurrent vs Parallel

Simple Thread Example

Complex Thread Mongrel

Green Threads•VM-managed•Lightweight•1 per core•Blocking I/O blocks all threads

Native Threads•Scheduled by OS•Do not block other threads during I/O•Can run on multiple cores*

*Global Interpreter Lock•Only allows one thread to run at a time•Ruby C code not threadsafe•Released during I/O

Released During I/O

Rubies without GIL

Thread Safety

Thread Safety

Thread Safety ruby thread.rb = 200000 jruby thread.rb = 1067198

Thread Safety

Thread Safety Queue, SizedQueue,

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 *

Thread Cons•Increased complexity (vs processes)•Notoriously hard to debug•Potential for deadlocks and race conditions Text

Actor Concurrency Message passing vs.

Actor Concurrency Message passing vs.

Actor Concurrency “Threads that don’t

Actor Concurrency girl_friday

Where You Can Get•JRuby and Rubinius Actor API•celluloid gem Text

Fiber Concurrency Manually scheduled

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

Simple Fiber Example

Complex Fiber ExampleGoliath, em-synchrony,

Complex Fiber ExampleGoliath, em-synchrony,

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

Fiber Cons•Limited to one CPU core•Nonintuitive•Syntax can get confusing•Need to use fiber-aware libraries for I/O Text

Event Concurrency Single-thread w/

Event Concurrency Single-thread w/


EventMachine Pros•Performs well under heavy network I/O•Zero overhead•Can still use threads via EM.defer

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)

General Advice: MRI Threads/Actors Threads/Actors Events Threads/Actors Forks/Processes Fibers (synchrony)

General Advice: JRuby/ Threads/Actors Threads/Actors Threads/Actors Events

Links on Questions? @subelsky

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Mike Subelsky: Ruby Concurrency Realities

At B'more On Rails I recently presented a new talk I've been refining called "Ruby Concurrency Realities". The slides and research links are below (I also ...
Read more

The Global Interpreter Lock and Ruby / Python concurrency ...

Matt Aimonetti describes how and why the reference implementations of Ruby & Python use a Global Interpreter Lock (GIL or GVML).
Read more

On Ruby: Ruby Concurrency with Actors

Ruby Concurrency with Actors. Tony Arcieri ... In reality the important part about Actors is that they cannot mutate shared state simultaneously.
Read more

Concurrent Programming and Threads in Ruby - a reading list

Concurrent Programming and Threads in Ruby ... though in reality they’re a very well researched and understood concept. ... Ruby, Concurrency and You ...
Read more

Ruby Concurrency and Parallelism: A Practical Primer ...

Ruby Concurrency and Parallelism: ... That would indeed be nice if it were true, but the reality is that threads are not free and so, ...
Read more

Ruby Concurrency and Parallelism: A Practical Tutorial

Thorough and practical intro to concurrency and parallelism in Ruby. Read about forking, multithreading, the Global Interpreter Lock (GIL), and more.
Read more

Ruby 1.9.x Concurrency - Ruby Forum

... threading still isn't the way to get real concurrency on Ruby 1.9 ... raise ArgumentError unless @reality.responds ...
Read more

Mike Subelsky: November 2011

Ruby Concurrency Realities. At B'more On Rails I recently presented a new talk I've been refining called "Ruby Concurrency Realities". The slides and ...
Read more

Pragmatic Concurrency With Ruby - Dotan Nahum

Pragmatic Concurrency With Ruby. I’m coming from a parallel computation, distributed systems background by education, ...
Read more