Engineering notes

Safe Database Migrations During Application Releases

Plan schema changes around mixed application versions, resumable backfills, database locks and a rollback path that preserves new data.

Bitcave4 min readUpdated

A release can pass its unit tests and fail when an old worker meets a newly renamed column. Database changes are shared contracts between processes that may not restart together. This hypothetical order-management release shows how to stage a change without assuming the whole system switches versions at one instant.

List every reader and writer

Include web processes, background workers, scheduled jobs, reports and external consumers. Record which schema versions each can tolerate. A rolling deployment usually creates a period when old and new code coexist, while queued jobs can outlive the release itself.

For a field replacement, distinguish a schema rename from a business meaning change. Reusing an old column for a new meaning can corrupt interpretation even when the type is unchanged. Define the desired invariant and the evidence that existing records satisfy it.

Old and new application modules sharing an expanded data bridge while the old bridge piece is set aside

Generated editorial illustration; not a record of a live system.

Expand before removing the old contract

Add the new representation in a form compatible with current code, then deploy code that can operate during transition. Decide how new writes populate the representations and how conflicts are resolved. Dual writing is a temporary consistency problem that needs explicit handling, not a free safety feature.

Switch reads only after the data and application population are ready. Remove the old field in a later release after confirming no old reader or queued work depends on it. Keep the compatibility interval documented so a future cleanup does not happen by guesswork.

Backfill in controlled, restartable units

A data migration over a large table can hold locks, create load and run longer than a deployment window. Assess the deployed database's operation semantics and test with representative volume. Separate a long-running backfill from application startup where appropriate.

Use stable batch boundaries, checkpoints and a rule that makes repeated batches safe. Account for records created or edited during the backfill. Verify completeness and invariants before enforcing a new constraint. In Django migrations, use the historical model state and database alias appropriate to the migration rather than importing today's application model blindly.

Define rollback as a data decision

Reverting application code does not necessarily reverse a schema change or recover information discarded by a transformation. Identify the last point where the old application can safely run. Preserve the old representation until the rollback window closes when that is part of the plan.

If a change is irreversible, document a forward-repair procedure and the role of a tested backup. A database restore can discard legitimate writes made after the backup, so it is not an automatic response to every release problem. Agree on the incident decision owner before deployment.

Rehearse the mixed-version release

Run old and new application versions against the transitional schema in an isolated environment. Interrupt and resume the backfill, retry the migration runner and exercise an old queued task. Test the planned rollback before any destructive cleanup.

During rollout, watch migration progress, lock waits, request errors and business invariants. Use one controlled migration executor rather than having every replica race to perform the same operation. The release is complete when compatibility, data correctness and cleanup conditions are satisfied, not merely when the deployment platform turns green.

Worked example and implementation boundary

A fictional application replaces a legacy contact-label field with a structured display-name representation. The transition first adds the new fields, then deploys compatible reads and writes, backfills in resumable batches and checks completeness. Only after old web processes and queued tasks are gone does a later release remove the legacy field. The exact write-compatibility rule depends on the model; merely writing both fields without conflict handling is not a complete migration plan.

Trade-offs and limitations

A longer compatibility interval makes rollback easier but prolongs dual-representation maintenance. A shorter interval reduces transitional code while demanding tighter deployment coordination. Irreversible data transformation requires an explicit forward-repair and incident decision plan.

Common pitfalls

Do not rename a shared column while old workers still use it, import current application models blindly into historical migrations, or run an unbounded backfill during every replica startup. Reverting code cannot recover data discarded by a transformation.

Key takeaways

List every reader and writer, expand before removal, checkpoint data work and rehearse mixed versions. Define the last safe rollback point. Remove compatibility code only when the recorded cleanup conditions are met.

References and further reading

Django documentation — Writing database migrations

Related reading and implementation support

Cloud Infrastructure and DevOps

Backup Restore Drills: Prove You Can Recover the Business

Related content

Cloud Infrastructure, Deployment and DevOps Services

Application deployment, CI/CD, containerisation and observability, with operating responsibilities designed around the needs of your business.

Backup Restore Drills: Prove You Can Recover the Business

Turn backup files into a tested recovery process covering databases, media, configuration, access and the first usable business workflow.

Define the implementation boundary

Discuss safe database migrations in the context of your current systems, decision owners and exception paths, then define a bounded first step.

Discuss your project