12
Redis - Persistence Eng. Ismail Enjreny Email: [email protected]

Redis Persistence

Embed Size (px)

Citation preview

Redis - PersistenceEng. Ismail Enjreny

Email: [email protected]

Redis Persistence

• Redis provides a different range of persistence options:

• The RDB persistence performs point-in-time snapshots of your dataset at specified intervals.

• The AOF persistence logs every write operation received by the server, that will be played again at serverstartup, reconstructing the original dataset. Commands are logged using the same format as the Redis protocolitself, in an append-only fashion. Redis is able to rewrite the log on background when it gets too big.

• If you wish, you can disable persistence at all, if you want your data to just exist as long as the server isrunning.

• It is possible to combine both AOF and RDB in the same instance. Notice that, in this case, when Redisrestarts the AOF file will be used to reconstruct the original dataset since it is guaranteed to be the mostcomplete.

Redis Persistence – RDB (Snapshotting)

• Redis snapshotting is the simplest Redis persistence mode

• It produces point-in-time snapshots of the dataset when specific conditions are met, for instance if theprevious snapshot was created more than 2 minutes ago and there are already at least 100 new writes,a new snapshot is created

• This conditions can be controlled by the user configuring the Redis instance, and can also be modified atruntime without restarting the server

• Snapshots are produced as a single .rdb file that contains the whole dataset.

Redis Persistence – RDB advantages

• RDB is a very compact single-file point-in-time representation of your Redis data

• RDB is very good for disaster recovery, being a single compact file can be transferred to far data centers

• RDB maximizes Redis performances since the only work the Redis parent process needs to do in orderto persist is forking a child that will do all the rest. The parent instance will never perform disk I/O oralike

• RDB allows faster restarts with big datasets compared to AOF

Redis Persistence – RDB disadvantages

• RDB is NOT good if you need to minimize the chance of data loss in case Redis stops working

• RDB needs to fork() often in order to persist on disk using a child process

• Fork() can be time consuming if the dataset is big, and may result in Redis to stop serving clients for somemillisecond or even for one second if the dataset is very big and the CPU performance not great

Redis Persistence – How to RDB

• Open redis.windows.conf file

• Go to SNAPSHOTTING section

• save <seconds> <changes>

• Will save the DB if both the given number of seconds and the given number of write operations against the DBoccurred.

• The filename where to dump the DB

• dbfilename dump.rdb

save 900 1 after 900 sec (15 min) if at least 1 key changed

save 300 10after 300 sec (5 min) if at least 10 keys changed

save 60 10000after 60 sec if at least 10000 keys changed

Redis Persistence – AOF

• The Append Only File, usually called simply AOF, is the main Redis persistence option

• The way it works is

• Every time a write operation that modifies the dataset in memory is performed, the operation gets logged

• The log is produced exactly in the same format used by clients to communicate with Redis

• At restart Redis re-plays all the operations to reconstruct the dataset

Redis Persistence – AOF advantages

• Using AOF Redis is much more durable, you can have different fsync policies:

• no fsync at all

• fsync every second

• fsync at every query

• The AOF log is an append only log, so there are no seeks, nor corruption problems if there is a poweroutage

• Even if the log ends with an half-written command for some reason (disk full or other reasons) the redis-check-aof tool is able to fix it easily

• Redis is able to automatically rewrite the AOF in background when it gets too big

• AOF contains a log of all the operations one after the other in an easy to understand and parse format

Redis Persistence – AOF disadvantages

• AOF files are usually bigger than the equivalent RDB files for the same dataset

• AOF can be slower than RDB depending on the exact fsync policy

Redis Persistence – How to AOF

• Open redis.windows.conf file

• Go to APPEND ONLY MODE section

• Change appendonly no to appendonly yes

• You change file name by altering the following option

• appendfilename “appendonly.aof”

Backing up Redis data

• Redis is very data backup friendly since you can copy RDB files while the database is running

• The RDB is never modified once produced, and while it gets produced it uses a temporary name and isrenamed into its final destination atomically using rename(2) only when the new snapshot is complete

• Take a backup from .AOF in case “appendonly yes”

• In the case both AOF and RDB persistence are enabled and Redis restarts the AOF file will be used toreconstruct the original dataset since it is guaranteed to be the most complete