Redis in Action

Redis in Action






Total Views
Views on SlideShare
Embed Views



5 Embeds 237 224 7 3 2 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Redis in Action Redis in Action Document Transcript

  • 1 Redis in Action By Josiah L. Carlson To prepare for building software with Redis you need to know how to keep your data safe, even in the face of system failure. In this article based on chapter 4 of Redis in Action, you’ll see how by combining replication and append-only files, you can configure Redis to be resilient against system failures. You may also be interested in… Handling System FailuresIn order to be able to handle system failures in Redis, you need to prepare yourself for the failure. A database isonly as useful as how well it can keep your data from being lost. Redis is software, and it runs on hardware. Even ifboth were designed perfectly and can’t fail, power can fail, generators can run out of fuel, and batteries can run outof power. You spend a lot of time preparing for potential system failures. This article talks about what you can dowhen failure does happen.Verifying snapshots and append-only filesWhen confronted with system failures, you have tools to help you recover when either snapshotting or append-onlyfile logging had been enabled. Redis includes two command-line applications for testing the status of a snapshotand an append-only file. These commands are redis-check-aof and redis-check-dump. If you run either command without arguments, you will see the very basic help that is provided, which isincluded below. $ redis-check-aof Usage: redis-check-aof [--fix] <file.aof> $ redis-check-dump Usage: redis-check-dump <dump.rdb> $If you provide --fix as an argument to redis-check-aof, the command will fix the file. Its method to fix anappend-only file is very simple; it scans through the provided AOF, looking for an incomplete or incorrectcommand. Upon finding the first bad command, it trims the file to just before that command would have beenexecuted. For most situations, this will discard the last partial write command. Unfortunately, there is no currently supported method of repairing a corrupted snapshot. While there is thepotential to discover where the first error had occurred, because the snapshot itself is compressed, an error partway through the dump has the potential to make the remaining parts of the snapshot unreadable. It is for thesereasons that we would generally recommend keeping multiple backups of important snapshots, and calculating theSHA1 or SHA256 hashes to verify content during restoration (modern Linux and Unix platforms will have anavailable sha1sum and sha256sum command-line application for generating and verifying these hashes). CHECKSUMS AND HASHES Redis versions including 2.6 and later include a CRC64 checksum of the snapshot as part of the snapshot. The use of a CRC-family checksum is useful to discover errors that are typical in some types of network transfers or disk corruption. The SHA family of cryptographic hashes is much better suited for discovering arbitrary errors. To the point, if you calculated the CRC64 of a file, then flipped any number of bits inside the file, you could later flip For Source Code, Sample Chapters, the Author Forum and other resources, go to
  • 2 a subset of the last 64 bits of the file to produce the original checksum. There is no currently known method for doing the same thing with SHA1 or SHA256. Once we have verified that our backups are what we had saved before, and we have corrected the last write toAOF as necessary, we may need to replace a Redis server.Replacing a failed masterWhen you are running a group of Redis servers with replication and persistence, there may be a time when somepart of your infrastructure stops working for one reason or another. Maybe you get a bad hard drive, maybe badmemory, or maybe the power just went out. Regardless of what could cause the system to fail, you will eventuallyneed to replace a Redis server. Let’s look at an example scenario involving a master, a slave, and needing toreplace the master. Machine A is running a copy of Redis that is acting as the master, and Machine B is running a copy of Redis thatis acting as the slave. Unfortunately, machine A has just lost network connectivity for some reason that you havenot yet been able to diagnose. However, you have another machine C with Redis installed that you would like touse as the new master. Your plan is going to be very simple. You will tell machine B to produce a fresh snapshot with SAVE. You willthen copy that snapshot over to machine C. After the snapshot has been copied into the proper path, you will startRedis on machine C. Finally, you will tell machine B to become a slave of machine C. You can see some examplecommands to make this possible on this hypothetical set of systems in listing 1.Listing 1 A sample sequence of commands for replacing a down master node user@vpn-master ~:$ ssh root@machine-b.vpn #1 Last login: Wed Mar 28 15:21:06 2012 from ... #1 root@machine-b ~:$ redis-cli #2 redis> SAVE #3 OK #3 redis> QUIT #3 root@machine-b ~:$ scp #4 > /var/local/redis/dump.rdb machine-c.vpn:/var/local/redis/ #4 dump.rdb 100% 525MB 8.1MB/s 01:05 #4 root@machine-b ~:$ ssh machine-c.vpn #5 Last login: Tue Mar 27 12:42:31 2012 from ... #5 root@machine-c ~:$ sudo /etc/init.d/redis-server start #5 Starting Redis server... #5 root@machine-c ~:$ exit root@machine-b ~:$ redis-cli #6 redis> SLAVEOF machine-c.vpn 6379 #6 OK #6 redis> QUIT root@machine-b ~:$ exit user@vpn-master ~:$ #1 Connect to machine B on our vpn network #2 Start up the command line redis client to do a few simple operations #3 Start a SAVE, and when it is done, QUIT so that we can continue #4 Copy the snapshot over to the new master, machine C #5 Connect to the new master and start Redis #6 Tell machine B’s Redis that it should use C as the new masterMost of these commands should be familiar to those of you with experience using and maintaining UNIX or Linuxsystems. The only interesting things in the commands being run here is that you can cause a save to occur onMachine B by running a command, and you later set up Machine B to be a slave of Machine C by simply running acommand. As an alternative to creating a new master, you may want to turn the slave into a master and create a newslave. Either way, Redis will be able to pick up where it left off, and your only job from then on is to update yourclient configuration to read and write to the proper servers, and optionally update the on-disk server configurationif you need to restart Redis. For Source Code, Sample Chapters, the Author Forum and other resources, go to
  • 3SummaryWe showed you how you can help keep Redis performing well while dealing with failures. The use of replication andappend-only files can go a long way towards keeping your data safe. For Source Code, Sample Chapters, the Author Forum and other resources, go to View slide
  • 4Here are some other Manning titles you might be interested in: Big Data Nathan Marz Hadoop in Practice Alex Holmes HBase in Action Nick Dimiduk and Amandeep KhuranaLast updated: July 6, 2012 For Source Code, Sample Chapters, the Author Forum and other resources, go to View slide