Course contentsShow
← Databases & SQL
Lesson 1308 of 1,95312. Key-Value and Caching EnginesFree lesson

Persistence vs Performance

Configure RDB snapshots and AOF logs to balance durability with throughput.

In this lesson, you will learn how to calibrate Redis persistence mechanisms to achieve the right balance between data safety and high-throughput performance.

Redis is primarily an in-memory store, meaning if the process crashes, data vanishes. To prevent this, we use RDB (Redis Database) snapshots and AOF (Append Only File) logs. However, writing data to disk is an I/O-intensive operation that creates a direct tug-of-war between durability and latency.

RDB: The Periodic Snapshot

RDB takes a point-in-time snapshot of your entire dataset. It is highly performant because Redis forks a child process to write the data to disk, leaving the main thread free to handle commands.

  • Trade-off: If you configure snapshots every 5 minutes (save 300 1), you risk losing up to 5 minutes of data if the system crashes. Increasing the frequency improves durability but consumes CPU cycles for frequent disk I/O.

AOF: The Incremental Log

AOF records every write operation received by the server. It is significantly more durable than RDB because it logs changes as they happen.

  • Trade-off: You can control the appendfsync policy. Setting it to always guarantees maximum safety but forces a disk sync on every command, which cripples performance. Setting it to everysec strikes a balance, committing writes to disk once per second, which keeps latency low while ensuring you never lose more than one second of data.

Finding the Balance

In high-throughput environments, the goal is to minimize the "blocking" cost of disk writes. Most production systems use a hybrid approach:

# Example configuration snippet
save 900 1      # Snapshot every 15 mins if 1 key changed
appendonly yes
appendfsync everysec 

By using appendfsync everysec, you shift the heavy lifting of disk I/O into the background. Your application experiences the speed of RAM for its operations, while the persistence layer provides a safety net that is "good enough" for most use cases without saturating your disk throughput.

Key Takeaway: Persistence is a performance tax; to minimize it, favor asynchronous background snapshots (RDB) and non-blocking, periodic log flushing (AOF) over synchronous disk writes.