Introduction to Redis

Redis Persistence

Redis has four different persistence modes made up of:

  • RDB a periodical snap-shot in time
  • AOF data is saved on every write command
  • No on-disk persistence
  • Both RDB and AOF

RDB

Advantages

  • Compact single-file snapshot of the Redis data
  • Restores easily from a single-file snapshot backup
  • Faster restart when loading large datasets

Disadvantages

  • Potential data loss between snapshots
  • Needs to fork child process to create snapshot, forking may take a lot of time if large dataset and slow processor preventing client access in the meantime.

AOF (Append Only File)

Advantages

  • More durable, setting fsync to 1 second means at most 1 second would be lost if a failure occurs
  • AOF log append-only, no seeks and if last write command fails, easy to fix with the redis-check-aof utility
  • AOF is automatically written in background, allowing Redis to continue to serve clients
  • Contains a log of all operations of each write operation, easy to export format

Disadvantages

  • AOF files are usually bigger than equivalent RDB files
  • Can be slower than RDB depending on fsync policy

Configuring redis.conf

Exercise: Saving RDB snapshot & redis.conf parameters

The SAVE will save a RDB snapshot of the running Redis instance to disk, the BGSAVE does a similar RDB snaphot only running in the background while the active Redis instance still can respond to client commands.

127.0.0.1:6379> SAVE
OK
127.0.0.1:6379> BGSAVE
Background saving started
127.0.0.1:6379>
    

Listed below is a snippet from a Redis 3.0 configuration file some of the important parameters and default values for setting both RDB and AOF persistence in Redis.

# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379


# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0

# Specify the server verbosity level.
loglevel notice

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output.
logfile ""

save 900 1
save 300 10
save 60 10000

# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes

# The filename where to dump the DB
dbfilename dump.rdb

# The working directory.
dir /.

appendonly no

# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"

# Max execution time of a Lua script in milliseconds.
lua-time-limit 5000
    

Operating a live Redis instance

Critical Commands for configuring a running Redis instance

Exercise: Configuration & other Commands

The CONFIG SET allows you to set a configuration parameter at runtime while the CONFIG GET command retrieves a parameter.

127.0.0.1:6379> CONFIG GET appendonly
1) "appendonly"
2) "no"
127.0.0.1:6379> CONFIG GET asfdsf
(empty list or set)
127.0.0.1:6379> CONFIG GET port
1) "port"
2) "6379"
127.0.0.1:6379> CONFIG SET appendonly yes
OK
     

The MONITOR command will display all incoming client commands to the monitored Redis instance

127.0.0.1:6379> MONITOR
OK
1418623217.197721 [0 127.0.0.1:53288] "DBSIZE"
1418623241.973480 [0 127.0.0.1:53288] "SET" "Book:3" "Pride and Prejudice"
1418623264.670176 [0 127.0.0.1:53288] "GET" "Book:3"
     

The BGREWRITEAOF command will compact and rewrite the AOF log file in the background while the instance can still accept client commands.

127.0.0.1:6379> BGREWRITEAOF 
Background append only file rewriting started
127.0.0.1:6379> 
     

References and Resources

  1. From the redis.io website, topic on http://redis.io/topics/persistence