Memcached与Redis? [英] Memcached vs. Redis?

查看:96
本文介绍了Memcached与Redis?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用带有 Redis 服务器的Ruby网络应用进行缓存.有没有要测试 Memcached 的点?

什么将给我们带来更好的性能? Redis和Memcached之间有什么优缺点?

要考虑的要点:

  • 读/写速度.
  • 内存使用情况.
  • 磁盘I/O转储.
  • 缩放.

解决方案

摘要(TL; DR)

2017年6月3日更新

与memcached相比,Redis功能更强大,更受欢迎并且得到更好的支持. Memcached只能做Redis可以做的一小部分.即使功能重叠,Redis也会更好.

对于任何新内容,请使用Redis.

Memcached与Redis:直接比较

这两个工具都是功能强大,快速的内存中数据存储,可用作高速缓存.两者都可以通过缓存数据库结果,HTML片段或其他可能生成成本很高的东西来帮助加快应用程序的运行速度.

要考虑的重点

在用于同一事物时,这是它们如何使用原始问题的要考虑的要点"进行比较:

  • 读/写速度:两者都非常快.基准测试因工作负载,版本和许多其他因素而异,但通常显示redis与memcached一样快或几乎一样快.我建议使用redis,但不是因为memcached速度慢.不是.
  • 内存使用情况:Redis更好.
    • memcached:您指定高速缓存大小,并且在插入项目时,守护程序会迅速增长到比该大小大一点的数量.除了重新启动memcached之外,从来没有真正的方法可以回收任何空间.您所有的密钥都可能过期,您可以刷新数据库,但它仍将使用您为其配置的RAM的全部块.
    • redis:设置最大大小由您决定. Redis永远不会使用过多的内存,并且会为您提供不再使用的内存.
    • 我将100,000个〜2KB字符串(〜200MB)的随机句子存储到了两者中. Memcached RAM使用量增加到约225MB. Redis RAM使用量增加到〜228MB.刷新两次后,redis下降至〜29MB,memcached保持在〜225MB.它们在存储数据方面同样有效,但是只有一个能够回收数据.
  • 磁盘I/O转储:redis显然是赢家,因为它默认情况下会这样做,并且具有非常可配置的持久性.没有第3方工具,Memcached就没有任何转储到磁盘的机制.
  • 扩展:在需要多个实例作为缓存之前,两者都为您提供了大量的净空. Redis包含的工具可帮助您超越此范围,而memcached则不会.

memcached

Memcached是一个简单的易失性缓存服务器.它允许您存储键/值对,其中值限制为最大1MB的字符串.

这很擅长,但仅此而已.您可以通过它们的键以极高的速度访问这些值,通常会饱和可用的网络甚至是内存带宽.

重新启动内存缓存后,您的数据不见了.这对于缓存很好.您不应该在其中存储任何重要内容.

如果您需要高性能或高可用性,则可以使用第三方工具,产品和服务.

redis

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

Redis也可以充当缓存.它也可以存储键/值对.在redis中,它们甚至可以达到512MB.

您可以关闭持久性,并且在重新启动时也很可能会丢失数据.如果您希望缓存能够继续运行,那么重新启动也可以.实际上,这是默认设置.

它也非常快,通常受网络或内存带宽的限制.

如果一个redis/memcached实例的性能不足以满足您的工作负载,那么redis是不二之选. Redis包括集群支持,并提供了高可用性工具(

Redis超集

Redis不仅仅是一个缓存.它是一个内存中的数据结构服务器.在下面,您将快速概览Redis可以做的事情,而不仅仅是像memcached这样的简单键/值缓存. Redis功能的大多数是memcached无法做到的.

文档

Redis的文档比memcached的文档更好.尽管这可能是主观的,但它似乎一直在越来越真实.

redis.io 是一种很棒的易于浏览的资源.它使您可以在浏览器中尝试Redis ,甚至还可以为您提供文档中每个命令的实时交互示例.

redis的堆栈溢出结果现在是memcached的2倍. Google搜寻结果的2倍.提供更多语言的更易于访问的示例.更积极的发展.更积极的客户开发.这些度量可能并没有太大的意义,但结合起来可以清楚地看到,Redis的支持和文档越来越多,而且是最新的.

持久性

默认情况下,redis使用称为快照的机制将数据持久保存到磁盘.如果您有足够的可用RAM,则可以将所有数据写入磁盘,而几乎不会降低性能.几乎是免费的!

在快照模式下,突然崩溃可能会导致少量数据丢失.如果您绝对需要确保没有数据丢失,请放心,redis也可以通过AOF(仅附加文件)模式在后台工作.在这种持久模式下,数据可以在写入时同步到磁盘.这样可以将最大写入吞吐量降低到磁盘可以写入的速度,但是仍然应该很快.

有许多配置选项可以根据需要微调持久性,但是默认值非常明智.这些选项使将Redis设置为安全,冗余的存储数据位置变得容易.这是一个 real 数据库.

许多数据类型

Memcached仅限于字符串,但是Redis是一个数据结构服务器,可以提供许多不同的数据类型.它还提供了充分利用这些数据类型所需的命令.

字符串(命令)

简单文本或二进制值,最大大小为512MB.这是唯一的数据类型redis和memcached共享,尽管memcached字符串限制为1MB.

Redis通过提供用于按位操作,位级别操作,浮点增量/减量支持,范围查询和多键操作的命令,为您提供了更多利用此数据类型的工具. Memcached不支持任何一个.

字符串对于各种用例都是有用的,这就是为什么memcached仅对这种数据类型有用的原因.

哈希(命令)

散列有点像键值存储中的键值存储.它们在字符串字段和字符串值之间映射.使用散列的Field-> Value映射比使用常规字符串的key-> Value映射具有更高的空间效率.

散列可用作名称空间,或者在您要逻辑上对许多键进行分组时很有用.使用散列,您可以有效地捕获所有成员,一起使所有成员到期,一起删除所有成员,等等.对于需要将多个键/值对进行分组的用例而言,它非常有用.

哈希的一个示例用法是在应用程序之间存储用户配置文件.使用用户ID作为键存储的Redis哈希将允许您根据需要存储有关用户的数据,同时将其存储在单个键下.使用散列而不是将配置文件序列化为字符串的优点是,您可以让不同的应用程序在用户配置文件中读/写不同的字段,而不必担心一个应用程序覆盖其他应用程序所做的更改(如果您将陈旧的序列化,则可能会发生这种情况)数据).

列表(命令)

Redis列表是字符串的有序集合.它们经过优化,可从列表的顶部或底部(又名:左侧或右侧)插入,读取或删除值.

Redis提供了许多命令来利用列表,包括用于推送/弹出项目的命令,列表,截断列表,执行范围查询等.

列表使持久的,原子的队列成为可能.这些适用于作业队列,日志,缓冲区和许多其他用例.

集合(命令)

集合是唯一值的无序集合.它们经过优化,可让您快速检查值是否在集合中,快速添加/删除值,并测量与其他集合的重叠.

这些功能非常适合访问控制列表,唯一的访问者跟踪器以及许多其他功能.大多数编程语言都有类似的东西(通常称为Set).就是这样,只有分布式.

Redis提供了几个命令来管理集合.存在诸如添加,删除和检查集合之类的显而易见的内容.因此,不太明显的命令(例如,弹出/读取随机项)以及用于执行与其他集合的并集和交集的命令.

排序集(命令)

排序集也是唯一值的集合.顾名思义,这些是有序的.它们按分数排序,然后按字典顺序排序.

此数据类型经过优化,可按分数快速查找.获得最高,最低或介于两者之间的任何值都非常快.

如果您将用户与他们的高分一起添加到排序的集合中,则您将拥有一个完美的排行榜.随着新的高分出现,只需将他们的高分再次添加到集合中,它将重新排列排行榜.跟踪用户上次访问的时间以及谁在您的应用程序中处于活动状态也非常有用.

存储具有相同分数的值会使它们按字典顺序(按字母顺序思考)排序.这对于自动完成功能等很有用.

许多排序集合命令与集合命令相似,有时带有附加的score参数.还包括用于管理分数和按分数查询的命令.

Geo

Redis有几个命令,用于存储,检索和测量地理数据.这包括半径查询和测量点之间的距离.

从技术上讲,redis中的地理数据存储在排序集中,因此这不是真正的独立数据类型.它更多地是对排序集的扩展.

位图和HyperLogLog

像geo一样,这些不是完全独立的数据类型.这些命令使您可以将字符串数据视为位图或超级日志.

位图是我在Strings下引用的位级运算符的作用.该数据类型是reddit最近的合作艺术项目的基本构建块: r /地方.

HyperLogLog允许您使用恒定的极少量空间以惊人的精度计算几乎无限的唯一值.仅使用〜16KB,您就可以有效地计算出网站唯一身份访问者的数量,即使该数量是数百万.

交易和原子性

redis中的命令是原子性的,这意味着您可以确保在向redis写入值后,该值对于连接到redis的所有客户端都是可见的.无需等待该值传播.从技术上讲,memcached也是原子的,但是redis在memcached之外添加了所有这些功能,值得注意的是,所有这些其他数据类型和功能也是原子的.

虽然与关系数据库中的事务不太相同,redis也具有事务,它们使用乐观锁定" ((观看// EXEC ).

管道

Redis提供了一项名为"管道"的功能.如果要执行许多redis命令,则可以使用流水线将它们一次发送一次,而不是一次发送到redis.

通常,当您执行命令以进行redis或memcached时,每个命令都是一个单独的请求/响应周期.借助管道,redis可以缓冲多个命令并一次执行所有命令,并在一次回复中对所有命令的所有响应进行响应.

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

Pub/Sub

Redis具有命令专门用于 RabbitMQ 这样的专用消息代理在某些方面可能具有优势,但是同一台服务器也可以为您提供持久性的事实持久的队列和您的发布/订阅工作负载可能需要的其他数据结构,Redis通常会被证明是完成这项工作的最好,最简单的工具.

Lua脚本

您可以想到 lua脚本,例如redis自己的SQL或存储过程.两者之间的差异更大或更小,但是这种类比大体上都是可行的.

也许您想让Redis执行复杂的计算.也许您负担不起交易回滚,并且需要保证复杂过程的每一步都将自动发生.这些问题以及更多问题可以通过lua脚本解决.

整个脚本是原子执行的,因此,如果您可以使逻辑适合lua脚本,则通常可以避免与乐观锁定事务混为一谈.

缩放

如上所述,redis包括对集群的内置支持,并与自己的称为redis-sentinel的高可用性工具捆绑在一起.

结论

对于任何新项目,或者没有使用memcached的现有项目,我都会毫不犹豫地建议在memcached上使用redis.

以上内容听起来像我不喜欢memcached.相反:它是一种功能强大,简单,稳定,成熟且经过强化的工具.甚至在某些用例中,它比redis快一点.我喜欢记忆快取.我只是认为这对未来的发展没有多大意义.

Redis可以完成memcached所做的一切,通常更好. memcached的任何性能优势都是次要的,并且取决于工作负载.还有一些工作负载的Redis将更快,而Redis可以做的很多工作却是memcached根本做不到的.面对巨大的功能鸿沟,微小的性能差异似乎微不足道,而且两个工具是如此之快和高效,它们很可能是您不得不担心扩展的基础架构的最后一部分.

只有一种情况使memcached更有意义:memcached已被用作缓存.如果您已经使用memcached进行缓存,那么在满足您需要的情况下,请继续使用它.迁移到Redis可能不值得,如果您仅将Redis用于缓存,则可能无法提供足够的好处,值得您花时间.如果memcached不能满足您的需求,那么您可能应该改用Redis.无论您需要扩展到内存缓存之外还是需要其他功能,都是如此.

We're using a Ruby web-app with Redis server for caching. Is there a point to test Memcached instead?

What will give us better performance? Any pros or cons between Redis and Memcached?

Points to consider:

  • Read/write speed.
  • Memory usage.
  • Disk I/O dumping.
  • Scaling.

解决方案

Summary (TL;DR)

Updated June 3rd, 2017

Redis is more powerful, more popular, and better supported than memcached. Memcached can only do a small fraction of the things Redis can do. Redis is better even where their features overlap.

For anything new, use Redis.

Memcached vs Redis: Direct Comparison

Both tools are powerful, fast, in-memory data stores that are useful as a cache. Both can help speed up your application by caching database results, HTML fragments, or anything else that might be expensive to generate.

Points to Consider

When used for the same thing, here is how they compare using the original question's "Points to Consider":

  • Read/write speed: Both are extremely fast. Benchmarks vary by workload, versions, and many other factors but generally show redis to be as fast or almost as fast as memcached. I recommend redis, but not because memcached is slow. It's not.
  • Memory usage: Redis is better.
    • memcached: You specify the cache size and as you insert items the daemon quickly grows to a little more than this size. There is never really a way to reclaim any of that space, short of restarting memcached. All your keys could be expired, you could flush the database, and it would still use the full chunk of RAM you configured it with.
    • redis: Setting a max size is up to you. Redis will never use more than it has to and will give you back memory it is no longer using.
    • I stored 100,000 ~2KB strings (~200MB) of random sentences into both. Memcached RAM usage grew to ~225MB. Redis RAM usage grew to ~228MB. After flushing both, redis dropped to ~29MB and memcached stayed at ~225MB. They are similarly efficient in how they store data, but only one is capable of reclaiming it.
  • Disk I/O dumping: A clear win for redis since it does this by default and has very configurable persistence. Memcached has no mechanisms for dumping to disk without 3rd party tools.
  • Scaling: Both give you tons of headroom before you need more than a single instance as a cache. Redis includes tools to help you go beyond that while memcached does not.

memcached

Memcached is a simple volatile cache server. It allows you to store key/value pairs where the value is limited to being a string up to 1MB.

It's good at this, but that's all it does. You can access those values by their key at extremely high speed, often saturating available network or even memory bandwidth.

When you restart memcached your data is gone. This is fine for a cache. You shouldn't store anything important there.

If you need high performance or high availability there are 3rd party tools, products, and services available.

redis

Redis can do the same jobs as memcached can, and can do them better.

Redis can act as a cache as well. It can store key/value pairs too. In redis they can even be up to 512MB.

You can turn off persistence and it will happily lose your data on restart too. If you want your cache to survive restarts it lets you do that as well. In fact, that's the default.

It's super fast too, often limited by network or memory bandwidth.

If one instance of redis/memcached isn't enough performance for your workload, redis is the clear choice. Redis includes cluster support and comes with high availability tools (redis-sentinel) right "in the box". Over the past few years redis has also emerged as the clear leader in 3rd party tooling. Companies like Redis Labs, Amazon, and others offer many useful redis tools and services. The ecosystem around redis is much larger. The number of large scale deployments is now likely greater than for memcached.

The Redis Superset

Redis is more than a cache. It is an in-memory data structure server. Below you will find a quick overview of things Redis can do beyond being a simple key/value cache like memcached. Most of redis' features are things memcached cannot do.

Documentation

Redis is better documented than memcached. While this can be subjective, it seems to be more and more true all the time.

redis.io is a fantastic easily navigated resource. It lets you try redis in the browser and even gives you live interactive examples with each command in the docs.

There are now 2x as many stackoverflow results for redis as memcached. 2x as many Google results. More readily accessible examples in more languages. More active development. More active client development. These measurements might not mean much individually, but in combination they paint a clear picture that support and documentation for redis is greater and much more up-to-date.

Persistence

By default redis persists your data to disk using a mechanism called snapshotting. If you have enough RAM available it's able to write all of your data to disk with almost no performance degradation. It's almost free!

In snapshot mode there is a chance that a sudden crash could result in a small amount of lost data. If you absolutely need to make sure no data is ever lost, don't worry, redis has your back there too with AOF (Append Only File) mode. In this persistence mode data can be synced to disk as it is written. This can reduce maximum write throughput to however fast your disk can write, but should still be quite fast.

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.

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.

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).

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.

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.

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.

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.

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.

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.

This can allow you to achieve even greater throughput on bulk importing or other actions that involve lots of commands.

Pub/Sub

Redis has commands dedicated to pub/sub functionality, allowing redis to act as a high speed message broadcaster. This allows a single client to publish messages to many other clients connected to a channel.

Redis does pub/sub as well as almost any tool. Dedicated message brokers like RabbitMQ may have advantages in certain areas, but the fact that the same server can also give you persistent durable queues and other data structures your pub/sub workloads likely need, Redis will often prove to be the best and most simple tool for the job.

Lua Scripting

You can kind of think of lua scripts like redis's own SQL or stored procedures. It's both more and less than that, but the analogy mostly works.

Maybe you have complex calculations you want redis to perform. Maybe you can't afford to have your transactions roll back and need guarantees every step of a complex process will happen atomically. These problems and many more can be solved with lua scripting.

The entire script is executed atomically, so if you can fit your logic into a lua script you can often avoid messing with optimistic locking transactions.

Scaling

As mentioned above, redis includes built in support for clustering and is bundled with its own high availability tool called redis-sentinel.

Conclusion

Without hesitation I would recommend redis over memcached for any new projects, or existing projects that don't already use memcached.

The above may sound like I don't like memcached. On the contrary: it is a powerful, simple, stable, mature, and hardened tool. There are even some use cases where it's a little faster than redis. I love memcached. I just don't think it makes much sense for future development.

Redis does everything memcached does, often better. Any performance advantage for memcached is minor and workload specific. There are also workloads for which redis will be faster, and many more workloads that redis can do which memcached simply can't. The tiny performance differences seem minor in the face of the giant gulf in functionality and the fact that both tools are so fast and efficient they may very well be the last piece of your infrastructure you'll ever have to worry about scaling.

There is only one scenario where memcached makes more sense: where memcached is already in use as a cache. If you are already caching with memcached then keep using it, if it meets your needs. It is likely not worth the effort to move to redis and if you are going to use redis just for caching it may not offer enough benefit to be worth your time. If memcached isn't meeting your needs, then you should probably move to redis. This is true whether you need to scale beyond memcached or you need additional functionality.

这篇关于Memcached与Redis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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