concurrency patterns in ruby

25
Paradigms of Concurrency

Upload: thoughtworks

Post on 01-Nov-2014

2.677 views

Category:

Technology


0 download

DESCRIPTION

Ruby is a wonderful language for rapid development, it is easy to learn, we have wonderful frameworks, an active and dynamic community. But when it comes to concurrency Ruby is plagued with problems, controversies and urban legends. A lot of people would know about green threads in Ruby, GIL and its inherent limitations. But that it only one part of the big picture. Ruby offers much more than threads to helps us with concurrency.This presentation explores other options of writing highly concurrent applications in Ruby and options available in it. We cover topics ranging from Actor like message passing concurrency in Ruby, dataflow concurrency of how we can coordinate across different threads, Event driven methods, coroutine based concurrency which never blocks ;) and finally Software Transactional Memory. We look at lots of code, some serious looking yet colorful performance graphs comparisons, and conditions at which each of these forms are concurrency are effective and ineffective.

TRANSCRIPT

Page 1: Concurrency patterns in Ruby

Paradigms of Concurrency

Page 2: Concurrency patterns in Ruby

Why is this important?

Page 3: Concurrency patterns in Ruby

Or for geeks to relate...

Page 4: Concurrency patterns in Ruby
Page 5: Concurrency patterns in Ruby

Without right abstractions...

Page 6: Concurrency patterns in Ruby

So what do we have in hand?

● Threads – The Java hangover● Fibers – Red headed cousin of threads● Actors – Stylish but effective● Reactor – He will do all the work...● Dataflow – The forgotten one..● STM – Remember ATM● The Nirvana

Page 7: Concurrency patterns in Ruby

A folk definition of insanity is to do the same thing over and over again and to expect the results to be different. By this definition, we in fact require that programmers of multithreaded systems be insane - The Problem with Threads Edward Lee

Page 8: Concurrency patterns in Ruby

Threading in Ruby

Concurrency is a myth Ruby - @igvita

Page 9: Concurrency patterns in Ruby
Page 10: Concurrency patterns in Ruby

Fibers

● New kid in the block from Ruby 1.9● Green threads with cooperatively

scheduler● Can give up CPU control to other fiber

when needed.● Based on Coroutines – Like python

generators but can yield to other coroutines

● Think NeverBlock...

Page 11: Concurrency patterns in Ruby

Nginx vs Apache Death Match

Page 12: Concurrency patterns in Ruby

EventMachine – Reactor Pattern

Page 13: Concurrency patterns in Ruby

EventMachine

● Based on python Twisted● Reactor sits in the middle● IOC pattern – Not many get used to it● Defer long running process● Support for lot of protocols and very

active development● Try my em-couchdb :)

(http://github.com/saivenkat/em-couchdb)

Page 14: Concurrency patterns in Ruby

Actors -Stylishly passing messages

Page 15: Concurrency patterns in Ruby

Actors

● Message passing concurrency● No first class support in Ruby● Revactor is good but we had problems

with it● If Jruby, lots of Java Actor framework

available like Jetlang.● Jruby + Jetlang - http://bit.ly/ahXh6j

Page 16: Concurrency patterns in Ruby

Transactions – We are safe

Page 17: Concurrency patterns in Ruby

Software Transactional Memory

● Similar to database transactions but inmemory (ACIDXX)

● Unfortunately again no first class support

● STM library by MentalGuy - http://bit.ly/c7Mxah

● In Jruby harness clojure's infrastructure – Ruby objects as refs. http://bit.ly/d6vpuP

Page 18: Concurrency patterns in Ruby

Multi Process

Page 19: Concurrency patterns in Ruby

Coordination

Page 20: Concurrency patterns in Ruby

Coordination

● How do we coordinate independent units of execution?

● Dataflow – Forgotten paradigm● Other methods – May be a big heavy

weight suitable when multi processing● Tuplespace, Queues (RabbitMq or

beanstalk)

Page 21: Concurrency patterns in Ruby

Nirvana

Page 22: Concurrency patterns in Ruby

Nirvana

● Polyglotism – Let the languages which excel in concurrency do the job

● Make Ruby talk with Clojure, Erlang, Haskell, node.js...

● Lots of ways. VM level support, external infrastructure.

Page 23: Concurrency patterns in Ruby

Sai Venkatgithub.com/saivenkat

@sai_venkathttp://developer-in-test.blogspot.com

HariKrishnangithub.com/harikrishnan83

@harikrishnan83http://harikrishnan83.wordpress.com

Page 24: Concurrency patterns in Ruby

All images used are attributed to the awesome guys who took them..

Please don't sue us :D

Page 25: Concurrency patterns in Ruby