NNabeel Hassan

Blog · December 3, 2026 · 8 min read

Changing the Database Schema on a Live App Without Downtime

By Nabeel Hassan — AI Engineer · ICPC World Finalist

TL;DR: Changing the database under a live app is safe if you never ask the old code and the new code to disagree about the same data at the same moment. I do every schema change in small steps: expand the schema in a way old code ignores, deploy code that writes both shapes, backfill existing data in batches, switch reads over, and only contract (drop the old column or field) once nothing reads it. On Postgres I watch for changes that lock busy tables. On MongoDB and Firestore, where there is no schema to migrate, I version documents and make the code read every shape it might find. And I take a backup before anything destructive. It sounds slower than one big migration. In practice it is faster, because nothing ever has to be rolled back at midnight.

Every product that lives long enough outgrows its first data model. A name field becomes first and last name, a single price becomes a price per plan, a user belongs to one team and then to several. The change itself is usually easy to write. The hard part is making it while people are using the app.

I have run backends on PostgreSQL, MongoDB and Firebase for my own products like LectureNotes AI and Lifemaxxing AI, for the mobile apps and web portal I lead as CTO at RAQTS, and in codebases I have inherited from other teams. The process below is the one I use on all of them now.

Why schema changes break live apps

A schema change goes wrong for one of three reasons, and it helps to name them because each needs a different fix.

Old code meets new data. During any deploy there is a window where the previous version of your server is still running. On serverless hosts, old instances can keep serving requests for a while. If the migration has already renamed a column, every request on the old code fails until it is gone.

New code meets old data. The reverse problem. New code assumes every row has the new field, but the rows written before the change do not. This one shows up days later, on the account nobody opened during testing.

The migration itself blocks the app. Some changes take a lock on the table while they run. On a small table nobody notices. On the table that every request touches, the app stalls until the migration finishes.

And on mobile there is a fourth, longer version of the first problem: old app builds stay installed for months and keep calling your API. I covered that side in keeping old mobile app versions working. The database work below is what lets the API stay additive while the data underneath changes.

The pattern: expand, migrate, contract

The idea is to break one risky change into several boring ones, where each step is safe on its own and the app works between every step. Take the classic example: splitting full_name into first_name and last_name.

Step 1: expand

Add the new columns, nullable, with no constraints that old code could violate. Old code does not know they exist and keeps working. Nothing reads them yet.

Step 2: write both

Deploy code that writes full_name and the two new fields on every create and update. It still reads full_name. From this point on, all new and edited rows are complete in both shapes.

Step 3: backfill

Fill the new fields for the rows written before step 2. This is a script or a job, not part of the deploy, and it runs in batches (more on that below).

Step 4: switch reads

Deploy code that reads the new fields. If something is wrong, rolling this deploy back is safe, because the old field is still there and still being written.

Step 5: contract

Once nothing reads full_name (check the code, the analytics queries, the admin dashboard and any n8n or reporting jobs that touch the database directly), stop writing it, and in a later deploy drop it.

Each step is a separate deploy. Between steps the app runs normally, sometimes for days. The contract step is the one teams rush, and it is the one I deliberately leave until the evidence says nothing depends on the old shape.

A rename is the same pattern. There is no such thing as a safe in-place rename on a live app, only add new, copy, switch, remove old.

Backfills: small batches, safe to re-run

The backfill is where a careful migration turns into an outage, usually because someone runs one UPDATE over the whole table. That can hold locks for a long time, spike database load for every user, and fail halfway with no record of where it stopped.

The rules I follow:

On Firestore and MongoDB the same rules apply, with paginated queries and batched writes instead of UPDATE. On Firestore I also keep an eye on write limits and cost, because a backfill is a burst of writes you pay for.

Postgres: changes that lock the table

Most schema changes on Postgres are quick, but a few can block reads or writes on a busy table, and they are worth knowing by heart:

Two habits cover the rest. I set a lock timeout on migration sessions, so a migration that cannot get its lock fails quickly instead of queueing behind a long query while every request queues behind it. And I run migrations against a copy of production data before running them for real, because a change that takes a second on the dev database can take ten minutes on the real one.

MongoDB and Firestore: there is no schema, so version the data

Document databases do not make you run a migration, which is not the same as not needing one. The schema just moves into your code, and every document you ever wrote is still out there in its original shape.

What works for me:

Deploy order and rollback

The order matters more than the tooling. My default:

  1. Back up first. Before anything destructive, confirm a recent backup and point-in-time recovery exist. The details are in backups and disaster recovery for a small product.
  2. Schema changes go out before the code that needs them, and they are always backward compatible with the code currently running.
  3. Code that stops using something goes out before the schema change that removes it.
  4. Every deploy can be rolled back on its own without touching the database. If rolling back code would require reverting a migration, the steps were too big.

Migrations live in version control as numbered files, run through the same pipeline in every environment, and get reviewed like code. When I review a pull request, a migration file is one of the first things I open, for the reasons in what to check in code review on a small team.

For a big switch in read paths, I sometimes put it behind a flag, so reads can move to the new shape gradually and move back instantly. That is the same tool as in feature flags and kill switches for a small team, applied to data.

When a maintenance window is fine

Not every product needs zero downtime. If the app has a few dozen users, all in one time zone, and the change is genuinely hard to do in steps, five minutes of planned downtime at 3am with a banner the day before is a reasonable trade. Founders sometimes pay for engineering rigor they do not need yet, which is part of what I wrote about in what to cut from an MVP.

The line I draw: once users are in several time zones, once mobile apps are in the stores, or once anything else (a voice agent, an automation, a client integration) talks to the database around the clock, there is no quiet hour left, and the stepwise pattern becomes the default.

A checklist before any schema change

  1. Is there a recent backup, and do I know how to restore it?
  2. Does the change work with the code currently running, without a deploy?
  3. Could any statement lock a busy table? Is there a lock timeout set?
  4. Is the backfill batched, idempotent and separate from the deploy?
  5. Have I found every reader of the old shape, including reports, dashboards, automations and old mobile builds?
  6. Can each deploy in the sequence be rolled back on its own?
  7. Has it been run against a copy of production-sized data?

None of this is clever. It is the difference between a schema change that nobody notices and one that becomes the story of the week.


I build and run backends for AI products, mobile apps and voice agent systems, and I step in as a fractional CTO when a growing product needs its foundations sorted out. If you have a data model change you are nervous about, read more about my background or book a call.

FAQ

How do you change a database schema without downtime?

Split the change into small steps that are each safe on their own: add the new column or field in a way old code ignores, deploy code that writes both old and new shapes, backfill existing rows in batches, switch reads to the new shape, and only remove the old column once nothing reads it. Each step is a separate deploy that can be rolled back.

Which Postgres schema changes lock the table?

The common ones are a plain CREATE INDEX (use CREATE INDEX CONCURRENTLY instead), adding NOT NULL or foreign key constraints that check every row (add them NOT VALID and validate separately), and column type changes that rewrite the table. Setting a lock timeout on migration sessions stops a blocked migration from stalling every request behind it.

How do you migrate data in MongoDB or Firestore?

Document databases have no schema to migrate, so the schema lives in code. Add a schema version field to documents, make the data layer read and convert every older shape, upgrade documents lazily on write, then run a batched backfill to finish the job. On Firebase, keep security rules accepting both shapes until older app versions are gone.

Is a maintenance window ever acceptable?

Yes, for a small app with users in one time zone and a change that is hard to do in steps. Once users are spread across time zones, mobile apps are in the stores, or automations and voice agents use the database around the clock, there is no quiet hour and the stepwise pattern should be the default.

Building something in this space?

I take on AI-agent, automation and product work directly — scoped fast, shipped fast.

Book a discovery call →

Keep reading