How safe is the thread local in Rails?

Viewed 1265

In our multi tenant Rails 4.2 app, we would like to define a thread local current_token for User model and intend to use it through out the whole app. Here is the how the current_token is defined:

class User < ActiveRecord::Base
  def self.current_token=(token)
    Thread.current['current_token'] = token
  end

  def self.current_token
    Thread.current['current_token']
  end
end

The token is retrieved with User.current_token. It is vital to keep User.current_token thread safe and alive throughout the lifespan of a user session. It is pointed out that the thread local may be cleared up by some web server such as puma. We would like to get feedback from the community about how sound is the thread local in real production. What kind of problem we should be aware of (and possible solution if there is one).

2 Answers
Related