title: How safe is the thread local in Rails?
body: <p>In our multi tenant Rails 4.2 app, we would like to define a thread local <code>current_token</code> for <code>User</code> model and intend to use it through out the whole app. Here is the how the <code>current_token</code> is defined:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    def self.current_token=(token)
      Thread.current['current_token'] = token
    end
  
    def self.current_token
      Thread.current['current_token']
    end
  end
  </code></pre>
  
  <p>The token is retrieved with <code>User.current_token</code>. It is vital to keep <code>User.current_token</code> 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 <code>puma</code>. 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). </p>