CodePark/ questions / Persistence (RDB & AOF)
START PRACTICINGPRACTICE
Backend question · Redis & caching › Persistence (RDB & AOF)

Compare RDB and AOF persistence. When would you use each, or both?

Use RDB when losing some recent data is acceptable. Use AOF when you need to lose as little as possible. Use both to get the best of each.

RDB saves a snapshot: a copy of the whole dataset at one moment. It writes it to a small binary file. Redis does this on a schedule (the save rules) or when you run BGSAVE.

  • Pros: the files are small, restarts are fast, and the files make good backups.
  • Cons: you lose everything written since the last snapshot.

AOF (Append Only File) logs every write command. On restart, Redis replays the log to rebuild the data. With appendfsync everysec, you lose at most about one second of writes.

  • Pros: very little data loss.
  • Cons: the file is larger and restarts are slower. Redis rewrites the log from time to time to keep it small.

With both turned on, use AOF for recovery and RDB for backups. On restart, Redis loads from the AOF, because it is more complete. A pure cache can run with persistence turned off.

  • RDB takes periodic snapshots: the file is compact and restarts are fast, but recent writes are lost.
  • AOF logs every write, has a configurable fsync setting, and makes a bigger file.
  • With everysec, AOF loses about one second of writes at most.
  • You can use both together, and Redis loads from the AOF on restart.
  • A pure cache can turn persistence off.