title: Understanding race_condition_ttl in Rails
body: <p>I am trying to understand the <code>race_condition_ttl</code> directive in Rails when using <code>Rails.cache.fetch</code>.</p>
  
  <p>I have a controller action that looks like this:</p>
  
  <pre><code>def foo
    @foo = Rails.cache.fetch("foo-testing", expires_in: 30.seconds, race_condition_ttl: 60.seconds) do
        Time.now.to_s
    end
    @foo # this gets used in a view down the line...
  end
  </code></pre>
  
  <p>Based on what I'm reading in the Rails docs, this value should expire after 30 seconds, but the stale value is allowed to be served for another 60 seconds. However, I can't figure out how to reproduce conditions that will show me this behavior working. Here is how I'm trying to test it.</p>
  
  <pre><code>100.times.map do 
    t = Thread.new { RestClient.get("http://myenvironment/foo") }
    t 
  end.map {|t| t.join.value }.uniq
  </code></pre>
  
  <p>I have my Rails app running on a VM behind a standard nginx/unicorn setup. I am trying to spawn 100 threads hitting the site simultaneously to simulate the "dog pile effect". However, when I run my test code, all the threads report the same value back. What I would expect to see is that one thread gets the fresh value, while <em>at least</em> one other thread gets served some stale content.</p>
  
  <p>Any pointers are welcome! Thanks so much.</p>