3

Short Version

Are class variables thread-safe for the duration of a controller action?

Long Version

I'm writing a Rails 3 app that implements multi-tenancy using a single database schema with tenant_id columns in each table to identify to whom the data belongs.

Naturally I need a way to scope all queries that are run on the database to ensure that access to data is properly isolated per-tenant.

Several people have suggested achieving this by always accessing the database through the tenant instance, e.g.:

current_tenant.associate_collection.where(...)

etc.

However, since multi-tenancy is an architectural concern (not a business domain concern), I'd prefer to keep this as transparent to the model as possible by setting it up the scope globally. It's also much DRYer.

I've found I can do this using the multitenant gem and around_filters:

class ApplicationController
  around_filter do
    Multitenant.with_tenant current_tenant
      yield
    end
  end
end

This manipulates the default_scope of the appropriate models so that all data access is automatically scoped to the current tenant.

This works great in development, but I have concerns about thread safety. Multitenant.with_tenant preserves the current_tenant reference in a class variable.

The question is can I rely on the integrity of that variable while the controller action is on the stack? or could it be corrupted by another request?

Also, what’s a good source on general concurrency issues in Rails?

1 Answer 1

1

It's not a matter of duration, and there isn't anything special about a controller that makes it an atomic (uninterruptible) sequence of code. Thread safety is about understanding locks, synchronization, and the thread scheduler.

I don't think your question is even Rails specific in this case. In a general sense, thread safety is achieved through locking data while a write is happening, and unlocking it when the write is finished. It's very similar database locking mechanisms, if you're familiar with those.

For a good reference that focuses on Ruby, I would go here: http://www.rubycentral.com/pickaxe/tut_threads.html

For more info about the concept the thread safety (threads run at the OS level, after all, so it's more about understanding how thread safety works in any program), I would go here: http://en.wikipedia.org/wiki/Thread_safety

2
  • 1
    Thanks normalocity, appreciated ~ but the question is indeed specific to Rails. I'm asking if the class variables in question are synchronized while the controller action is running. In any case I guess you can create the lock yourself, but it would be useful to know how Rails handles this by default. Oct 7, 2011 at 6:00
  • Perhaps I was a bit too broad, I'll agree. Controllers are not synchronized automatically. You would have to put a lock around it yourself.
    – jefflunt
    Oct 7, 2011 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.