Using the Redis NoSql database with .NET Part 17: persistence

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.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: