1. Why a Successful Deployment Can Still Break Your Database
Consider a backend application running with three instances behind a load balancer.
Load Balancer
|
+-----------+-----------+
| | |
v v v
App v1.0 App v1.0 App v1.0
| | |
+-----------+-----------+
|
v
PostgreSQLEverything is working normally. Users are accessing APIs, background workers are processing jobs, and all application instances are using the same database schema.
Now, suppose a new release introduces a change to the database.
You decide that a column named asset_name should be replaced with asset_code to standardize the naming across the application.
The migration might look simple:
ALTER TABLE assets
RENAME COLUMN asset_name TO asset_code;The new application version has already been updated to use asset_code. The migration executes successfully, and the new application instance starts without any errors.
But during a rolling deployment, the old application instances may still be running.
Those instances are still executing queries against asset_name.
Suddenly, requests handled by the older instances begin failing because the database column they expect no longer exists.
Load Balancer
|
+-----------+-----------+
| | |
v v v
App v1.0 App v1.0 App v2.0
| | |
+-----------+-----------+
|
v
PostgreSQL
asset_code exists
asset_name removedThe new application works. The database migration works. But the overall deployment is broken.
This is why database schema changes should not be treated as isolated operations. They are part of the application's deployment lifecycle.
2. Understand the Expand-and-Contract Pattern
One approach I prefer for schema changes that require application compatibility is the Expand-and-Contract pattern.
Instead of removing the old schema immediately, we introduce the new schema first, allow both versions to coexist, migrate the data, and remove the old schema only when it is no longer needed.
The process has three main stages.
Expand
Introduce the new database structure without removing or breaking the existing structure.
Migrate
Update the application, migrate existing data, validate it, and gradually switch the application to the new schema.
Contract
Remove the old database structure after all dependent application versions and processes have stopped using it.
This approach provides a compatibility window during which both old and new application versions can operate against the database.
It is not a guarantee of zero downtime, but it significantly reduces the risk of breaking deployments when implemented correctly.
3. Expand the Schema Before Changing the Application
Let's continue with our asset management example.
The existing table looks like this:
CREATE TABLE assets (
id BIGSERIAL PRIMARY KEY,
asset_name VARCHAR(100) NOT NULL,
site_id INTEGER NOT NULL
);The application currently uses asset_name.
Instead of immediately renaming the column, I would first introduce the new column.
ALTER TABLE assets
ADD COLUMN asset_code VARCHAR(100);Now the database contains both columns.
assets
------------------------------------------------
id | asset_name | asset_code | site_id
------------------------------------------------
1 | METER-001 | NULL | 7
2 | METER-002 | NULL | 7
3 | METER-003 | NULL | 8
------------------------------------------------The old application can continue using asset_name, because nothing has been removed or changed in its expected schema.
At this stage, I would not deploy application code that exclusively depends on asset_code until the compatibility plan is ready.
The database change itself is only the first step.
4. Deploy a Compatibility Version and Backfill Existing Data
The next step is to deploy an application version that understands both columns.
For example, the compatibility release could continue reading the old column while writing both the old and new columns.
A simplified Python example:
async def update_asset(asset_id, asset_name):
asset = await repository.get(asset_id)
asset.asset_name = asset_name
asset.asset_code = asset_name
await repository.save(asset)This is illustrative code. In a real application, the write operation should be transactional, and the application should have a clearly defined source of truth during the transition.
The compatibility version ensures that new changes are reflected in both fields while the migration is underway.
However, this strategy is only safe after considering all writers. Older application instances, background workers, scheduled jobs, scripts, and other services may still write to the database. If they continue updating only the old column, the new column could become inconsistent.
Therefore, I would deploy the compatibility version across the relevant application components and verify that older writers have been replaced before relying on dual writes.
Join the conversation! Your thoughts help the community grow.