Junior — Middle
How to implement data persistence in Redis for durability?
sobes.tech AI
Answer from AI
For long-term data storage in Redis, you need to use persistence mechanisms supported by Redis:
- RDB (Redis Database Backup) — periodic snapshots of the database on disk.
- AOF (Append Only File) — recording each data modification operation in a log file, which can be replayed upon restart.
In Python, using the redis library, you can simply save data, and the configuration for long-term storage is handled on the Redis server side.
Example of saving data:
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('key', 'value')
For long-term storage, ensure that AOF is enabled or RDB snapshots are configured regularly in the Redis configuration. This guarantees data safety upon server restart.