Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you mean Ruby on Rails is not thread safe?

I was just reading up on ROR (haven't dived into it yet), and I hear that it isn't thread safe. Obviously, this doesn't mean that more than one person can't access your site at one time, so what exactly does it mean? Where do threads come into play in ROR? Do they just mean the request handling?

like image 277
ryeguy Avatar asked Feb 03 '09 03:02

ryeguy


2 Answers

Your information is out of date. It is thread safe as of 2.2.2

Keep in mind Ruby MRI 1.8.x, the most widely used implementation of Ruby uses Green Threads, so with 1.8.x if you create 100 threads they all run on the same CPU. Therefore when hosting Rails websites using MRI, you probably want as many instances of Ruby running as you have CPUS. Stuff like passenger takes care of this for you.

This used to be a big problem for JRuby, because JRuby has Native threads, and juggling processes seems superfluous. Anyway, its sorted out now.

On an aside, Iron Ruby, the .Net Ruby interpreter runs native threads.

Note: Ruby 1.9.1 uses native threads, but there is still a global interpreter lock in place.

like image 192
Sam Saffron Avatar answered Sep 30 '22 17:09

Sam Saffron


Basically what it's saying is that you can't have multiple copies of rails running in the same process under different threads because it is possible for some of the resources to leak between the threads unintentionally causing weird behavior such as objects seemingly changing/disappearing at random times.

Additionally, it could also be the case that classes aren't designed with any synchronization built into them, making it hard to put parts of rails into threads and have other parts be shared among threads.

like image 26
Stephen Caldwell Avatar answered Sep 30 '22 16:09

Stephen Caldwell