What is RDB in Redis: Explanation and Usage
RDB in Redis stands for Redis Database Backup, a snapshotting method that saves the dataset to disk at specified intervals. It creates a compact binary file representing the data at a point in time, allowing Redis to restore data after a restart or failure.How It Works
RDB in Redis works like taking a photo of your data at certain moments. Imagine you have a notebook where you write down your daily expenses. Every hour, you take a clear photo of the notebook page to save a record. Similarly, Redis saves a snapshot of all its data to a file on disk at configured times.
This snapshot is a compact binary file that contains all the keys and values exactly as they were when the snapshot was taken. If Redis stops or crashes, it can load this snapshot file to restore the data to the state it was in at the last snapshot.
This method is efficient because it doesn’t save data continuously but only at intervals, which reduces disk usage and speeds up Redis operations during normal runtime.
Example
This example shows how to configure Redis to save an RDB snapshot every 60 seconds if at least one key changed, and how to trigger a manual snapshot.
save 60 1 # To manually create an RDB snapshot, use the Redis CLI command: SAVE
When to Use
Use RDB snapshots when you want fast recovery with minimal impact on Redis performance. It is ideal for applications where losing a few seconds or minutes of data is acceptable, like caching or session storage.
RDB is also useful for backups because the snapshot file is compact and easy to move or copy. However, if you need to save every change instantly, consider Redis Append Only File (AOF) persistence instead.
Key Points
- RDB creates point-in-time snapshots of Redis data to disk.
- It saves data at configured intervals or manually on demand.
- RDB files are compact and fast to load.
- It is suitable for use cases where some data loss is acceptable.
- RDB has less runtime overhead compared to other persistence methods.