Back to logs

Coding4 min read ยท Today

Your Plan B Needs Practice: Test Failover and Backups

PostgreSQL elephant logo floating on a light grid background

Benjamin Cane opens his latest post on failover with an old line about backups: “Your backups are only as good as the last time you restored them.” Then he says the same applies to failover.

I agree. This post turns that idea into work you can do this week: one restore drill with real commands I ran myself, and a simple way to make failover part of your normal releases.

Why a Ready Standby Can Still Fail

Cane’s example is a warm standby. The app is running, the servers exist and the health checks pass. It still might not take traffic.

While an environment sits idle, database connections go stale, firewall rules change and credentials expire. Nobody notices until real traffic arrives, and that usually happens during an outage.

Two fixes follow from his post. Make health checks use the same dependencies, network paths and credentials as real traffic. Give persistent database connections an idle timeout or a maximum connection lifetime.

The Google SRE book says it more bluntly in the epigraph of its chapter on testing for reliability: “If you haven’t tried it, assume it’s broken.”

Carousel cover titled Your Plan B Needs Practice with a photo of a railway track switch and a note reading If you haven't tried it, assume it's broken
Cover of today's shahari.me carousel. Railway photo by daxiaob on Pixabay.

What You Will Need

  • A recent backup made with pg_dump in custom format. The idea works for any database. I am using PostgreSQL because its docs spell out every step.
  • A machine or container where a restore cannot touch production.
  • One query you trust to check the data, like a row count on your most important table.
  • A slot in your calendar, so the drill actually happens.

The Restore Drill, Step by Step

I ran these exact commands on PostgreSQL 16 in a sandbox, with a test table of 5,000 rows. They follow the SQL Dump and pg_restore pages of the PostgreSQL docs.

Step 1: Take a Custom Format Dump

pg_dump -Fc shop > shop.dump

Custom format gives you an archive that pg_restore can restore, and you can list what is inside it later.

Step 2: Create an Empty Database From template0

createdb -T template0 shop_restore_test

The docs clone the new database from template0, not template1, so it starts empty.

Step 3: Restore Into It

pg_restore -d shop_restore_test shop.dump

No -C here. The docs connect straight to the database you are restoring into.

Step 4: Check It Worked

psql -X -d shop_restore_test -c 'select count(*) from orders;'
pg_restore -l shop.dump

Compare the count with production, or with a number you logged when the backup was made. The second command lists the table of contents of the archive.

Step 5: Run ANALYZE

psql -X -d shop_restore_test -c 'ANALYZE;'

The docs say it is wise to run ANALYZE after restoring a backup, so the query optimizer has useful statistics.

Terminal output of a PostgreSQL restore drill showing pg_dump, createdb, pg_restore, a row count of 5000 and ANALYZE
My restore drill on PostgreSQL 16 in a sandbox, captured today.

Write down how long the whole restore took. That is your real recovery time, not the number in the plan.

Make Failover Part of Normal Releases

Cane’s suggestion is to use failover during routine changes. Move traffic away from one environment, deploy and validate there, then move traffic back. He calls this a planned failover. Do it often and the switch stops being an event.

Slide titled Fail Over On Purpose During Planned Changes over a photo of railway tracks at a busy station
From today's shahari.me carousel. Station photo by Kranich17 on Pixabay.

Doing it often also forces three questions. What happens to data and replication delay? What happens to requests in flight? How long does the switch take, and is that acceptable?

Slide listing three questions to answer before a failover: data, traffic in flight and time
From today's shahari.me carousel. Hard drive photo by ds_30 on Pixabay.

If you run active active, check that what is left after a failover can carry the full load. Cane sees teams with three environments drop to two in a normal failover, and sometimes they have to run on one.

Common Mistakes and Fixes

  • Restoring onto the production server. Always use a separate database or host.
  • Trusting that a dump file exists. A file you never restored is a hope, not a backup. Check rows on a table you care about.
  • Mixing -C with --single-transaction. The pg_restore docs say the two cannot be used together.
  • Treating a planned shift as proof of automatic failover. Cane points out that a planned traffic shift does not validate an unexpected failure. Test automatic detection on its own.

When not to do this: do not start with a production failover on a heavy traffic day, or before you have a written way back. Start with restore drills on a copy, then move to planned failovers in a quiet window.

My Take

After 10+ years building for the web, the habit I trust most is boring: rehearse the thing you hope you never need. Put a restore drill on the calendar every month, log the time it took, and make failover a normal step in your releases.

The day you need plan B, it should feel like routine. I share build notes on Instagram @shahari.me.

Sources

Open to new projects

Let's build something ๐Ÿ‘‹

Open to conversations about products, partnerships, or just building things that last. Reach out any time.