Redis Persistence
Redis is known for its fast in-memory data storage capabilities. However, in certain scenarios, it is important to ensure data durability even in the event of a server restart or power outage. This is where Redis persistence comes into play.
Redis provides two mechanisms for persistence: RDB persistence and AOF persistence.
RDB Persistence
RDB (Redis Database) persistence performs point-in-time snapshots of the dataset at configurable intervals. This snapshot is written to a binary file on disk. In the event of a server restart, Redis can load the latest snapshot and continue serving the data from where it left off.
To enable RDB persistence, you can configure the save
directive in the Redis configuration file. For example, to save the dataset to disk every 5 minutes if at least 1 key changed, you can set the following configuration:
1save 300 1
AOF Persistence
AOF (Append-Only File) persistence logs every write operation to a file on disk. This log can be replayed to reconstruct the dataset in the event of a server restart. AOF persistence provides better data durability compared to RDB persistence, but at the cost of increased disk usage and slower write performance.
You can enable AOF persistence by setting the appendonly
configuration directive in the Redis configuration file to yes
.
Mixed Persistence
In addition to the individual persistence modes, Redis also supports mixed persistence. With mixed persistence, both RDB snapshots and AOF logs are used for data durability. In the event of a server restart, Redis can first load the latest RDB snapshot and then replay the AOF log to reconstruct the dataset up to the point of the last snapshot.
To enable mixed persistence, you need to configure both the save
directive for RDB persistence and the appendonly
directive for AOF persistence.