0

The Database Problem Hiding Inside Every Gradual Rollout

Almost every article about safe releases talks about traffic. Send 5% of requests here, switch a load balancer there, or roll back in seconds.

But there's one important part that often gets overlooked: your database doesn't roll back in seconds. And it's usually shared by both versions of your application.

That gap is where many supposedly safe rollouts go wrong.

A Small Example

Let's say you want to rename a database column from username to handle.

The obvious approach is to write a migration that renames the column, update your application code to use handle, and deploy the changes.

Now imagine that deployment uses a rolling update. For a few minutes, old and new pods run simultaneously.

As soon as the migration runs, every old pod starts failing because the username column it depends on no longer exists.

Trying to roll back doesn't help much either. The old application code still expects username, but the database schema has already changed.

Suddenly, your quick rollback isn't so quick anymore.

Expand, Then Contract

The solution is to break database changes into smaller steps where every application version remains compatible with the database schema.

This approach is commonly known as the expand-and-contract pattern.

Here's how it works:

  1. Expand: Add the new handle column without removing the existing username column.
  2. Dual Write: Deploy application code that writes to both columns but continues reading from username.
  3. Backfill: Copy existing data from username into handle.
  4. Switch Reads: Deploy a new version that reads from handle instead.
  5. Contract: Once no application version depends on username, remove the old column in a later release.

Instead of completing everything in one deployment, you make the change across four or five releases.

It might feel slower the first time, but there's a good reason for doing it this way.

At every stage, you can roll back the application without immediately having to reverse a database migration. That's the whole point.

Habits That Help

After dealing with these issues, our team started following a few simple rules:

  • Keep migrations separate: Never run database migrations automatically when an application pod starts. Run them as a separate step before the rollout.
  • Maintain backward compatibility: Every migration must work with the application version currently running in production, not just the new version.
  • Delay destructive changes: Dropping columns, renaming fields, and changing data types should wait at least one release.
  • Backfill in batches: Large data migrations should run in smaller batches to avoid locking tables during peak traffic.

These practices make deployments more predictable and reduce the chances of unexpected failures.

Why This Matters for Every Deployment Strategy

Whether you're using rolling updates, blue-green deployments, or canary releases, the application layer can switch between versions relatively quickly.

The database layer is different. It remains shared, and its changes often outlive individual application releases.

Most software deployment strategies assume that old and new application versions can safely work with the same database. It's your responsibility to make that assumption true.

Once your database migrations follow the expand-and-contract pattern, the rest of your release process becomes much less stressful.

A safe deployment isn't just about switching traffic without downtime. It's also about making sure your application and database can handle the transition together.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí