Dragonfly Cloud is now available in the AWS Marketplace - learn more

Question: How can you configure Redis to serve as a persistent cache?

Answer

Redis can serve as both a caching system and a data store, thus providing the ability for persistent storage. Here's an overview of how you can configure Redis to function as a persistent cache. There are two primary persistence options in Redis - RDB (Redis Database Backup) and AOF (Append Only File).

  1. RDB: The RDB persistence option performs point-in-time snapshots of your dataset at specified intervals.
# Example of enabling RDB configuration in redis.conf save 900 1 # Snapshot after 900 seconds if at least 1 key changed save 300 10 # Snapshot after 300 seconds if at least 10 keys changed save 60 10000 # Snapshot after 60 seconds if at least 10000 keys changed rdbcompression yes # Enable compression of RDB files
  1. AOF: The AOF persistence option logs every write operation received by the server, that can be replayed at server startup, reconstructing the original dataset.
# Example of enabling AOF configuration in redis.conf appendonly yes # Enable AOF appendfsync everysec # fsync every second

You can use both the RDB and AOF options together for maximum data safety - where the AOF file will be used to reconstruct the database when a server restarts and the RDB file will be used for point-in-time backups. Note: This requires more disk space and can have a slight performance impact during writes due to fsync calls.

# Using both RDB and AOF together in redis.conf save 900 1 rdbcompression yes appendonly yes appendfsync everysec

Keep in mind, always make sure the dir option in your redis.conf points to a secure and reliable directory path. This is where your RDB / AOF file will be stored.

Remember that while Redis provides these mechanisms for persisting data, it is primarily an in-memory datastore and there are cases where data can be lost (e.g., power loss before data is saved to disk). Therefore, it's important to understand your application needs and reliability requirements before relying on Redis for critical persistent data.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost