ABOUT MELearning programming for the last 11 yearsDid Java for around 8 yearsStarted learning Ruby ~3 years back♥ Ruby♥ the Ruby communityAlso learning some CoffeeScript and Scalahttp://rockyj.in@whatsuprocky
CONCURRENCY?Concurrency is when two tasks can start, run, andcomplete in overlapping time periodsConcurrency can be implemented even in singleprocessing units to speed things upConcurrency is non-deterministicWhereas a parallel program is one that merely runs onmultiple processors, with the goal of hopefully runningfaster than it would on a single CPU
THREADS VS PROCESSESSThreads are light weight processes that run in the samememory contextRuby has Green Threads which are managed by the RubyprocessJRuby has real OS thread that run parallel to the parentthread
SAMPLE UNICORN SETUP15 Unicorns = 15 Processes1 Unicorn Process ~= 150 MB15 Processes ~= 2 GB RAM*Scaling this means more processes = more memory =more moneyAlso, If you are CPU bound you want to use no moreunicorn processes than you have cores, otherwise youoverload the system and slow down the scheduler.
CONCURRENCY IS GOODJRuby + Puma / TorqueboxHigh-Scalability with less memoryResque / SidekiqMore workers and faster processing with less memory
SO IS IT ALL DOOM AND GLOOM?No!Most Rails applications are IO boundWith MRI you are always thread safeMRI is getting faster and GC is getting betterProcesses management is optimizedPassenger is using a hybrid - evented + threaded /process architecture
THREAD-SAFETYLET ME GIVE YOU A DEMOAppending to Arrays:MRI VersionvsJRuby VersionDEMO
RUN CODE ON MRI & JRUBYarray = 5.times.map do Thread.new do #Init 5 threads 1000.times do array << nil #In each thread add 1000 elements to the Ar end endend.each(&:join)puts array.size
SAFE CONCURRENCYDont do it.If you must do it, dont share data acrossthreads.If you must share data across threads, dontshare mutable data.If you must share mutable data across threads,synchronize access to that data.
THREAD SAFETY IN JRUBYLOCKSATOMICITYIMMUTABILITY
THE ACTOR MODELIntroduced by Carl Hewitt in 1973Contributions by a lot of scholars and universitiesPopularized by Erlang, now in ScalaSimple and high-level abstractions for concurrency andparallelismObjects are Actors each with their own state which is neversharedCommunication happens through messagesVery lightweight event-driven processes (approximately 2.7million actors per GB RAM [Akka])
THE ACTOR MODEL -2Easier to deal with humans than with threadsLike humans, Actors communicate via messagesNo state sharing, communicate via immutable messages
SUMMARYConcurrency is the need of the hourMRI is thread safe by default due to GIL / GVLJRuby gives you real concurrency (RBX as well)With power comes responsibilityDont worry, concurrency can be easy if you follow theground rulesIf you want to write concurrent code yourself, useActors* I did not cover STM (provided by Clojure)
THANK YOU!QUESTIONS#A lot of this content has been taken from blogs, wikis and books. I do not claim it is myown and I wholeheartedly thank everyone who helped me with this presentation.