Would someone be able to explain how ruby class/object contexts work
from the point of view of a deployed rails application using FASTCGI and
noting there are multiple Ruby processes running (say on dreamhost for
example). Some questions to help clarify what I'm trying to understand:
a) how do incoming URL requests get spread across the multiple Ruby
processes (let's say there are 5 processes)?
The front end webserver balances between the fcgi listeners. On dreamhost this would be apache doing that I believe.
b) What component organising/manages this? (e.g. plugin in apache?)
c) is there any 'stickness' that occurs? i.e. where a request from
person A that goes to ruby process 2 the first time will go to process 2
again for the next person A request (I assume no?)
Not with apache and usually not with fcgi. some front end proxies allow this like pound or nginx. Its called sticky session adn it makes sure a clients request always goes to the same backend.
With rails I don't think this is a good idea. You need to code your rails apps so that any backend can answer a request.
d) is there any sharing of contexts between the ruby processes? i.e.
Can a ruby class variable be "seen" across processes?
No the ruby processes are totally independent. you can share state through the database or via a drb server or memcached.
e) is there any equivalent to the java static class variable (which can
be accessed from any user request coming in which is being hosted within
the same web-container on the same JVM)? I understand there isn't an
equivalent in ruby?
I'm not familiar with Java enough to know what this means exactly. But I don't think rails has what you want here. Can you explain what you want to do with this and then I can help?
f) does the running ruby process handle multiple simultaneous user
requests at the same time? i.e. is it operating in a multi-threaded
No each request to rails gets gaurded with mutex. So each of your rails processes will only serve one request at a time and new requests will queue up waiting their turn. So you need to watch out if your users are dong huge file uploads. If you have three fcgi's and people try to upload 5 huge files at the same time, two of them will queue up and wait until one of the other uploads is done.
g) If so (for last question) is there any scope for collisions when
using class variables (@@variable), or is each user request (i.e. each
HTTP request) totally isolated from every other request at the
ruby/rails application level? (of course the database would be shared).
i.e. could both users/requests share the same class variable somehow??
On each request, your models and controllers are instantiated and go from there. @@classvars can get confusing because they are visible throughout the entire inheritance tree from where they are defined. So you can use these but think it thru carefully if you do as it can get confusing and if you don't do it right it can cause leakages between sessions.
h) what non-database caching raw ruby techniques are there for same
caching a bunch of constants stored in a properties type file - perhaps
put the properties/configuration setting in a ruby file itself as
constants. Is it effectively already "cached" once it is "require"ed
via ruby then holding it in memory? or does ruby re-load that file each
If you put a constant in environment.rb it will only load on bootup and will stay in memory until the procees is killed.
i) what would be simplest / rudimentory way to load/cache a bunch of
properties/configuration parameters for an application? (would it be as
per my suggestion in item h)
I would make a yaml config file and then load the contents of that into a constant in environment.rb. That works great for certain things. Of course you don't want to store a huge amount of data thins way but for config options it works great.
Still a bit confused about ruby/rails contexts
No worries, its a bit difficult to figure out on your own. But once you follow a few guidelines it will become clear.
Thanks in advance