ruby-on-railscachingruby-on-rails-2

Why Rails.cache is not thread safe?


I know Rails.cache is ActiveSupport::Cache::MemoryStore, and it is not thread safe.

I don't understand, why rails use a thread-unsafe cache as its default? Why not use ActiveSupport::Cache::SynchronizedMemoryStore? In my opinion, in a web site, if a cache is not thread-safe, it almost useless, because the requests are not handled in ONE thread.

Do you use Rails.cache in you webapp? And how do you use it?


Solution

  • The default cache store in Rails is ActiveSupport::Cache::FileStore, not MemoryStore.

    The memory store is of limited use in practice, since it is restricted to a single process, which makes it useless for Rails apps that are deployed using Passenger or a Mongrel cluster where requests are handled in separate processes, not in separate threads.

    For small to medium-sized applications you'll probably do fine with the default file store. If you need to scale beyond that, you should have a look at ActiveSupport::Cache::MemCacheStore.