Redis · Chapter 21 of 26

Persistence

By the end of this chapter you can say exactly what a crash takes from Redis, take snapshots (RDB) and keep an append-only file (AOF), choose how often that file is synced, turn the AOF on in a running server without losing a key, and choose the right persistence for each of GullyXI's Redis servers. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Redis in production
  • 3 tasks
  • Redis
  • In the all labs plan

The first 2 chapters of Redis are free. This one comes with the all labs plan.

Why you need this

Chapter 1 said memory has two costs, and this chapter is about the second: memory is lost when the server stops. GullyXI's Redis now holds more than copies. Sessions live only there (chapter 12), the points queue holds jobs nobody else has (chapter 14), and Venkat wants fans' wallets, in paise, in a Redis of their own, where a lost change is a fan's lost money.

In March Gurpreet restarted the sessions Redis for a security patch, and every fan was logged out at once: the app's "log in again" screen, two lakh times, an hour before a match. Then she turned on the append-only file in its config and restarted again, and the Redis came back empty. Farhan wants every Redis at GullyXI to have a persistence setting someone chose on purpose.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    wallets.conf: wallets that survive a crash

    Write wallets.conf, redis.conf lines for Venkat's wallets Redis: the append-only file on, and an fsync of it on every write (appendfsync always). Leave out port, dir and the rest: the Check sets them.

    The Check starts a Redis from your file and reads its appendonly (yes) and appendfsync (always). Then it changes 50 wallets with HINCRBY, kills Redis as a power cut would, starts another Redis from your file on the same folder, and reads all 50 balances back, the very last change included.

  2. Task 2

    snapshot.js: a snapshot taken from code

    In snapshot.js, snapshot(redis) starts a snapshot in the background with BGSAVE, waits until INFO persistence says rdb_bgsave_in_progress:0, throws an Error if rdb_last_bgsave_status is not ok, and returns LASTSAVE (unix seconds) read after the save.

    When another snapshot is already running, BGSAVE fails with an error whose message has in progress in it: then wait for that save to finish and start yours, so writes made during it are saved too. Rethrow any other error.

    The Check slows the save down on purpose, so returning before it is done shows. It calls snapshot once, then again while a save it started itself is running and after one more write, then kills Redis and starts another on the same folder: every key must be there.

  3. Task 3 · interview question

    Interview: enable-aof.js, the AOF turned on safely

    The sessions Redis runs on snapshots only (save 60 1 and appendonly no in its sessions.conf). Setting appendonly yes in the conf and restarting brought it back empty, because with the AOF on Redis loads only the AOF, and there was none.

    In enable-aof.js, enableAof(redis) turns the AOF on in the running server with CONFIG SET, waits until INFO persistence says aof_rewrite_in_progress:0 and aof_rewrite_scheduled:0 (throwing if aof_last_bgrewrite_status is not ok), then writes the running settings into the server's own conf file so a restart keeps the AOF on.

    The Check starts a Redis from a sessions.conf with 300 sessions, calls enableAof, reads the conf file for the line appendonly yes, adds a session, kills Redis and starts it from the same sessions.conf on the same folder: all 301 sessions must be there.

    Farhan will ask: which persistence would you choose for the cache, the sessions and the leaderboards, and why?

What you will learn

  • What a crash loses: an in-memory Redis, killed and started again.
  • RDB snapshots: save points, BGSAVE, LASTSAVE and INFO persistence.
  • CONFIG GET, CONFIG SET and CONFIG REWRITE: settings, while Redis runs.
  • The AOF: appendonly, Redis 7's appendonlydir, appendfsync always, everysec and no, BGREWRITEAOF.
  • Turning the AOF on safely, and why a restart can come back empty.
  • Choosing, for each kind of data.

More in Redis in production

Build "Persistence" yourself

GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Redis are free. This one comes with the all labs plan.