Rollback Planning

Rollback Planning for a Feature Added to a Live Product: Database Migrations With Expand and Contract

When a new feature goes live, the code is the easy part to take back. Most teams can redeploy an earlier version of an application in a short time. The database is a different matter: a dropped column, a rewritten table or a renamed field does not return because the application did. For that reason, rollback planning for a feature added to a live product is largely a question about the data, and it is best answered before the first migration is written.

 

Why schema changes break rollbacks

During a deployment, old and new versions of the application often run side by side against one database, and a rollback means the old code meets whatever the new code left behind. Suppose a feature renames the column due_date to delivery_date. The new code expects the new name. The previous version still reads the old one. If the rename has already happened, the previous version fails the moment it is restored, and the team that wanted a quick way back has none.

The same holds for changes that look harmless: tightening a constraint, changing a type, splitting one field into two. Each one is safe in one direction and fatal in the other.

Expand and contract

Martin Fowler’s bliki entry on Parallel Change describes a pattern for exactly this problem, often called expand and contract. A change is made in three phases. In the expand phase the new structure is added next to the old one without removing anything. In the migrate phase consumers are moved from the old structure to the new one, one at a time. In the contract phase, once nothing uses the old structure, it is removed. An article on evolutionary database design, also hosted on Fowler’s site, describes the supporting practice: database changes written as small, ordered migration scripts that are applied continuously rather than in one large step.

Applied to the rename, the sequence is this:

  1. Expand. Add delivery_date as a new nullable column. The old column stays untouched, and the previous version of the application keeps working.
  1. Migrate. Copy existing values across in small batches, write to both columns while both versions are in use, and switch reads to the new column once the values agree.
  1. Contract. Remove due_date only after the old code is gone and the team has decided that going back is no longer needed.

Which steps can be undone

The value of the pattern is that the first two phases are reversible. Adding a column can be undone by ignoring it. Writing to two places can be undone by writing to one. Only the last phase destroys information, and so it should be treated as a separate decision with its own owner and its own moment, not as the tail of the same change.

That decision can be framed as a rollback window: the period during which the team keeps the old structure in place so that a return remains possible. How long the window stays open is a judgment about risk. It is shorter when the feature is small and well observed, and longer when it touches data that is hard to rebuild.

Questions for a rollback plan

A short plan, written before the work begins, can answer the following:

  1. What does “rolled back” mean for a user: the feature disappears, or the feature disappears and the data created through it disappears too?
  1. Which data written while the feature was live must survive a rollback, and where will it live if the new structure is removed?
  1. Which migrations are additive, and which are destructive? Is every destructive step separated from the additive ones?
  1. Who can approve the contract phase, and what evidence is required first?
  1. Has the migration been rehearsed on a copy of realistic data, including a run in reverse?

The product owner’s part

Several of these questions are not technical. Whether data entered through a new feature may be discarded on rollback is a product decision with consequences for customers. A product owner who has seen the options can say which data is precious, which can be regenerated and what users should be told. These answers shape the migration plan, so they are better given at the start than discovered during an incident.

This is one of the practical differences in feature development for existing software: the feature does not arrive in an empty system. It lands in a database that the in-house team has built up over years, that other services and reports read, and that must keep working whichever way the release goes. Planning the way back alongside the way forward belongs to the same conversation that defines acceptance criteria. How that conversation is arranged for product owners is described under feature development for existing software.

Common pitfalls

  1. Contracting too early. Removing the old structure as soon as the new one works removes the safety net at the moment it is most likely to be needed.
  1. Forgetting other readers. Reports, exports and analytics jobs often read tables that the application team does not think of as theirs. The migrate phase has to include them.
  1. Large backfills in one statement. Copying every row at once can lock a table and affect users. Batches keep the load predictable.
  1. Testing only on empty databases. A migration that passes on a clean schema can fail on years of accumulated data. Rehearsal needs realistic volumes and odd records.
  1. No owner for the last step. If nobody is responsible for the contract phase, old structures stay forever and the schema accumulates clutter.

A note on proportion

Not every change needs the full pattern. Adding a new table that nothing else reads can be undone by leaving it unused, and a small team may reasonably skip ceremony there. The pattern earns its place when old and new code must coexist, or when the change touches data that users or other systems depend on. Judging which case applies is part of the engineers’ job, and the product owner can help by saying how costly a failed release would be in business terms.

Planning a rollback does not mean expecting failure. It means that the release decision rests on knowing what the way back looks like. This note is published by JetRuby Team as a general guide for teams that add features to products already in use.

Similar Posts