Video in progress

To deploy services independently, how do you ensure compatibility between versions N and N-1, especially for DB migrations?

Every change is split so that during a rolling deploy the old and new versions run at the same time and both must be able to work. In the DB, expand–migrate–contract: first an additive change, then moving data, and removing the old thing only in a later release.

📖 Answer

A rolling deploy means that for a few minutes (sometimes for hours, with a canary) the old and new code run against one DB and the same topics. A rollback can happen too. So every change must be compatible in both directions.

An example: splitting the customer.full_name column into first_name and last_name.

Release 1 (expand):   ADD COLUMN first_name, last_name (nullable)
                      code: writes to both formats, reads from the old one
Backfill job:         fills in the old rows in chunks
Release 2:            code reads from the new one, still writes to both
Release 3 (contract): writing to the old column stops
Release 4:            DROP COLUMN full_name

Four releases looks long, but each one is safe and each can be rolled back. A RENAME COLUMN in a single release makes the old pods fail immediately. In interviews people often slip up right here.

Rules beyond the DB:

  • API. Adding a new field is safe; a client must ignore unknown fields (FAIL_ON_UNKNOWN_PROPERTIES=false in Jackson, which is the default in Spring Boot).
  • Events. A consumer version that understands the new format is deployed before the producer sends it. Schema Registry checks only that schemas are compatible, so you still watch the deploy order yourself.
  • Cache. If the new version writes to the cache in a different format, the old version can't read it. Adding the format version to the cache key is a simple fix.
  • Feature flag. New behavior ships with the code but is turned on with a flag, so deploy and release are separated.

If a Flyway or Liquibase migration runs at application startup, the new pod runs the expand migration while the old pods keep working, so the migration must be compatible with the old code too. On big tables, account for ALTER locks as well: in PostgreSQL, CREATE INDEX CONCURRENTLY, and adding a column with a default value is fast in version 11+.

To turn this into a habit, put a question in the PR template: "does this change work alongside the N-1 version?". In CI you can also run the old version's tests against the new schema.