文档结构  
翻译进度:53%     翻译赏金:10 元 (?)    ¥ 我要打赏

相比于 memcached,Redis 更强大,更流行,更好的支持。Memcached 仅能完成 Redis 所有功能中的一小部分。即使对于其特性重叠的部分,Redis 也更为优秀。

对于新项目,请使用 Redis。

Memcached vs Redis: 直接比较

作为缓存,两个工作都是强大,快速,有用的内存数据存储。两者都可以通过缓存数据库结果,HTML片段或是任何其他生成较为昂贵的内容有助于加速你的应用。

考虑点

当用于相同的目的时,下面是就原始问题的 ”考虑点“,他们如何比较。

第 1 段(可获 1.25 积分)
  • 读写速度:两者都非常快。虽然不同版本或者不同负载等因素都对速度有影响,但是总的来说,redis几乎和memcached一样快。我推荐redis的原因并不是因为memcached速度慢,memcached也是很快的。
  • 内存占用:Redis更优。
    • memcached: 你可以指定缓存大小,不过当插入数据时,守护进程会马上增加一点缓存的大小。如果不重启memcached,那么增加的这一点缓存不会被释放。当所有key都过期,然后你flush数据库以后,你会发现memcached仍然占用一整块你分配给它的RAM。
    • redis:分配多大的内存都由你决定。Redis永远不会占用比规划的空间更多的内存,而且在不再用时,会释放内存。
    • 我做过这样的测试,存储随机的大小为100,000~2KB的字符串(共200MB)到两者中。memcached占用RAM最多225MB,redis占用最多228MB。清空数据之后,redis内存占用掉到29MB,而memcached仍然在225MB。它们在存储数据方面都很高效,不过只有一个能释放不再用的内存。
  • dump数据到硬盘:redis显然更胜一筹,因为redis自带导出dump的功能,并且可以灵活配置持久化。memcached如果没有第三方库不能dump数据。
  • 使用规模:两者都可以在处理大量数据时使用。不过redis有辅助工具可以指导你,memcached没有。
第 2 段(可获 3.19 积分)

memcached

Memcached 是一个简单的可变缓存服务器。它允许你存储键/值对,而且其值被限制为至多 1MB 的字符串。

它擅长于此,而这也是它的全部工作。你可以以极高的速度通过键访问这些值,经常使得可用网络甚至是内存带宽得到饱和。

当你重启 memcached 时你的数据会丢失。这对于缓存是可以的。你不应该在那里存储任何重要的内容。

如果你需要高性或高可用性,有一些第三方工作,产品与服务可供选择。

redis

Redis 可以完成与 memcached 相同的工作,而且可以做得更好。

第 3 段(可获 1.4 积分)

redis也可以用来做缓存服务,它也可以存储键值对,存储大小甚至可以到512MB。

你可以不用持久化,redis也会在重启后丢失数据。你也可以在重启之后仍然有数据,这是redis的默认功能。

redis的存取也很快,常常受限于网络或者内存。

如果单实例的redis/memcached对于你来说性能不佳,redis集群是个更好的选择。redis包括对集群的支持,并且有现成的高可用工具(redis-sentinel)。在过去的几年里,redis在第三方支持库里是领军地位。一些公司,比如 Redis Labs、Amazon 提供了许多有用的redis工具和服务。redis的生态系统更大。redis的使用范围比memcached广得多。

第 4 段(可获 1.95 积分)

Redis  超集

Redis 不止是一个 cache。它是一个内存数据结构服务器。接下去你可以快速了解, redis除了能像memcached一样作为键值缓存之外,还能干些什么。大部分redis的功能是memcached不具备的

文档

Redis的文档比memcached更好。虽然这可能有点主观,但似乎越来越正确。

redis.io 是个非常容易导航的文档资源。能让你在浏览器中试用redis,甚至还给文档中的每个命令都提供了实时交互示例。

目前,stackoverflow上的帖子数或Google的结果数量,redis都是memcached的2倍。更多可访问的语言示例,更积极的发展,更积极的客户端开发。这些指标也许并不意味着什么, 但综合起来可以明显看出, redis的支持力度更大且文档更新。

第 5 段(可获 1.95 积分)

持久化

Redis默认使用一种被称为“快照“的机制来持久化数据。在机器有足够的内存的情况下,它可以把所有的数据都写入到磁盘,并且几乎不引起性能的下降。使用该机制的成本几乎为零。

在快照模式下,一次服务的崩溃可能会引起少量的数据丢失。如果你不能接受任何数据丢失,也不用担心,Redis还提供了AOF(追加文件)模式。在这种模式下,数据可以实时的被同步到磁盘上。这样会使Redis写数据的速度降到磁盘写数据的速度水平,但是这样也还是很快的。

第 6 段(可获 1.5 积分)

There are many configuration options to fine tune persistence if you need, but the defaults are very sensible. These options make it easy to setup redis as a safe, redundant place to store data. It is a real database.

Many Data Types

Memcached is limited to strings, but Redis is a data structure server that can serve up many different data types. It also provides the commands you need to make the most of those data types.

Strings (commands)

Simple text or binary values that can be up to 512MB in size. This is the only data type redis and memcached share, though memcached strings are limited to 1MB.

第 7 段(可获 1.36 积分)

Redis gives you more tools for leveraging this datatype by offering commands for bitwise operations, bit-level manipulation, floating point increment/decrement support, range queries, and multi-key operations. Memcached doesn't support any of that.

Strings are useful for all sorts of use cases, which is why memcached is fairly useful with this data type alone.

Hashes (commands)

Hashes are sort of like a key value store within a key value store. They map between string fields and string values. Field->value maps using a hash are slightly more space efficient than key->value maps using regular strings.

第 8 段(可获 1.21 积分)

Hashes are useful as a namespace, or when you want to logically group many keys. With a hash you can grab all the members efficiently, expire all the members together, delete all the members together, etc. Great for any use case where you have several key/value pairs that need to grouped.

One example use of a hash is for storing user profiles between applications. A redis hash stored with the user ID as the key will allow you to store as many bits of data about a user as needed while keeping them stored under a single key. The advantage of using a hash instead of serializing the profile into a string is that you can have different applications read/write different fields within the user profile without having to worry about one app overriding changes made by others (which can happen if you serialize stale data).

第 9 段(可获 1.85 积分)

Lists (commands)

Redis lists are ordered collections of strings. They are optimized for inserting, reading, or removing values from the top or bottom (aka: left or right) of the list.

Redis provides many commands for leveraging lists, including commands to push/pop items, push/pop between lists, truncate lists, perform range queries, etc.

Lists make great durable, atomic, queues. These work great for job queues, logs, buffers, and many other use cases.

Sets (commands)

Sets are unordered collections of unique values. They are optimized to let you quickly check if a value is in the set, quickly add/remove values, and to measure overlap with other sets.

第 10 段(可获 1.34 积分)

These are great for things like access control lists, unique visitor trackers, and many other things. Most programming languages have something similar (usually called a Set). This is like that, only distributed.

Redis provides several commands to manage sets. Obvious ones like adding, removing, and checking the set are present. So are less obvious commands like popping/reading a random item and commands for performing unions and intersections with other sets.

Sorted Sets (commands)

Sorted Sets are also collections of unique values. These ones, as the name implies, are ordered. They are ordered by a score, then lexicographically.

第 11 段(可获 1.23 积分)

This data type is optimized for quick lookups by score. Getting the highest, lowest, or any range of values in between is extremely fast.

If you add users to a sorted set along with their high score, you have yourself a perfect leader-board. As new high scores come in, just add them to the set again with their high score and it will re-order your leader-board. Also great for keeping track of the last time users visited and who is active in your application.

Storing values with the same score causes them to be ordered lexicographically (think alphabetically). This can be useful for things like auto-complete features.

第 12 段(可获 1.34 积分)

Many of the sorted set commands are similar to commands for sets, sometimes with an additional score parameter. Also included are commands for managing scores and querying by score.

Geo

Redis has several commands for storing, retrieving, and measuring geographic data. This includes radius queries and measuring distances between points.

Technically geographic data in redis is stored within sorted sets, so this isn't a truly separate data type. It is more of an extension on top of sorted sets.

Bitmap and HyperLogLog

Like geo, these aren't completely separate data types. These are commands that allow you to treat string data as if it's either a bitmap or a hyperloglog.

第 13 段(可获 1.4 积分)

Bitmaps are what the bit-level operators I referenced under Strings are for. This data type was the basic building block for reddit's recent collaborative art project: r/Place.

HyperLogLog allows you to use a constant extremely small amount of space to count almost unlimited unique values with shocking accuracy. Using only ~16KB you could efficiently count the number of unique visitors to your site, even if that number is in the millions.

Transactions and Atomicity

Commands in redis are atomic, meaning you can be sure that as soon as you write a value to redis that value is visible to all clients connected to redis. There is no wait for that value to propagate. Technically memcached is atomic as well, but with redis adding all this functionality beyond memcached it is worth noting and somewhat impressive that all these additional data types and features are also atomic.

第 14 段(可获 1.84 积分)

While not quite the same as transactions in relational databases, redis also has transactions that use "optimistic locking" (WATCH/MULTI/EXEC).

Pipelining

Redis provides a feature called 'pipelining'. If you have many redis commands you want to execute you can use pipelining to send them to redis all-at-once instead of one-at-a-time.

Normally when you execute a command to either redis or memcached, each command is a separate request/response cycle. With pipelining, redis can buffer several commands and execute them all at once, responding with all of the responses to all of your commands in a single reply.

第 15 段(可获 1.23 积分)

这可以使您在批量导入或涉及大量命令的其他操作中实现更高的吞吐量。

发布/订阅

Redis有一个专用于发布/订阅 功能命令,允许redis充当高速信息广播器。 这允许单个客户端将消息发布到连接到频道的许多其他客户端。

Redis拥有几乎任何工具都有的发布/订阅。 像RabbitMQ这样的专用消息代理可能在某些方面有优势,但事实上同一台服务器,也可以为你的发布/订阅负载,尽可能的提供持久的持久队列和其他数据结构,为此Redis通常会被证明是跑任务最好,最简单的工具。

第 16 段(可获 1.45 积分)

Lua 脚本

你可以认为 lua scripts 是redis自己的SQL或存储过程。可能不太准确,但大致类似。

也许你有复杂的计算需要redis执行。也许你不能承受事务回滚,需要保证一个复杂的过程原子化执行。它们都可以通过Lua脚本解决。

整个脚本是原子化执行的,所以如果你能将你的逻辑通过Lua脚本实现,通常可以避免被乐观锁事务干扰。

第 17 段(可获 1.26 积分)

扩展性

前面提到过,redis有对集群的内建支持,也有自己的高可用工具,redis-sentinel。

结论

毫无疑问,对于任何新项目或者没有在用memcached的老项目,我更推荐redis。

这篇文章可能让人觉得我不喜欢memcached。事实上我很喜欢memcached,因为它是一个简洁、稳定、成熟的工具。有些时候它甚至要比redis还要快。虽然我喜欢memcached,但它的应用前景确实不够好。

第 18 段(可获 1.19 积分)

其实Redis几乎能做到memcached能做到的一切,甚至更好。memcached的所有性能优势都不明显,而且需要在特定的业务场景下。在一些业务场景下redis会更快,还有更多的业务场景下redis可以做到memcached做不到的性能。对于巨大的功能差异,以及两种工具都非常快也非常有效率,这些微小的性能差异几乎可以忽略,它们很可能是您的基础架构的最后一部分,您其实不必过多担心使用哪个的问题。

只有在一种场景memcached是更适用的:已经使用memcached来作缓存了。 如果你已经用memcached作缓存了,而且它也正好满足你的需求,那么就继续使用它。 如果你只是使用redis来缓存,那么移值到redis可能不那么值得,它可能没有那么多值得你花时间的优点。 如果memcached并不能满足你的需求,那么你应该转向使用redis。 无论您需要在memcached上扩展还是需要别的功能,情况都是如此。

第 19 段(可获 2.28 积分)

文章评论