Advanced Track Do this after finishing Chapters 01-15.

Estimated Time

  • Reading: 30 min
  • Lab: 50 min
  • Quiz: 5 min

Prerequisites

  • Core track (Chapters 01-15) completed.
  • GitOps promotion and observability workflows available.

What You Will Produce

On your own kind cluster: the schema version the database has and the log of the initContainer that applied it; a new column written before anything reads it, and switched on with a flag through a pull request; a breaking schema change that an image rollback does not fix - and the roll forward that does; a rollback to the previous release that keeps every row written since; and the list of users a point-in-time restore to before the change would have deleted.

Chapter 18: Rollback and Data Migrations

Learning Objectives

By the end of this chapter, you will be able to:

  • Explain why rolling back an image does not roll back a database, and why a point-in-time restore is not a rollback when users keep writing
  • Split a schema change into expand and contract steps so that the previous release always runs on the new schema
  • Trace where this platform’s migrations run - an initContainer, before the app starts - and what the app checks at startup
  • Roll back a release that migrated, prove that no data was lost, and fix a broken schema by rolling forward

Recall Check

Before continuing, quickly recall:

  • How do you roll back a release on this platform - and what makes the rollback hold? (Chapter 10)
  • What does a point-in-time restore give back, and where does it restore to? (Chapter 12)
  • Why is the revert of a release the first thing to check during an incident - and what has to be true before you run it? (Chapter 15)

If you can’t answer these, revisit the corresponding chapter before proceeding.

Context

Chapter 10 made a rollback one line in Git: the previous image tag. That holds while a release changes only code. The backend also owns a database - the app database of the CloudNativePG cluster app-postgres - and a release can change its schema. An image goes back in seconds; the schema stays where the new release left it. If the previous release cannot run on that schema, the rollback fails exactly when it is needed. Chapter 12’s point-in-time restore can take the database back too - and with it every row users wrote since. On a system that people use, that is not a rollback; it is a second incident.

This chapter does not try to cover every way to change a schema safely. It shows one of the most widely used approaches, the one this platform runs: expand/contract, roll forward, migrations in an initContainer. Other approaches work too, each with its own costs:

  • down migrations - a reverse script for every change; it undoes the schema, but cannot give back data that a drop removed;
  • a migration Job before the deploy - a Kubernetes Job, a Helm hook or an Argo CD PreSync hook instead of an initContainer;
  • online schema change tools - pgroll, gh-ost, pt-online-schema-change - which keep the old and new schema live at once, for large tables and zero-downtime changes;
  • declarative schema operators - Atlas, SchemaHero - where Git holds the desired schema and a controller computes the change;
  • event sourcing - the data is a log of events, and a schema is only a view that can be rebuilt.

Work through each lesson section; the chapter video, once it is published, gives the concept overview.

A Release That Changes the Schema

Diagram: Git new image tag; initContainer backend migrate (advisory lock); database schema N+1 (expand only); app starts checks: schema at least N+1; rollback?; image N; release N runs on schema N+1 no data lost; restore to before; every row since the release: gone

Detailed Lessons

Hands-On Materials

Labs, quizzes and runbooks of the advanced track - sign in and redeem an access code.

  • Lab: The Rollback That Keeps the Data Advanced
  • Quiz: Chapter 18 (Rollback and Data Migrations) Advanced
  • Runbook: A Release with a Migration Advanced