- devops
- database
- deploy
- backup
The "roll back" button brings the application back. What happens to the database?
Rolling back code takes two minutes. Rolling back a database does not work that way. Why, and how a team sets this up properly: a ledger analogy, a three-step rule and what to look at before a release.
The first day of the week, 10:20. A new version of an online shop goes live. It is a small change: the customer's first and last name will now be kept in two separate fields instead of one.
Ten minutes later the support line starts ringing: the orders page will not open. The team stays calm, because they have prepared for a day like this. "We are rolling back." Two minutes later the old version is in place.
The site still does not work.
Everyone stares at the screen. The code is yesterday's code, and yesterday everything worked. So what has changed?
The ledger and the person who keeps it
To understand this, picture a shop that still works the old way. Every customer and every order is written down in a thick ledger.
The database is that ledger. Every page has columns: first and last name, phone, order, amount.
The code is the employee who writes in the ledger and reads it. They know by heart which piece of information is in which column. When a customer calls, their eye goes straight to the right column.
When a new version goes live, two things usually happen at once. The employee is taught a new rule, and the columns of the ledger are changed. In our example the "first and last name" column was erased and two separate columns were drawn in its place: "first name" and "last name".
Now look at what the "roll back" button does. It puts the employee back on the old rule, but it does not touch the ledger. Out of old habit the employee looks for the "first and last name" column, and that column is no longer there.
On top of that, new orders arrived in those ten minutes and were written into the ledger by the new rule. An employee who knows only the old rule cannot read them.
This is the main point of this post: rolling back code is easy, rolling back a database is not. Yesterday's copy of the code is still there, you take it and put it back. The ledger, though, gets a new line every minute. Put it back to yesterday's state and today's orders are lost.
That is why it is wrong to say "we have a rollback button" and relax. The button exists, but it only rolls back half of the work.
The three-step rule
Good teams do not try to roll the database back. They arrange the work so that they never need to. The rule is simple: add first, then move, remove last.
1. Add first
Two new columns are drawn in the ledger, and the old column stays where it is. Nothing is erased.
An employee working by the old rule notices nothing: their column is in place, there are just two empty columns next to it. If something breaks, going back is easy, because everything old is untouched.
2. Then move
The names in the old column are written into the new columns as well. For a while every new customer is entered in both the old column and the new ones. This is the stage where the employee is taught the new rule.
If the new rule turns out to be wrong, the employee goes back to the old one and everything works, because the old column is still filled in. This is the stage where the "roll back" button really does its job.
3. Remove last
A week or two goes by. The new rule has proved itself, and nobody looks at the old column any more. Only now is the old column erased. On a different day, as a separate job, without hurry.
The price is patience: three releases instead of one. But after every step the way back stays open. The team in the opening scene had squeezed all three steps into one morning.
Steps with no way back
There are two kinds of change that no button can undo.
Removing. When a column is erased, what was written in it goes too. When a field or a table is removed from the database, the data in it is lost with it.
Changing. Merging two fields, converting a date format, writing a new value over the old one. After that the old state does not exist anywhere.
Before a step like this a backup is always taken. Keep two things in mind:
- The backup must be tested. A backup that has never been restored cannot be relied on.
- A backup is the last resort. If you restore the database from the ten o'clock backup, the orders that came in after that go too. So the main protection is the three-step rule.
What to look at before a release
Before a new version goes live, it is enough to know a few things.
Does this version change the database? If it does, it must be tested in advance that the old code works with the new database. If it has not been tested, the "roll back" button cannot be relied on.
Adding and removing must not happen on the same day. Removing is always a separate job.
It must be known when the last backup was taken and how long a restore from it takes.
If rolling back does not work, what will be done that day and who makes the decision should also be agreed in advance.
The short answer
This post grew out of a question. After our post on Docker and automatic deploys, a database specialist asked: how do you handle rollback once database changes are involved?
The answer is this: we do not roll the database back. We split the change into steps small and ordered enough that the old code still works after each one. Removing comes last, and before it there is a tested backup.
In that post, rolling back was one sentence: undo the change and push again. For code that is true. Once a database is involved, that sentence needs this post next to it.
JezaHost offers a backup service for VPS: when it is switched on, a copy of the server is taken automatically 4 times a day. Details: VPS plans.