Using the Redis NoSql database with .NET Part 17: persistence
May 10, 2017 Leave a comment
Introduction
In the previous post we looked at the security features of Redis. We saw that Redis doesn’t come with the highly granular security features of a full-fledged relational database like SQL Server or Oracle. It offers password passed authentication where the client needs to authenticate with a password before issuing commands. There’s no separate username and we cannot set granular rules like userA can only access this and this resource. Another security feature is command obfuscation where we can change how a command is called. The CONFIG command can be a “dangerous” one that we don’t want just any user to be able to call. We can instead provide another name, possibly a cryptic one, for it so that CONFIG must be called by that name instead.
In this post we’ll discuss the persistence options in Redis.
Persistence
We already know that the primary storage of Redis is the memory which makes it extremely fast. We also mentioned before that Redis periodically saves its data to disk. The primary way of backing up the records is called snapshotting. Snapshotting is configured in the SNAPSHOTTING section of the Redis configuration file. Snapshotting is configured with two integers: the number of seconds and number of changes. Redis will persist the changes if they have occurred within the specified number of seconds. The examples in the default config file include the following:
save 900 1
save 300 10
save 60 10000
The first setting says that Redis will save the changes after 900 seconds if 1 change has occurred.
Therefore note that there’s some blind spot where the data changes can be lost in case the Redis DB process is abruptly shut down for some reason like an outage.
If you only use Redis for caching then you may not even need any persistence. In that case you could remove all save settings from the config file.
An alternative of snapshotting is the append-only mode which is configured in the APPEND ONLY MODE section. If append-only is turned on then it will maintain a file where all the commands are saved. Append-only provides a higher level of durability than snapshotting but it will also slow down the database. The two modes, i.e. snapshotting and append-only can be used simultaneously.
The flag called “appendonly” must be set to yes to turn on append-only mode. The file where the commands are saved is specified by the appendfilename parameter which is by default set to appendonly.aof. So if you turn on append-only and start writing modifications to the database then you’ll see that the appendonly.aof file is created in the Redis root directory. You can open that file in a text editor to check the format. It looks like this with a GET and a SADD command:
*3
$3
SET
$16
append-only-test
$5
value
*3
$4
SADD
$15
persistence-set
$5
hello
Redis obviously understands this format and will load this file on start-up if append-only is enabled.
Another important setting related to append-only is appendfsync. It specifies the frequency to persist the Redis commands to disk. It is really a setting for the operating system that Redis is running on. appendfsync has three modes: always, everysec and no. The documentation in the config file is pretty straightforward:
no: don’t fsync, just let the OS flush the data when it wants. Faster.
always: fsync after every write to the append only log . Slow, Safest.
everysec: fsync only one time every second. Compromise.
Therefore in case your data needs a better level of durability than snapshotting then the append-only mode is a safer option.
We continue in the next post with caching.
You can view all posts related to data storage on this blog here.