# Redis 持久化机制

Redis 提供了几种不同类型的数据持久化方案：

1. RDB 会每间隔一段时间，就持久化当前时间点的数据快照，并保存数据快照至磁盘文件中；
2. AOF 会记录每个修改数据的命令至磁盘文件中，用于在服务重启后的数据恢复；
3. 完全不使用数据持久化机制；
4. 组合使用 RDB 和 AOF 两种机制；

## RDB 优点

* RDB 产生的快照文件是非常紧凑的，相比于 AOF 的日志文件会小很多；
* RDB 非常适合用于数据的备份和灾备；

## RDB 缺点

* RDB 可能会丢失最近一次间隔时间内的数据；
* 在数据量非常大的情况下，RDB 在 fork 子进程时可能会非常耗时，以至于影响 Redis 的主服务进程。

## AOF 优点

* 可以选择「不显示 fsync」、「每秒 fsync」、「每个命令后 fsync」三种不同的 AOF 同步数据策略；
* AOF 只是简单的追加日志，所以执行起来会很简单；
* 当 AOF 的日志文件太大的时候，Redis 会重写并压缩日志文件；
* AOF 的日志文件内容更容易让人理解

## AOF 缺点

* AOF 产生的日志文件会非常大；
* 在「每个命令后 fsync」策略下，AOF 可能会非常得慢。

## 参考资料

* [Redis Persistence](https://redis.io/docs/manual/persistence)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.fantasticmao.cn/tech/shu-ju-ku/redis/redis-chi-jiu-hua-ji-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
