concurrency & ruby
DESCRIPTION
Slides for my talk at RubyConf India 2013TRANSCRIPT
![Page 1: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/1.jpg)
CONCURRENCY&
RUBY
RockyJaiswalRubyConfIndia2013
![Page 2: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/2.jpg)
WHYCONCURRENCY?
![Page 3: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/3.jpg)
ABOUTMELearningprogrammingforthelast11years
DidJavaforaround8years
StartedlearningRuby~3yearsback
♥Ruby♥theRubycommunity
AlsolearningsomeCoffeeScriptandScala
http://rockyj.in@whatsuprocky
![Page 4: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/4.jpg)
CONCURRENCY?
Concurrencyiswhentwotaskscanstart,run,andcompleteinoverlappingtimeperiods
Concurrencycanbeimplementedeveninsingleprocessingunitstospeedthingsup
Concurrencyisnon-deterministic
Whereasaparallelprogramisonethatmerelyrunsonmultipleprocessors,withthegoalofhopefullyrunning
fasterthanitwouldonasingleCPU
![Page 5: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/5.jpg)
THREADSVSPROCESSESS
Threadsarelightweightprocessesthatruninthesamememorycontext
RubyhasGreenThreadswhicharemanagedbytheRubyprocess
JRubyhasrealOSthreadthatrunparalleltotheparentthread
![Page 6: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/6.jpg)
THREADSINRUBY
![Page 7: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/7.jpg)
SAMPLEUNICORNSETUP
15Unicorns=15Processes1UnicornProcess~=150MB15Processes~=2GBRAM*
Scalingthismeansmoreprocesses=morememory=moremoney
Also,IfyouareCPUboundyouwanttousenomoreunicornprocessesthanyouhavecores,otherwiseyouoverloadthesystemandslowdownthescheduler.
![Page 8: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/8.jpg)
CONCURRENCYISGOOD
JRuby+Puma/Torquebox
High-Scalabilitywithlessmemory
Resque/Sidekiq
Moreworkersandfasterprocessingwithlessmemory
![Page 9: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/9.jpg)
SOISITALLDOOMANDGLOOM?
No!
MostRailsapplicationsareIOboundWithMRIyouarealwaysthreadsafeMRIisgettingfasterandGCisgettingbetterProcessesmanagementisoptimizedPassengerisusingahybrid-evented+threaded/processarchitecture
![Page 10: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/10.jpg)
THREAD-SAFETYLETMEGIVEYOUADEMO
AppendingtoArrays:
MRIVersionvs
JRubyVersion
DEMO
![Page 11: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/11.jpg)
RUNCODEONMRI&JRUBY
array=[]5.times.mapdoThread.newdo#Init5threads1000.timesdoarray<<nil#Ineachthreadadd1000elementstotheArrayendendend.each(&:join)putsarray.size
![Page 12: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/12.jpg)
EVENAPPENDINGTOARRAYSISNOTTHREADSAFE!
![Page 13: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/13.jpg)
WHATABOUTRAILS
config.threadsafe!
defthreadsafe!@preload_frameworks=true@cache_classes=true@dependency_loading=false@allow_concurrency=trueselfend
![Page 14: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/14.jpg)
JRUBYONRAILS
DEMO
![Page 15: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/15.jpg)
BADCOUNTERCODE
classPagesController<ApplicationController@counter=0class<<selfattr_accessor:counterend#Classicread-modify-writeproblemdefindexcounter=self.class.counter#readsleep(0.1)counter+=1#updatesleep(0.1)self.class.counter=counter#writeusers=User.allputs"-----------"+self.class.counter.to_s+"------------"endend
![Page 16: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/16.jpg)
UGLYSYNCHRONIZEDCODE
classPagesController<ApplicationController@counter=0@semaphore=Mutex.newclass<<selfattr_accessor:counterattr_accessor:semaphoreenddefindex#counter=self.class.counter#readsleep(0.1)self.class.semaphore.synchronize{self.class.counter+=1#update}sleep(0.1)#self.class.counter=counter#writeusers=User.allputs"-----------"+self.class.counter.to_s+"------------"endend
![Page 17: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/17.jpg)
RAILS4ISCONCURRENCYENABLEDBYDEFAULT
![Page 18: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/18.jpg)
CONCURRENCYINTRODUCES
RaceConditionsDeadlocksStarvation
etc.
BUTGIVESYOUSpeed
LessMemoryUsage
![Page 19: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/19.jpg)
SAFECONCURRENCY
Don'tdoit.Ifyoumustdoit,don'tsharedataacrossthreads.Ifyoumustsharedataacrossthreads,don'tsharemutabledata.Ifyoumustsharemutabledataacrossthreads,synchronizeaccesstothatdata.
![Page 20: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/20.jpg)
THREADSAFETYINJRUBY
LOCKS
ATOMICITY
IMMUTABILITY
![Page 21: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/21.jpg)
ATOMICCOUNTER
java_import'java.util.concurrent.atomic.AtomicInteger'
classPagesController<ApplicationController@counter=AtomicInteger.new(1)class<<selfattr_accessor:counterend
defindexsleep(0.1)counter=self.class.counter.getAndIncrement()#updatesleep(0.1)users=User.allputs"-----------------"+counter.to_s+"-----------------"endend
![Page 22: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/22.jpg)
ALLTHISSUCKS!
95%ofsyncronizedcodeisbroken.Theother5%iswrittenbyBrianGoetz.-VenkatSubramaniam
![Page 23: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/23.jpg)
ENTERACTOR
![Page 24: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/24.jpg)
THEACTORMODEL
IntroducedbyCarlHewittin1973Contributionsbyalotofscholarsanduniversities
PopularizedbyErlang,nowinScala
Simpleandhigh-levelabstractionsforconcurrencyandparallelismObjectsareActorseachwiththeirownstatewhichisneversharedCommunicationhappensthroughmessagesVerylightweightevent-drivenprocesses(approximately2.7millionactorsperGBRAM[Akka])
![Page 25: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/25.jpg)
THEACTORMODEL-2
Easiertodealwithhumansthanwiththreads
Likehumans,Actorscommunicateviamessages
Nostatesharing,communicateviaimmutablemessages
![Page 26: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/26.jpg)
IMPLEMENTATIONS
![Page 27: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/27.jpg)
PRODUCERCONSUMERPROBLEM
DemowithJRuby+LocksDemowithJRuby+Celluloid
![Page 28: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/28.jpg)
PRODUCERCONSUMERwithlocks
HTTPS://GIST.GITHUB.COM/ROCKY-JAISWAL/5847810
![Page 29: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/29.jpg)
PRODUCERCONSUMERwithactors
HTTPS://GIST.GITHUB.COM/ROCKY-JAISWAL/5847814
![Page 30: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/30.jpg)
SUMMARY
ConcurrencyistheneedofthehourMRIisthreadsafebydefaultduetoGIL/GVLJRubygivesyourealconcurrency(RBXaswell)WithpowercomesresponsibilityDon'tworry,concurrencycanbeeasyifyoufollowthegroundrulesIfyouwanttowriteconcurrentcodeyourself,useActors
*IdidnotcoverSTM(providedbyClojure)
![Page 31: Concurrency & Ruby](https://reader034.vdocuments.us/reader034/viewer/2022042507/5592329c1a28ab312f8b4731/html5/thumbnails/31.jpg)
THANKYOU!
QUESTIONS
#Alotofthiscontenthasbeentakenfromblogs,wikisandbooks.Idonotclaimitismy
ownandIwholeheartedlythankeveryonewhohelpedmewiththispresentation.