Lab · Reliability
Table change: rename a column with no downtime
A live app reads and writes the column name. Rename it to full_name in one step, or in six safe steps. Skip a step, and see which version of the code breaks.
Try it
- Old code — reads name — healthy — writes name
- users table — 2 rows — healthy — columns: id, name
Next: step 1, Add the new column.
| id | name | Written by |
|---|---|---|
| 1 | Asha | Before the plan |
| 2 | Ben | Before the plan |
What the last step did
Nothing yet. After each step, each running version of the code signs up one user, then loads the list of users. A load is one read for each row.
The plan
- Add the new columnDatabase change. Add full_name to the table. It is empty in every row. Next
- Write to both columnsCode release. Ship code that writes each name to name and to full_name. Later
- Copy the old rowsDatabase change. Copy name into full_name in every row where full_name is empty. Later
- Read the new columnCode release. Ship code that reads full_name. It still writes both columns. Later
- Stop writing the old columnCode release. Ship code that writes full_name only. Later
- Remove the old columnDatabase change. Remove name from the table. Later
Sign-ups failed
0/ 0
the code wrote a column that is not there
Reads that failed
0/ 0
the code read a column that is not there
Names shown blank
0
the cell the code read was empty
Names lost for good
0
in no column that still exists
Numbers add up:0 sign-ups = 0 ok + 0 failed · 0 reads = 0 ok + 0 blank + 0 failedThe lab checks this sum as it runs, so a counting mistake would show here.
Controls
The plan
Add a second column, move the code and the data over, then remove the old column.
What this model simplifies
- The table is tiny. On a table with millions of rows, the copy runs in small batches for hours, so it does not slow the live app.
- A release is one round here, with the older and the newer code side by side. A real rolling release moves over server by server.
- The only requests are sign-ups and loads of the user list. If the double write is skipped, a user who changes their name adds another fault: the new column keeps the old name.
- A cell may be empty here. If the real name column is marked as required, sign-ups fail at step 5 unless that rule is removed first.
- Adding a column is instant here. On some databases, adding a column locks a large table for a while.
- The code writes both columns itself. Many teams use a migration tool or a database trigger to do the double write and the copy.
- In a real plan, the first four steps can be undone at once. After step 5 the old column is out of date, so teams wait and check before they go on.