title: Is Redis too slow for Rails production I18n?
body: <p>I recently switch from the default Simple I18n backend to a Redis backend for my I18n. I did it so make it easier for us to handle the translations, but I've found that there was a substantial performance hit on every page. </p>
  
  <p>I've run some Benchmarks with Rails 3.2 and Redis 2.6.4 installed on my MBP to demonstrate. I'm using <a href="https://github.com/pietern/hiredis-rb" rel="noreferrer">hiredis-rb</a> as my client.</p>
  
  <p>It's a pretty clear difference when exercising the two different backends. With the simple backend there is a short delay on the first call - I assume the translations are being loaded into memory - and then great performance after that:</p>
  
  <pre><code>pry(main)&gt; Benchmark.realtime { 500.times { I18n.t 'shared.slogan' } }
  =&gt; 0.143246
  pry(main)&gt; Benchmark.realtime { 500.times { I18n.t 'shared.slogan' } }
  =&gt; 0.00415
  pry(main)&gt; Benchmark.realtime { 500.times { I18n.t 'shared.slogan' } }
  =&gt; 0.004153
  pry(main)&gt; Benchmark.realtime { 500.times { I18n.t 'shared.slogan' } }
  =&gt; 0.004056
  </code></pre>
  
  <p>The Redis backend is consistently slow:</p>
  
  <pre><code>pry(main)&gt; Benchmark.realtime { 500.times { I18n.t 'shared.slogan' } }
  =&gt; 0.122448
  pry(main)&gt; Benchmark.realtime { 500.times { I18n.t 'shared.slogan' } }
  =&gt; 0.263564
  pry(main)&gt; Benchmark.realtime { 500.times { I18n.t 'shared.slogan' } }
  =&gt; 0.232637
  pry(main)&gt; Benchmark.realtime { 500.times { I18n.t 'shared.slogan' } }
  =&gt; 0.122304
  </code></pre>
  
  <p>It makes absolute sense to me why this is slow for I18n... I'm queueing up dozens of I18n calls throughout my code base. If I could batch them together up front I'd be in good shape:</p>
  
  <pre><code>pry(main)&gt; keys = $redis.keys[0..500]
  pry(main)&gt; Benchmark.realtime { $redis.mget keys }
  =&gt; 0.04264
  </code></pre>
  
  <p>But I don't really see a clean way to do this with any of the existing I18n backends. Has anybody out there tackled this problem?</p>
  
  <h1>EDIT</h1>
  
  <p>I took Chris Heald's suggestion and created a backend with memoization a simple cache bust. The gist is up here:</p>
  
  <p><a href="https://gist.github.com/wheeyls/5650947" rel="noreferrer">https://gist.github.com/wheeyls/5650947</a></p>
  
  <p>I'll try this out for a few days and then turn it into a gem.</p>
  
  <h1>UPDATE</h1>
  
  <p>My solution is available as a gem now:</p>
  
  <p><a href="https://github.com/wheeyls/cached_key_value_store" rel="noreferrer">https://github.com/wheeyls/cached_key_value_store</a></p>
  
  <p>And I also blogged about this problem:</p>
  
  <p><a href="http://about.g2crowd.com/faster-i18nredis-on-rails/" rel="noreferrer">http://about.g2crowd.com/faster-i18nredis-on-rails/</a></p>