如果 redis 已经是堆栈的一部分,为什么 Memcached 仍然与 Redis 一起使用? [英] If redis is already a part of the stack, why is Memcached still used alongside Redis?

查看:43
本文介绍了如果 redis 已经是堆栈的一部分,为什么 Memcached 仍然与 Redis 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Redis 可以完成 Memcached 提供的所有功能(LRU 缓存、项目到期,现在在 3.x+ 版本中进行集群,目前处于测试阶段)或通过 twemproxy 之类的工具.性能也差不多.此外,Redis 增加了持久性,因此您无需在服务器重启时进行缓存预热.

Redis can do everything that Memcached provides (LRU cache, item expiry, and now clustering in version 3.x+, currently in beta) or by tools like twemproxy. The performance is similar too. Morever, Redis adds persistence due to which you need not do cache warming in case of a server restart.

参考一些比较 Redis 和 Memcache 的旧答案,其中一些支持 Redis 作为 Memcache 的替代品(如果已经存在于堆栈中):

Reference to some old answers which compare Redis and Memcache, some of which favor Redis as replacement of Memcache (if already present in the stack):

与 Redis 相比,memcached 是恐龙吗?

Redis 和 Memcache 还是只是 Redis?

尽管如此,在研究 Instagram、Pinterest、Twitter 等大型网络公司的堆栈时,我发现他们将 Memcached 和 Redis 用于不同的目的,而不是将 Redis 用于主要缓存.主缓存依然是Memcached,基于数据结构的逻辑缓存使用Redis.

In spite of this, on studying stacks of large web scale companies like Instagram, Pinterest, Twitter etc, I found that they use both Memcached and Redis for different purposes, not using Redis for primary caching. The primary cache is still Memcached, and Redis is used for its data structures based logical caching.

截至 2014 年,为什么 memcached 仍然值得将其作为附加组件添加到您的堆栈中,而您已经拥有一个可以完成 memcached 所能做的一切的 Redis 组件?除了现有的 Redis 之外,架构师/工程师倾向于仍然包含 memcached 的优点是什么?

As of 2014, why is memcached still worth the pain to be added as additional component into your stack, when you already have a Redis component which can do everything that memcached can? What are the favorable points that incline the architects/engineers to still include memcached apart from already existing Redis?

对于我们的平台,我们完全放弃了 Memcached,并使用 redis 来满足普通和逻辑缓存要求.高度高性能、灵活且可靠.

For our platforms, we have completely discarded Memcached, and use redis for plain as well as logical caching requirements. Highly performant, flexible and reliable.

一些示例场景:

  • 按特定模式列出所有缓存的键,并读取或删除它们的值.在 redis 中很容易,在 memcached 中不可行(很容易).
  • 在 redis 中存储超过 1mb 的有效负载很容易,但需要在 memcached 中调整slab 大小,这本身就有性能副作用.
  • 当前缓存内容的简单快照
  • Redis 集群与语言驱动程序一起已做好生产准备,因此集群部署也很容易.

推荐答案

我今天将 memcached 视为基于 Redis 的用例的主要原因是您应该能够使用 plain HTML 片段缓存(或类似的应用程序).如果您需要将对象的不同字段存储在不同的 memcached 键中,那么 Redis 散列将具有更高的内存效率,但是当您有大量键 -> simple_string 对时,memcached 应该能够为您提供更多的每个项目兆字节.

The main reason I see today as an use-case for memcached over Redis is the superior memory efficiency you should be able to get with plain HTML fragments caching (or similar applications). If you need to store different fields of your objects in different memcached keys, then Redis hashes are going to be more memory efficient, but when you have a large number of key -> simple_string pairs, memcached should be able to give you more items per megabyte.

其他关于 memcached 的优点:

Other things which are good points about memcached:

  • 这是一段非常简单的代码,所以如果您只需要它提供的功能,我想这是一个合理的替代方案,但我从未在生产中使用过.
  • 它是多线程的,因此如果您需要在单机设置中进行扩展,这是一件好事,您只需要与一个实例进行对话.

我相信随着人们转向智能缓存或当他们试图通过 Redis 数据结构保留缓存数据的结构时,Redis 作为缓存变得越来越有意义.

I believe that Redis as a cache makes more and more sense as people move towards intelligent caching or when they try to preserve structure of the cached data via Redis data structures.

memcached 和 Redis 都不会执行真正的 LRU 驱逐,而只是近似值.

Both memcached and Redis don't perform real LRU evictions, but only an approximation of that.

内存缓存逐出是按大小分类的,取决于其slab分配器的实现细节.例如,如果您想添加一个适合给定大小类别的项目,memcached 将尝试删除该类别中过期/最近未使用的项目,而不是尝试全局尝试了解该对象是什么,而不管它是什么大小,这是最好的候选.

Memcache eviction is per-size class and depends on the implementation details of its slab allocator. For example if you want to add an item which fits in a given size class, memcached will try to remove expired / not-recently-used items in that class, instead to try a global attempt to understand what is the object, regardless of its size, which is the best candidate.

Redis 会尝试在达到 maxmemory 限制时选择一个好的对象作为驱逐的候选对象,查看所有对象,无论大小类别如何,但只能提供大约好的对象,而不是空闲时间更长的最好的对象.

Redis instead tries to pick a good object as a candidate for eviction when the maxmemory limit is reached, looking at all the objects, regardless of the size class, but is only able to provide an approximately good object, not the best object with the greater idle time.

Redis 这样做的方法是对几个对象进行采样,选择空闲(未访问)时间最长的对象.由于 Redis 3.0(目前处于测试阶段),算法得到了改进,并且在逐出时也采用了良好的候选池,因此改进了近似值.在 Redis 文档中,您可以找到有关其工作原理的描述和图表.

The way Redis does this is by sampling a few objects, picking the one which was idle (not accessed) for the longest time. Since Redis 3.0 (currently in beta) the algorithm was improved and also takes a good candidates pools across evictions, so the approximation was improved. In the Redis documentation you can find a description and graphs with details about how it works.

Redis 是一个更复杂的软件,因此 Redis 中的值以一种更类似于高级编程语言中的对象的方式存储:它们具有关联的类型、编码、用于内存管理的引用计数.这使得 Redis 内部结构良好且易于管理,但与仅处理字符串的 memcached 相比存在开销.

Redis is a more complex piece of software, so values in Redis are stored in a way more similar to objects in a high level programming language: they have associated type, encoding, reference counting for memory management. This makes Redis internal structure good and manageable, but has an overhead compared to memcached which only deals with strings.

Redis 能够以一种特殊的内存节省方式存储小型聚合数据类型.例如,代表一个对象的小型 Redis 哈希在内部存储,而不是使用哈希表,而是作为二进制唯一 blob 存储.因此,将每个对象的多个字段设置为一个哈希比将 N 个分隔的键存储到 memcached 中更有效.

Redis is able to store small aggregate data types in a special memory saving way. For example a small Redis Hash representing an object, is stored internally not with an hash table, but as a binary unique blob. So setting multiple fields per object into an hash is more efficient than storing N separated keys into memcached.

实际上,您可以将对象作为单个 JSON(或二进制编码)blob 存储到 memcached 中,但与 Redis 相反,这将不允许您获取或更新独立字段.

You can, actually, store an object into memcached as a single JSON (or binary-encoded) blob, but contrary to Redis, this will not allow you to fetch or update independent fields.

由于 Redis 数据结构,当缓存失效时,memcached 使用的通常模式是销毁对象,稍后从数据库中重新创建它,这是使用 Redis 的原始方式.

Because of Redis data structures, the usual pattern used with memcached of destroying objects when the cache is invalidated, to recreate it from the DB later, is a primitive way of using Redis.

例如,假设您需要缓存发布到 Hacker News 中的最新 N 条新闻,以便填充站点的最新"部分.您使用 Redis 所做的是获取插入最新新闻的列表(最多为 M 项).如果您使用另一个存储来存储数据,并使用 Redis 作为缓存,那么您要做的是在发布新项目时填充两个视图(Redis 和 DB).没有缓存失效.

For example, imagine you need to cache the latest N news posted into Hacker News in order to populate the "Newest" section of the site. What you do with Redis is to take a list (capped to M items) with the newest news inserted. If you use another store for your data, and Redis as a cache, what you do is to populate both the views (Redis and the DB) when a new item is posted. There is no cache invalidation.

然而,应用程序始终可以有逻辑,以便如果发现 Redis 列表为空,例如在启动后,可以从数据库重新创建初始视图.

However the application can always have logic so that if the Redis list is found to be empty, for example after a startup, the initial view can be re-created from the DB.

与memcached相比,通过使用智能缓存,可以以更高效的方式使用Redis执行缓存,但并非所有问题都适合这种模式.例如,HTML 片段缓存可能无法从这种技术中受益.

By using intelligent caching it is possible to perform caching with Redis in a more efficient way compared to memcached, but not all the problems are suitable for this pattern. For example HTML fragments caching may not benefit from this technique.

这篇关于如果 redis 已经是堆栈的一部分,为什么 Memcached 仍然与 Redis 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆