Redis is a in-memory storage system. Primary memory is volatile i.e., data is lost when we restart our computer. Some people want to use redis just like an DBMS which keeps its data stored permanently. Redis does provide some mechanism to make the data persistance(permanent).
There are three different ways to make Redis persistant: RDB, AOF and SAVE command.
- RDB Mechanism: RDB makes a copy of all the data in memory and stores them in secondary storage(permanent storage). This happens in a specified interval. If your computer restarts due to power failure then there is chance that you will loose data that are set after RDB’s last snapshot.
- AOF: AOF logs all the write operations received by the server. Therefore everything is persistance. The problem with using AOF is that it writes to disk for every operation and it is a expensive task and also size of AOF file is large than RDB file.
- SAVE Command: You can force redis server to create a RDB snapshot anytime using the redis console client SAVE command.
You can use AOF and RDB together to get best persistance result.
Setting Up Data Persistance
If you can live with few hours/minutes of data loss then its always better to only use RDB mechanism only. By default redis.conf file is enabled to make the dataset save every minute into a file named as dump.rdb. You can also manually trigger the SAVE command anytime to dump latest dataset into the dump.rdb file.
If every single bit of data is important then you should use AOF mechanism. By default AOF is disabled. To enable it go to your redis.conf file and edit
Now redis will append all operations into a file called as appendonly.aof. Redis has internal mechanism to clean this file regularly. The size of this file will never exceed your filesystem size because harddisk always has more space then primary memory. Redis puts everything into appendonly.aof that has been put in the primary memory. This logic is same for dump.rdb file also.
Its recommend that if you are using AOF then you should also use RDB. Don’t use AOF alone. There are little chances for appendonly.conf file corruption as log files tend to be corrupted sometimes. When you are using AOF and RDB together redis automatically manages them to reduce filesystem write overwhelming.
For more information of Redis persistance refer this official documentation.
Leave a Reply