search by tags

for the user

adventures into the land of the command line

using redis as a persistent data store

redis is a key value store and by default, it stores those keys and values in memory. that means, when you switch off the computer, those keys and values disappear.

redis can be configured to persist data on restarts by writing what’s in memory to disk. it has two different modes to achieve this, RDB and AOF, which are both good on their own, and even better when combined.

RDB is sort of like a database dump. redis takes a snapshot of what is currently in memory and saves it to an .rdb file on disk. when the host is rebooted, the contents of the file are reloaded into memory and awaaaaaaay we goooooooo. there’s a bunch of configs that relate to rdb stuff. in your redis config file in the section named SNAPSHOTTING, you can put some lines like this

$ sudo vim /etc/redis.conf

save 86400 1
rdbcompression yes
dbfilename dump.rdb
dir /mnt/data/redis/redis_instance1

redis will dump the dataset to disk every 86400 seconds if at least 1 key has changed, to a file named dump.rdb in this directory and compress it.

every dump will have an incremented filename, and if you want to preserve disk space, you can make a cron to clean up old files.

$ crontab -l
0 3 * * * find /mnt/data/redis/*/*.rdb -ctime +14 -exec rm -f {} \;

AOF is the other method, it stands for append-only file, which is sort of like the binary log on a MySQL database. it records every single command in the .aof file before/after the command is run on the dataset. redis remembers the size of the AOF file after the latest rewrite and if the current size is bigger than the specified percentage (in the config file), the rewrite is triggered. and so on a rewrite, redis runs thru the file and executes every single command, effectively rebuilding the dataset to the last point in time the .aof file was updated.

in the same config file in the section called APPEND ONLY MODE, you could put some lines like this

$ sudo vim /etc/redis.conf

appendonly  yes
appendfsync everysec
no-appendfsync-on-rewrite  yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64

the appendonly.aof file goes into whatever you’ve specified for ‘dir’ in the SNAPSHOTTING section. enabling appendonly causes redis to use appendonly rewites as opposed to the rdb dumps for rebuilding the dataset on restarts.

so rdb is a point in time snapshot feature and aof is a replay from operation log feature. the redis site has a list of really good pros/cons for both and some suggestions about using them.

i quite like it.