简体   繁体   English

Redis 是否持久化数据?

[英]Does Redis persist data?

I understand that Redis serves all data from memory, but does it persist as well across server reboot so that when the server reboots it reads into memory all the data from disk.我知道 Redis 从内存中提供所有数据,但它是否也能在服务器重新启动后持续存在,以便在服务器重新启动时将磁盘中的所有数据读入内存。 Or is it always a blank store which is only to store data while apps are running with no persistence?或者它总是一个空白存储,仅在应用程序运行时存储数据而没有持久性?

I suggest you read about this on http://redis.io/topics/persistence .我建议您在http://redis.io/topics/persistence上阅读有关此内容的信息。 Basically you lose the guaranteed persistence when you increase performance by using only in-memory storing.基本上,当您仅使用内存存储来提高性能时,就会失去保证的持久性。 Imagine a scenario where you INSERT into memory, but before it gets persisted to disk lose power.想象一个场景,您 INSERT 到内存中,但在它被持久化到磁盘之前就断电了。 There will be data loss.会有数据丢失。

Redis supports so-called "snapshots". Redis 支持所谓的“快照”。 This means that it will do a complete copy of whats in memory at some points in time (eg every full hour).这意味着它将在某些时间点(例如,每整整一小时)对内存中的内容进行完整复制。 When you lose power between two snapshots, you will lose the data from the time between the last snapshot and the crash (doesn't have to be a power outage..).当您在两次快照之间断电时,您将丢失上次快照和崩溃之间的数据(不一定是停电..)。 Redis trades data safety versus performance, like most NoSQL-DBs do. Redis 在数据安全与性能之间进行权衡,就像大多数 NoSQL-DB 一样。

Most NoSQL-databases follow a concept of replication among multiple nodes to minimize this risk.大多数 NoSQL 数据库遵循在多个节点之间复制的概念,以最大限度地减少这种风险。 Redis is considered more a speedy cache instead of a database that guarantees data consistency. Redis 更被认为是一种快速缓存,而不是保证数据一致性的数据库。 Therefore its use cases typically differ from those of real databases: You can, for example, store sessions, performance counters or whatever in it with unmatched performance and no real loss in case of a crash.因此,它的用例通常与真实数据库的用例不同:例如,您可以存储会话、性能计数器或其中的任何具有无与伦比的性能并且在崩溃时不会真正丢失的东西。 But processing orders/purchase histories and so on is considered a job for traditional databases.但是处理订单/购买历史记录等被认为是传统数据库的工作。

Redis server saves all its data to HDD from time to time, thus providing some level of persistence. Redis 服务器不时将其所有数据保存到 HDD,从而提供一定程度的持久性。

It saves data in one of the following cases:它在以下情况之一中保存数据:

  • automatically from time to time不时自动
  • when you manually call BGSAVE command当您手动调用BGSAVE命令时
  • when redis is shutting down当 redis 关闭时

But data in redis is not really persistent, because:但是redis中的数据并不是真正持久化的,因为:

  • crash of redis process means losing all changes since last save redis 进程崩溃意味着丢失自上次保存以来的所有更改
  • BGSAVE operation can only be performed if you have enough free RAM (the amount of extra RAM is equal to the size of redis DB) BGSAVE操作只有在你有足够的空闲RAM时才能执行(额外RAM的数量等于redis DB的大小)

NB: BGSAVE RAM requirement is a real problem, because redis continues to work up until there is no more RAM to run in, but it stops saving data to HDD much earlier (at approx. 50% of RAM).注意: BGSAVE RAM 要求是一个真正的问题,因为 redis 会继续工作,直到没有更多的 RAM 可以运行,但它会更早地停止将数据保存到 HDD(大约为 RAM 的 50%)。

For more information see Redis Persistence .有关更多信息,请参阅Redis 持久性

It is a matter of configuration.这是配置问题。 You can have none, partial or full persistence of your data on Redis.您可以在 Redis 上不保留、部分或完全保留数据。 The best decision will be driven by the project's technical and business needs.最佳决策将由项目的技术和业务需求驱动。

According to the Redis documentation about persistence you can set up your instance to save data into disk from time to time or on each query, in a nutshell.根据关于持久性Redis 文档,简而言之,您可以将实例设置为不时或在每次查询时将数据保存到磁盘中。 They provide two strategies/methods AOF and RDB (read the documentation to see details about then), you can use each one alone or together.它们提供了两种策略/方法 AOF 和 RDB(阅读文档以了解有关 then 的详细信息),您可以单独或一起使用每一种。

If you want a "SQL like persistence", they have said:如果你想要一个“像持久性一样的 SQL”,他们说:

The general indication is that you should use both persistence methods if you want a degree of data safety comparable to what PostgreSQL can provide you.一般的迹象是,如果您想要与 PostgreSQL 可以提供的数据安全程度相当的数据安全性,则应该同时使用这两种持久性方法。

The answer is generally yes , however a fuller answer really depends on what type of data you're trying to store.答案通常是肯定的,但是更完整的答案实际上取决于您尝试存储的数据类型。 In general, the more complete short answer is:一般来说,更完整的简短答案是:

  • Redis isn't the best fit for persistent storage as it's mainly performance focused Redis 不是持久存储的最佳选择,因为它主要关注性能
  • Redis is really more suitable for reliable in-memory storage/cacheing of current state data, particularly for allowing scalability by providing a central source for data used across multiple clients/servers Redis 确实更适合可靠的内存中存储/缓存当前状态数据,特别是通过为跨多个客户端/服务器使用的数据提供中央源来实现可扩展性

Having said this, by default Redis will persist data snapshots at a periodic interval (apparently this is every 1 minute, but I haven't verified this - this is described by the article below, which is a good basic intro):话虽如此,默认情况下 Redis定期保留数据快照(显然这是每 1 分钟一次,但我尚未验证这一点 - 下面的文章对此进行了描述,这是一个很好的基本介绍):

http://qnimate.com/redis-permanent-storage/ http://qnimate.com/redis-permanent-storage/


TL;DR TL; 博士

From the official docs :来自官方文档

  • RDB persistence [the default] performs point-in-time snapshots of your dataset at specified intervals. RDB 持久性[默认]以指定的时间间隔执行数据集的时间点快照。
  • AOF persistence [needs to be explicitly configured] logs every write operation received by the server, that will be played again at server startup, reconstructing the original dataset. AOF持久化[需要显式配置]记录服务器收到的每个写操作,在服务器启动时会再次播放,重建原始数据集。

Redis must be explicitly configured for AOF persistence, if this is required, and this will result in a performance penalty as well as growing logs.如果需要,Redis 必须为 AOF 持久性显式配置,这将导致性能损失以及日志增长。 It may suffice for relatively reliable persistence of a limited amount of data flow.对于有限数量的数据流的相对可靠的持久性可能就足够了。

You can choose no persistence at all.Better performance but all the data lose when Redis shutting down.完全可以选择不持久化。性能更好,但是Redis关闭时所有数据都会丢失。

Redis has two persistence mechanisms: RDB and AOF.RDB uses a scheduler global snapshooting and AOF writes update to an apappend-only log file similar to MySql. Redis 有两种持久化机制:RDB 和 AOF。RDB 使用调度程序全局快照,AOF 将更新写入类似于 MySql 的仅附加日志文件。

You can use one of them or both.When Redis reboots,it constructes data from reading the RDB file or AOF file.您可以使用其中之一或两者。当Redis重启时,它通过读取RDB文件或AOF文件来构造数据。

Many Not well-informed and relatively new users think that Redis is a cache only and NOT an ideal choice for Reliable Persistence .许多消息灵通和相对较新的用户认为 Redis 只是一个缓存,而不是Reliable Persistence的理想选择。 The reality is that the lines between DB, Cache (and many more types) are blurred nowadays.现实情况是,如今 DB、Cache(以及更多类型)之间的界限已经模糊。

It's all configurable and as users/engineers we have choices to configure it as a cache, as a DB (and even as a hybrid).它都是可配置的,作为用户/工程师,我们可以选择将其配置为缓存、数据库(甚至混合)。

Each choice comes with benefits and costs.每一个选择都有好处和成本。 And this is NOT an exception for Redis but all well-known Distributed systems provide options to configure different aspects (Persistence, Availability, Consistency, etc).这不是 Redis 的例外,但所有著名的分布式系统都提供了配置不同方面(持久性、可用性、一致性等)的选项。 So, if you configure Redis in default mode hoping that it will magically give you highly reliable persistence then it's team/engineer fault (and NOT that of Redis).因此,如果您在默认模式下配置 Redis,希望它能神奇地为您提供高度可靠的持久性,那么这是团队/工程师的错误(而不是 Redis 的错误)。

I have discussed these aspects in more detail on my blog here .我有更多的细节在我的博客中讨论这些方面在这里

Also, here is a link from Redis itself.另外,这里有一个来自 Redis 本身的链接

All the answers in this thread are talking about the possibility of redis to persist the data: https://redis.io/topics/persistence (Using AOF + after every write (change)).该线程中的所有答案都在谈论redis持久化数据的可能性: https : //redis.io/topics/persistence (每次写入(更改)后使用AOF +)。

It's a great link to get you started, but it is defenently not showing you the full picture.这是一个很好的链接,可以帮助您入门,但显然没有向您展示全貌。


Can/Should You Really Persist Unrecoverable Data/State On Redis?您真的可以/应该在 Redis 上保留不可恢复的数据/状态吗?

Redis docs does not talk about: Redis 文档没有谈到:

  1. Which redis providers support this (AOF + after every write) option:哪些 redis 提供者支持这个(AOF + 每次写入后)选项:
  • Almost none of them - redis labs on the cloud does NOT provide this option.几乎没有 - 云上的 redis 实验室不提供此选项。 You may need to buy the on-premise version of redis-labs to support it.可能需要购买内部部署版本的 redis-labs 来支持它。 As not all companies are willing to go on-premise, then they will have a problem.由于并非所有公司都愿意进行内部部署,因此他们会遇到问题。
  • Other Redis Providers does not specify if they support this option at all.其他 Redis 提供程序根本没有指定是否支持此选项。 AWS Cache, Aivan,... AWS 缓存、Aivan、...
  • AOF + after every write - This option is slow. AOF + 每次写入后 - 此选项很慢。 you will have to test it your self on your production hardware to see if it fits your requirements.您必须在生产硬件上自行测试,看看它是否符合您的要求。
  • Redis enterpice provide this option and from this link: https://redislabs.com/blog/your-cloud-cant-do-that-0-5m-ops-acid-1msec-latency/ let's see some banchmarks: Redis 企业提供了这个选项,从这个链接: https ://redislabs.com/blog/your-cloud-cant-do-that-0-5m-ops-acid-1msec-latency/ 让我们看看一些基准:

1x x1.16xlarge instance on AWS - They could not achieve less than 2ms latency: AWS 上的 1x x1.16xlarge 实例 - 他们无法实现低于 2 毫秒的延迟:

where latency was measured from the time the first byte of the request arrived at the cluster until the first byte of the 'write' response was sent back to the client其中延迟是从请求的第一个字节到达集群到“写”响应的第一个字节发送回客户端的时间测量的

They had additional banchmarking on a much better harddisk (Dell-EMC VMAX) which results < 1ms operation latency (!!) and from 70K ops/sec (write intensive test) to 660K ops/sec (read intensive test).他们在更好的硬盘 (Dell-EMC VMAX) 上进行了额外的基准测试,结果操作延迟小于 1 毫秒 (!!),从 70K ops/sec(写密集测试)到 660K ops/sec(读密集测试)。 Pretty impresive!!!相当感人!!!

在此处输入图片说明

But it defenetly required a (very) skilled devops to help you create this infrastructure and maintain it over time.它绝对需要(非常)熟练的开发人员来帮助您创建此基础架构并随着时间的推移对其进行维护。

  1. One could (falsy) argue that if you have a cluster of redis nodes (with replicas), now you have full persistency.有人可能(虚假)争辩说,如果您有一组 redis 节点(带有副本),那么现在您拥有完全的持久​​性。 this is false claim:这是虚假声明:
  • All DBs (sql,non-sql,redis,...) have the same problem - For example, running set x 1 on node1, how much time it takes for this (or any) change to be made in all the other nodes.所有数据库(sql、非 sql、redis...)都有相同的问题 - 例如,在 node1 上运行set x 1 ,在所有其他节点中进行此(或任何)更改需要多长时间. So additional reads will receive the same output.所以额外的读取将收到相同的输出。 well, it depends on alot of fuctors and configurations.好吧,这取决于很多因素和配置。
  • It is a nightmare to deal with inconsistency of a value of a key in multiple nodes (any DB type).处理多个节点(任何DB类型)中一个键的值不一致是一场噩梦。 You can read more about it from Redis Author (antirez): http://antirez.com/news/66 .您可以从 Redis Author (antirez) 阅读更多相关信息: http ://antirez.com/news/66。 Here is a short example of the actual ngihtmare of storing a state in redis (+ a solution - WAIT command to know how much other redis nodes received the latest change change):这是在 redis 中存储状态的实际 ngihtmare 的简短示例(+ 解决方案 - WAIT命令以了解其他 redis 节点收到了多少最新更改):
    def save_payment(payment_id)
        redis.rpush(payment_id,”in progress”) # Return false on exception
        if redis.wait(3,1000) >= 3 then
            redis.rpush(payment_id,”confirmed”) # Return false on exception
            if redis.wait(3,1000) >= 3 then
                return true
            else
                redis.rpush(payment_id,”cancelled”)
                return false
            end
        else
            return false
    end

The above example is not suffeint and has a real problem of knowing in advance how much nodes there actually are (and alive) at every moment.上面的例子不够完整,并且有一个真正的问题是提前知道每个时刻实际上有多少(和活着的)节点。

Other DBs will have the same problem as well.其他数据库也会有同样的问题。 Maybe they have better APIs but the problem still exists.也许他们有更好的 API,但问题仍然存在。

As far as I know, alot of applications are not even aware of this problem.据我所知,很多应用程序甚至都没有意识到这个问题。

All in all, picking more dbs nodes is not a one click configuration.总而言之,选择更多的dbs节点不是一键配置。 It involves alot more.它涉及更多。


To conclude this research, what to do depends on:总结这项研究,做什么取决于:

  1. How much devs your team has (so this task won't slow you down)?你的团队有多少开发人员(所以这项任务不会减慢你的速度)?
  2. Do you have a skilled devops?你有熟练的 DevOps 吗?
  3. What is the distributed-system skills in your team?您团队中的分布式系统技能是什么?
  4. Money to buy hardware?花钱买硬件?
  5. Time to invest in the solution?是时候投资解决方案了吗?
  6. And probably more...而且可能更多...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM