Redis Persistence is the mechanism that allows storing Redis data on the hard disk to ensure data is not lost during Redis server restarts or in case of failures. Redis supports two main persistence mechanisms: RDB (Redis Database File) and AOF (Append-Only File).
RDB (Redis Database File)
- RDB is a backup mechanism that creates a snapshot of the Redis database at a specific point in time.
- When using RDB, Redis saves the data into a file with the .rdb extension.
- RDB can be configured to perform backups periodically or when significant events occur, such as a certain number of key changes within a specific timeframe.
- RDB is a fast and efficient backup mechanism as it uses a complete process to save data.
AOF (Append-Only File)
- AOF is a backup mechanism that writes all database operations to a log file.
- When using AOF, Redis writes every write command (SET, DELETE, etc.) to the log file.
- AOF can be configured to log data based on a time-based rotation or an event-based rotation.
- AOF can be used to recover data when Redis restarts by replaying all the operations recorded in the log file.
You can choose to use RDB, AOF, or both, depending on your application's requirements and environment. RDB is commonly used for periodic backups and consumes fewer resources, while AOF is often used to ensure durability and higher reliability. Some applications use both mechanisms to ensure optimal security and recovery capabilities.