Class variables and Rails ... how global? When initialized? Thread safety?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Class variables and Rails ... how global? When initialized? Thread safety?

Ralph Shnelvar
I'm trying to understand something basic about Rails (and Apache).

This is a follow-up to Matt Jones' answer in https://groups.google.com/forum/#!topic/rubyonrails-talk/y6ktv7rL6IA

I repeat his answer here
I suspect `User.exists?` is going to be the lightest possible option. `User.count == 0` will also work, but I know there are some DBs where that operation can be expensive on large tables.

This is still going to do a query every time it checks, so if that's too much load you could cache the result:

class User < ActiveRecord::Base
  def self.any_users?
    @any_users ||= exists?
  end
end

Then your role-defaulting code can check `self.class.any_users?`, which will only run one query (per server) that returns true.

NOTE NOTE NOTE: the above is not 100% thread-safe. It assumes that there is exactly one user sending requests trying to be the first. If you're worried about somebody racing to sign up for that first account, you'll want to come up with a more secure approach.

--Matt Jones


In
class User < ActiveRecord::Base
 def self.any_users?
   @any_users ||= exists?
 end
end

So at some point in the code, I call
User.any_users;
So Ruby says "Ok, @any_users isn't defined so I'm going to call exists?"  That makes perfect sense.

Let's say I have several people banging on my website.  Do each of them get a completely different copy of Rails?  How many copies of @any_users are there?  Does @any_users get initialized for each user?  What data is common for all
threads? What's different for all threads?

Where can I learn more about this?  I've skimmed https://bearmetal.eu/theden/how-do-i-know-whether-my-rails-app-is-thread-safe-or-not/ but it really isn't helping me understand what is and isn't common between each thread/user/session.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/abe7fdf1-b381-4e3c-aa7b-04733cadd9c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Class variables and Rails ... how global? When initialized? Thread safety?

Frederick Cheung-2

On Friday, July 14, 2017 at 9:40:56 AM UTC+1, Ralph Shnelvar wrote:



In
class User < ActiveRecord::Base
 def self.any_users?
   @any_users ||= exists?
 end
end

So at some point in the code, I call
User.any_users;
So Ruby says "Ok, @any_users isn't defined so I'm going to call exists?"  That makes perfect sense.

Let's say I have several people banging on my website.  Do each of them get a completely different copy of Rails?  How many copies of @any_users are there?  Does @any_users get initialized for each user?  What data is common for all
threads? What's different for all threads?
 
Where can I learn more about this?  I've skimmed <a href="https://bearmetal.eu/theden/how-do-i-know-whether-my-rails-app-is-thread-safe-or-not/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fbearmetal.eu%2Ftheden%2Fhow-do-i-know-whether-my-rails-app-is-thread-safe-or-not%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHL2cXWrMlZN9-OQCtsPDWsQz1_mA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fbearmetal.eu%2Ftheden%2Fhow-do-i-know-whether-my-rails-app-is-thread-safe-or-not%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHL2cXWrMlZN9-OQCtsPDWsQz1_mA&#39;;return true;">https://bearmetal.eu/theden/how-do-i-know-whether-my-rails-app-is-thread-safe-or-not/ but it really isn't helping me understand what is and isn't common between each thread/user/session.

It depends on how you serve your rails app. For some setups you have many processes, each process handles a request at a time. Each process holds a completely separate copy of your rails application. Others use one process with many threads. This is one process, so there is only one copy of your rails app in memory. Some setups are a hybrid (many processes, each of which serving multiple concurrent requests).

As for the scope of you data, first off there isn't really such a thing as session scope. global / class variables are shared across all threads. If the data is an instance variable of a User, then it's specific to that user (note though that in the code you post @any_users is a instance variable of the User class (not an individual user), so it is shared.

Fred

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/452fecf7-ebbb-49d6-9228-eadcedf0ca43%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...