tezvyn:

Schema Evolution: Changing a Live Database Without Outages

AI-drafted, machine-checkedSource: Wikipedia: Schema evolutionintermediate

Schema evolution is like renovating a house while you live in it: you must change your database's structure without breaking the live application. This is critical when adding or renaming columns.

WHY IT EXISTS Software is never finished. As products evolve, their data requirements change, forcing modifications to the database schema. In a live system with real users, you can't simply take the database offline to change it. Schema evolution provides a set of strategies to alter a database's structure while the application is running, ensuring continuity of service.

THE MENTAL MODEL Think of schema evolution like renovating a house while you're still living in it. You can't just knock down a load-bearing wall; the house would collapse. Instead, you'd install a temporary support beam first. Similarly, you can't just rename a critical database column; all the application code relying on the old name would instantly break. You need a careful, multi-step process to transition from the old structure to the new one without causing an outage.

HOW IT WORKS Schema evolution is managed through version-controlled migration scripts. For simple changes like adding a new, nullable column, a single script might suffice. For complex changes like renaming a column or changing a data type, a multi-step deployment is required to avoid downtime. A common pattern for renaming a column involves: first, adding the new column; second, deploying code that writes to both the old and new columns; third, backfilling data from the old column to the new one for existing records; fourth, deploying code that reads only from the new column; and finally, dropping the old column in a later, separate migration.

WHEN TO USE IT Use schema evolution strategies any time you modify the schema of a production database or any database that cannot be taken offline. This includes adding new columns for features, adding indexes for performance, splitting tables for normalization, or renaming columns for clarity. It is a core practice of continuous delivery and DevOps for stateful systems.

WHEN NOT TO USE IT You don't need a formal evolution process for local development databases that can be easily reset, for throwaway prototypes, or during the initial project setup before any real data or users exist. In these scenarios, it's often faster to drop the database and recreate it from an updated schema definition.

ONE CANONICAL EXAMPLE Safely renaming a users.name column to users.full_name on a high-traffic table. A naive ALTER TABLE users RENAME COLUMN name TO full_name; would lock the entire users table, causing widespread errors and timeouts. The safe approach is a four-step process spread across multiple deployments: 1. Add full_name column. 2. Deploy code to write to both columns. 3. Run a script to copy all existing name data to full_name. 4. Deploy code to read/write only to full_name. 5. Finally, in a later cleanup deployment, drop the name column.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.