Backup, Recovery, and High Availability
How DBAs protect data and keep it available: RPO and RTO, types of backups, point-in-time recovery, the 3-2-1 rule, high availability vs disaster recovery, replication, and failover.
What can go wrong
Protecting data from loss and keeping it available are the two promises a DBA makes to the business. This lesson covers the key ideas behind both: backups, recovery, high availability, and disaster recovery. These are the concepts you'll hear in your first week on the job.
Databases don't usually fail in dramatic ways, but over the years, all of these happen:
- Human error. The most common cause of data loss: an
UPDATEorDELETEwithout aWHEREclause, or a table dropped in the wrong environment. - Hardware failure. Disks, servers, and network equipment wear out and break.
- Software bugs and corruption. A bug in an application, the operating system, or the database itself damages data.
- Cyberattacks. Ransomware encrypts files, including databases and sometimes their backups, and demands payment.
- Disasters. Fire, flood, power loss, or an outage of a whole data center or cloud region.
Every one of these has a different answer. Backups protect against losing data. High availability keeps the database running through local failures. Disaster recovery brings it back after losing a whole site.
RPO and RTO: how much can you afford to lose?
Before designing anything, a DBA agrees two numbers with the business for each database:
- RPO (recovery point objective)
- The most data the business can afford to lose, measured in time. An RPO of 15 minutes means that after a failure, you must be able to recover everything up to at most 15 minutes before it happened.
- RTO (recovery time objective)
- The longest the database can be down before the business is seriously hurt. An RTO of 1 hour means it must be back in service within an hour.
An online store might need an RPO of near zero (no lost orders) and an RTO of minutes. An internal reporting database might accept losing a day of data and being down for a day. Lower numbers always cost more, so RPO and RTO are business decisions, and the DBA designs backups and high availability to meet them.
Backups
A backup is a copy of the database, kept somewhere safe, that you can restore if the original is lost or damaged. Backups are described in a few ways:
| Type | What it copies | Trade-off |
|---|---|---|
| Full | The whole database | Simplest to restore, but the slowest and largest |
| Incremental | Only what changed since the last backup | Fast and small, but a restore needs the full backup plus every incremental since |
| Differential | Everything that changed since the last full backup | A middle ground: a restore needs the full backup plus only the latest differential |
- Physical vs logical. A physical backup copies the database's files, block by block; in Oracle, this is done with RMAN (Recovery Manager). A logical backup, or export, saves the data and object definitions as SQL-level content, such as with Oracle Data Pump. Physical backups are used to recover whole databases; exports are handy for copying a few tables or moving data between databases.
- Online vs offline. Online (hot) backups run while users keep working. Offline (cold) backups require shutting the database down, so production databases are almost always backed up online.
A common schedule combines the types, such as a full backup each weekend and an incremental backup every night:
Point-in-time recovery
Backups alone only take you back to when the backup ran. To get closer, databases keep their archived logs, the history of every change, as covered in the Database Architecture and Storage lesson. Recovery happens in two steps:
- Restore: copy the backup files back into place.
- Recover: replay the archived logs to bring the database forward, to the latest moment or to any point you choose.
That second step is called point-in-time recovery. If someone drops an important table at 2:15 PM, the DBA can restore last night's backup and replay the logs up to 2:14 PM, losing only the minute in between.
Protecting the backups
A backup stored on the same server as the database dies with it. The widely used 3-2-1 rule says to keep:
- 3 copies of your data (the original plus two backups),
- on 2 different types of storage,
- with 1 copy off-site, such as in another data center or cloud region.
Because ransomware now targets backups too, many companies also keep one immutable copy, which can't be changed or deleted for a set period, even by an administrator. Backups should also be encrypted, since they contain all of the database's data.
High availability and disaster recovery
Backups protect the data, but restoring a large database can take hours. When the business can't wait that long, DBAs use two related approaches:
| High availability (HA) | Disaster recovery (DR) | |
|---|---|---|
| Protects against | Local failures: a server, disk, or database instance going down | Losing a whole site: a data center or cloud region |
| How | Redundant copies close by, with automatic failover | A copy far away, in another region |
| Typical recovery time | Seconds to minutes | Minutes to hours |
| Goal | Users barely notice | The business survives |
The "nines"
Availability is usually stated as a percentage of time a system is up, often described by its number of nines. Each extra nine is much harder, and more expensive, to achieve:
| Availability | Called | Downtime allowed per year |
|---|---|---|
| 99% | Two nines | About 3.65 days |
| 99.9% | Three nines | About 8.8 hours |
| 99.99% | Four nines | About 53 minutes |
| 99.999% | Five nines | About 5 minutes |
Replication and failover
The main tool for both HA and DR is replication: keeping a second copy of the database, called a standby or replica, continuously up to date. The primary database ships its changes, its redo, to the standby, which applies them:
- Synchronous replication
- A commit on the primary waits until the standby has the change too, so no committed data is lost if the primary fails (an RPO of zero). The cost is a small delay on every commit, which grows with the distance between sites.
- Asynchronous replication
- The primary commits without waiting, and the standby catches up a moment later. Commits stay fast, even across long distances, but the last few seconds of changes can be lost in a failure.
- Failover
- When the primary fails, the standby takes over as the new primary, often automatically. Applications then reconnect to it.
- Switchover
- A planned, controlled swap of roles between primary and standby, with no data loss, used for maintenance or to test the setup.
- Clustering
- Several servers running one database together, so if one server fails, the others keep serving users. Oracle Real Application Clusters (RAC) works this way.
Every major platform has its own tools for this:
| Platform | Replication and failover |
|---|---|
| Oracle | Data Guard (standby databases); Real Application Clusters (RAC) for clustering |
| SQL Server | Always On availability groups; failover cluster instances |
| PostgreSQL | Streaming replication, often managed by tools such as Patroni |
| MySQL | Replication; InnoDB Cluster |
| Cloud managed databases | Built-in options, such as Amazon RDS Multi-AZ deployments, which keep a standby in a second availability zone |
Choosing a disaster recovery strategy
The right strategy depends on the RPO and RTO the business needs, and how much it's willing to pay:
| Strategy | How it works | Typical RPO and RTO | Cost |
|---|---|---|---|
| Backup and restore | Backups copied to another region, restored onto new servers after a disaster | Hours to a day | Low |
| Standby database | A replica kept up to date in another region, ready to take over | Seconds to minutes | Medium to high |
| Active-active | Two or more sites serving users at the same time | Near zero | Highest, and the most complex |
In practice: Friday, 4:05 p.m.
A support engineer runs a cleanup script against production. It contains DELETE FROM orders with no WHERE clause, and it commits. Every order is gone. Here's how Kofi handles it:
- Stop the damage and get the facts. What exactly ran, against which database, and at what time: 4:05 p.m. New orders are still arriving, so simply rolling the whole database back would lose them.
- Choose the least disruptive fix. The delete was only minutes ago, so Oracle's undo still holds the old rows. Kofi queries the table as it was at 4:04 p.m. with a flashback query, and copies the missing rows back, without touching anything else.
- Know the fallback. Had the mistake been found days later, after the undo was gone, Kofi would restore last night's backup to a separate server, recover it to 4:04 p.m. with the archived logs, and copy just the orders back. Rolling back all of production would wipe out every other change made since.
- Prevent the next one. Afterward, the team removes direct delete access from the support account and requires scripts to be reviewed and tested first.
Point-in-time recovery, archived logs, and tested backups are what make each of those options possible.
The DBA's part
Backup and recovery is one of the few areas where the DBA is often the last line of defense. When everything else has failed, the business asks one question: can we get the data back? A good DBA makes sure the answer is yes:
- Agree RPO and RTO with the business for every important database.
- Automate backups, so they never depend on someone remembering.
- Monitor every backup, and get alerted when one fails.
- Test restores regularly, and measure how long they take.
- Write a runbook: step-by-step instructions anyone on the team can follow during a crisis.
- Practice failovers and DR drills, so the first time isn't during a real disaster.
- Protect the backups: off-site, encrypted, and with at least one copy that can't be deleted.
Key takeaways
- Data is lost through human error, hardware failure, corruption, cyberattacks, and disasters. Human error is the most common.
- RPO is how much data you can afford to lose; RTO is how long you can afford to be down. The business sets them, and the DBA designs to meet them.
- Full, incremental, and differential backups, plus archived logs, make point-in-time recovery possible.
- Follow the 3-2-1 rule, keep an immutable copy, and test restores: an untested backup is only a hope.
- High availability survives local failures in seconds or minutes; disaster recovery survives losing a whole site.
- Replication keeps a standby up to date for failover, but it isn't a backup, since mistakes are replicated too.