乐闻世界logo
搜索文章和话题

What are the differences between Redis RDB and AOF persistence? How to choose?

2月19日 19:37

Redis provides two persistence methods: RDB (Redis Database) and AOF (Append Only File). Each has its advantages and disadvantages, and they can also be used together.

RDB Persistence

Working Principle: RDB generates point-in-time snapshots of the dataset at specified time intervals. Redis forks a child process that writes the in-memory data to a temporary file, then replaces the old RDB file with this temporary file.

Advantages:

  1. Compact File: RDB files are compressed binary files, small in size, suitable for backup and disaster recovery
  2. Fast Recovery: RDB file recovery is faster than AOF because it doesn't need to re-execute commands
  3. Low Performance Impact: RDB is executed by a child process, with minimal impact on the main process
  4. Suitable for Cold Backup: RDB files can be easily transferred to remote servers for backup

Disadvantages:

  1. Data Loss Risk: If Redis crashes suddenly, all data after the last snapshot may be lost
  2. Fork Operation Time-Consuming: When data volume is large, forking a child process may block the main process
  3. Poor Real-time: RDB is based on time intervals and cannot achieve real-time persistence

Configuration Parameters:

  • save <seconds> <changes>: Set save conditions, e.g., save 900 1 means save if at least 1 key changes within 900 seconds
  • rdbcompression yes: Whether to compress RDB files
  • rdbchecksum yes: Whether to checksum RDB files

AOF Persistence

Working Principle: AOF records every write operation command received by the server, appending these commands to the end of the AOF file. When Redis restarts, it re-executes the commands in the AOF file to restore data.

Advantages:

  1. High Data Security: AOF can be configured to sync every second or on every write operation, with low data loss risk
  2. High Readability: AOF files are in text format and can be manually viewed and modified
  3. Automatic Rewrite: When AOF files become too large, they are automatically rewritten to compress file size

Disadvantages:

  1. Large File Size: AOF files are typically larger than RDB files
  2. Slow Recovery: Need to re-execute all commands, recovery speed is slower than RDB
  3. High Performance Impact: Every write operation needs to sync to disk, with significant performance impact

Configuration Parameters:

  • appendonly yes: Enable AOF persistence
  • appendfsync always/everysec/no: Sync strategy
    • always: Sync on every write operation, safest but worst performance
    • everysec: Sync once per second, recommended configuration
    • no: Let the OS decide when to sync, best performance but least secure
  • auto-aof-rewrite-percentage 100: AOF file rewrite growth percentage
  • auto-aof-rewrite-min-size 64mb: AOF file rewrite minimum size

RDB + AOF Hybrid Persistence

After Redis 4.0, hybrid persistence is supported. When enabled, AOF rewrite writes RDB content to the beginning of the AOF file, and subsequent incremental commands continue to be appended in AOF format. This ensures both data security and improves recovery speed.

Configuration:

  • aof-use-rdb-preamble yes: Enable hybrid persistence

Selection Recommendations

  1. If data security requirements are low: Use only RDB for better performance
  2. If data security requirements are high: Use only AOF, configured with appendfsync everysec
  3. If both performance and security are needed: Use RDB + AOF hybrid persistence
  4. If data volume is large: Recommend using RDB, as AOF recovery speed is too slow

In actual production environments, typically both RDB and AOF are enabled, or hybrid persistence is used, to balance performance and data security.

标签:Redis