Rails Rails cache store 的一个疑问:memcached (或 redis) 比 FileStore 有什么好处?

nowherekai · July 20, 2015 · Last by guanting112 replied at October 27, 2015 · 4057 hits

看了一篇对 Rails cache 各个 store 的对比文章,这篇文章有对不同 store 的 benchmark 对比,在一个 server 的情况下,FileStore 要比 memcached 和 redis 快的。

作为 Rails 的缓存 backend,只有一个 server 的情况下,memcached(或 redis)和 FileStore 相比的优势在哪里,各自适用于什么情况?(Rails 默认是 FileStore,ruby-china 用 memcached)

只有一个 server 的时候用什么缓存都一样. 文件缓存有额外的读写锁。

IO 的怎么可能比内存快。

请问原文中什么地方说 FileStore 比 memcached 和 redis 快?

Filesystems are slow(ish). Accessing the disk will always be slower than accessing RAM. However, it might be faster than accessing a cache over the network (which we'll get to in a minute).

楼主,原文只是说 FileStore 有时候比通过网络访问 Cache 服务要快,本机的话,肯定内存快。

Comparison:
LruRedux::ThreadSafeCache:   337353.5 i/s
ActiveSupport::Cache::MemoryStore:    52808.1 i/s - 6.39x slower
ActiveSupport::Cache::FileStore:    12341.5 i/s - 27.33x slower
ActiveSupport::Cache::DalliStore:     6629.1 i/s - 50.89x slower
ActiveSupport::Cache::RedisStore:     6304.6 i/s - 53.51x slower
ActiveSupport::Cache::DalliStore at pub-memcache-13640.us-east-1-1.2.ec2.garantiadata.com:13640:       26.9 i/s - 12545.27x slower
ActiveSupport::Cache::RedisStore at pub-redis-11469.us-east-1-4.2.ec2.garantiadata.com:       25.8 i/s - 13062.87x slower

@billy @lgn21st @kgen 但是这是这篇博客的结果,单机上 FileStore 甚至比 memcached 和 redis 快,当时也是注意到这点。

文章里面关于每个缓存的优势和劣势都讲解的比较清楚,如果只是单机的话: FileStore: Advantages:

  • Disk space is cheaper than RAM Disadvantages:
  • Not an LRU cache

When should I use ActiveSupport::FileStore? Reach for FileStore if you have low request load (1 or 2 servers) and still need a very large cache (>100MB). Also, don't use it on Heroku.

Memcache and dalli: Disadvantages:

  • Expensive.
  • Cache values are limited to 1MB.

Redis and redis-store: Advantages:

  • Allows different eviction policies beyond LRU
  • Can persist to disk, allowing hot restarts

Disadvantages:

  • Expensive
  • While Redis supports several data types, redis-store only supports Strings

LRURedux: Advantages:

  • Ridiculously fast

Disadvantages:

  • Caches can't be shared across processes or hosts
  • Caches add to your total RAM usage
  • Can't use it as a Rails cache store

其实 Memcache/Redis,对比来看,两个无论是性能还是特性在单机上都差不是太多,甚至有网络访问,需要多机器是也差不多。相比而言 Redis 还有比较好的缓存实效时间的设置和热备份机制。所以这两个来选可以选 Redis。

对比 FileStore 来看,虽然从理论上讲访问硬盘肯定要比访问内存慢,但是文章中的测试结果是,访问文件系统更快,这点我也比较有疑问。不过比较好的地方就是 FileStore 比较廉价,不好的地方就是没有 LRU。

如果楼主有比较大的内容需要缓存并且内存吃紧,可以用 FileStore 缓存来处理,但是记得自己清除缓存就好了。Redis 的话,比较方便的是缓存的生命周期可以设置。

其实我觉得这个 LRURedux 也不错,这变态的性能也让人跃跃欲试。不过就是缺点也挺多,Caches can't be shared across processes or hosts,感觉有点像 MemoryStore。

楼主可以 FileStore 做大文件的缓存,然后同时可以用 Redis-objects 存一些数据,然后同时使用 LRURedux 来做一些小片段的缓存,要注意的是这货和 MemoryStore 一样不能 processes 之间共享。😄

那篇文章作者对 fetch 的benchmark 代码在这里,我摘一部分在下面,结果 5 楼贴了。我自己也跑了一下他的代码,结果类似。对 memcached 和 redis 不是太熟,所以不知道为什么会比 FileStore 慢。

Benchmark.ips do |x|
  file_store = ActiveSupport::Cache::FileStore.new("tmp/cache")
  x.report(file_store.class) { file_store.fetch(rand(KEY_SIZE)) { :value } }

  memory_store = ActiveSupport::Cache::MemoryStore.new
  x.report(memory_store.class) { memory_store.fetch(rand(KEY_SIZE)) { :value } }

  lru_redux = LruRedux::ThreadSafeCache.new(1_000)
  x.report(lru_redux.class) { lru_redux.getset(rand(KEY_SIZE)) { :value } }

  dc = ActiveSupport::Cache::DalliStore.new('localhost:11211')
  dc.clear
  begin
    dc.dalli.alive!
    x.report(dc.class) { dc.fetch(rand(KEY_SIZE)) { :value } }
  rescue Dalli::RingError
    puts "Not running Memcache bench - you must have a Memcache server available"
  end
   ...

我想有一部份在某種情況下 FileStore 會快的原因,除了網路因素,也有可能是來自作業系統介入,有興趣可以瞭解一下 OS 的 File Caching

You need to Sign in before reply, if you don't have an account, please Sign up first.