SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
Part 3: Caching
Part 4: Data and Storage
  • Indexing
  • Partitioning
  • Replication
  • Sharding
  • Query Optimization
  • Denormalization
  • Materialized Views
  • Connection Pooling
  • Replication Models
  • Replication Lag
  • Quorum (R + W > N)
  • Shard Keys, Hot Shards and Resharding
  • Cross-Shard Joins
  • Backups, RPO, RTO and Disaster Recovery
  • Point-in-Time Recovery
  • Data Archiving and TTL
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/Data and Storage/Database Scaling

Point-in-Time Recovery

Rewind a database to the minute before the mistake, using a backup plus the log.

โฑ 12 min readAdvancedInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Point-in-time recovery (PITR) lets you rewind a database to any moment inside a time window, for example to one minute before someone ran a bad command.

  • It starts from a base backup (a full copy taken earlier) and then replays the log of every change made since, and stops at the time you choose.
  • A backup alone only takes you back to the moment the backup was made. PITR fills the gap between backups.
  • It needs the transaction log to be saved continuously, not just the backups.

02Concept: What is it?

Most databases write every change to a log before they apply it. The PostgreSQL write-ahead log (WAL) and the MySQL binary log are two examples. This log is an exact, ordered diary of everything that changed.

If you keep a base backup and all the log written after it, you can rebuild the database as it was at any moment after that backup. Restore the backup, then replay the log forward until the time you want, and stop there.

The usual reason is a human or software mistake: a DELETE without a WHERE, a bad deploy that corrupted rows, a dropped table. A replica will not save you here, because replication copies the mistake to every copy within seconds.

How point-in-time recovery works
๐Ÿ’พBase backupfull copy, 02:00
โ†’โ†“then replay
๐Ÿ“œArchived logevery change since
โ†’โ†“stop at
๐ŸŽฏTarget timejust before the mistake
โ†’โ†“
๐Ÿ—„๏ธRecovered databasea new instance
Pick the newest base backup before your target time, then replay the log from there up to the target.
๐Ÿง Backup copies the past, the log fills the gaps
Think of the base backup as a photo and the log as a film made after the photo. With both, you can pause the film at any second.

03Real-Life Example

๐ŸตThe Autosave of a Document
You write a long report. Every night your computer saves a full copy of the file (the base backup). During the day it also records every keystroke in a hidden history (the log). At 3:07 pm you accidentally select everything and press delete, then save. You do not want last night's copy, because you would lose a whole day of work. Instead you take last night's copy and replay the keystroke history up to 3:06 pm. You get back everything except the mistake.
The document
Point-in-time recovery
Last night's full copy
The base backup
The hidden record of every keystroke
The archived transaction log
Replaying keystrokes in order
Replaying the log
Stopping at 3:06 pm
The recovery target time
Opening the result as a new file
Restoring to a new instance
No keystroke history kept
Log archiving off: only nightly copies
A longer day of keystrokes to replay
A longer replay, so a longer recovery
Two ways to get back, with a mistake at 15:07
Restore last night's backup
Data losteverything since 02:00
โชBack to 02:00
Point-in-time recovery
Data lostabout a minute
๐ŸŽฏBack to 15:06

04Why Do We Need It?

๐ŸงฏUndo human mistakes
A bad query or a bad deploy can be undone without losing the good work around it.
๐ŸŽš๏ธTiny data loss
You can lose seconds of data instead of hours, because you are not stuck with backup moments.
๐Ÿ”Investigate safely
Restore to a new copy, look at the data as it was, and compare it with today's.
๐Ÿฆ Slow-moving damage
A bug that slowly corrupts rows can be rolled back to the last good minute.
๐Ÿ“œCompliance
Many rules ask for the ability to recover data to a given time within a retention period.
๐ŸงชSafe testing
You can build a copy of production as it was at a chosen time to reproduce a bug.
Replicas protect you from broken machines. Point-in-time recovery protects you from mistakes, which replicas copy faithfully.

05Where Do We Use It?

๐Ÿ˜PostgreSQL
Continuous WAL archiving plus a base backup. You set a recovery target time and the server replays up to it.
๐ŸฌMySQL
Full backups plus the binary logs, replayed up to a chosen position or time.
โ˜๏ธManaged databases
Cloud relational services usually offer a restore to a chosen time within a retention period. Check the length of that period for your product.
๐ŸƒDocument and other stores
Many databases offer a log-based restore (sometimes called oplog or journal replay) for the same purpose.
๐Ÿ’ณPayments and orders
Where one lost minute means lost money, people want recovery to the exact minute.
๐ŸฅRegulated data
Health and finance systems often must prove they can recover to a point in time.

06Types / Parts / How It Works

The pieces you need

Everything that has to exist before the mistake
1. Base backups
๐Ÿ’พFull snapshotevery few hours or days
โฑ๏ธKept for the windowretention period
2. Log archive
๐Ÿ“œEvery log filecopied away continuously
๐ŸงฉNo gapsone missing file stops replay
3. Safe place
๐Ÿ—ƒ๏ธOther machine or regionnot on the database disk
๐Ÿ”Access and encryptionprotect the history
If any piece is missing when you need it, PITR does not work.

Choosing the recovery target

TargetWhat it meansWhen to use it
A timeStop at a given date and timeYou know roughly when the mistake happened.
A log positionStop at an exact point in the log (an LSN or binlog position)You found the bad statement in the log and want to stop just before it.
A named restore pointStop at a label you created before a risky changeBefore a big migration or deploy, mark a point you can come back to.
The end of the logReplay everything, to the latest changeThe disk or the machine was lost, not a mistake.

Base backup frequency

Backups more oftenBackups less often
Less log to replay, so a faster recoveryMore log to replay, so a slower recovery
More backup storage and more backup workCheaper and lighter
Fewer log files needed to restoreA long chain of log files that must all be intact
VisualizerRewind to the minute before the mistakeMove the mistake and the target time, change how often backups run, then turn log archiving off to see why the log matters. Compare with restoring only the newest backup.
How do you recover?
A base backup is taken every
Last 48 hours (the right edge is now). Green ticks are base backups.
๐Ÿ’ฅ
0 h12 h24 h36 h48 h
backup used, and the log replayed after itgood writes lostthe mistake
Restored to
30:15
any minute is possible
Log replayed
6.3 h
after the backup
Recovery time
28 min
illustrative
Good writes lost
0
at 50 writes per minute (illustrative)
The database is rebuilt from the 24:00 backup and then replays 6.3 hours of archived log, stopping just before the mistake at 30:15. No good write is lost. The work needed grows with the span you replay.

A simplified model with fixed numbers (no random values). The write rate, the 15 minutes to copy a backup back and the log replaying 30 times faster than real time are illustrative, not measurements. Real recovery time depends on database size, hardware and how busy the log was.

07How to Use It / Steps / Implementation

Setting up PITR and using it

  1. 1
    Turn on continuous log archiving
    Copy each finished log file to safe storage away from the database machine. Without this, there is nothing to replay.
  2. 2
    Take base backups on a schedule
    Pick the gap between backups from how long a recovery you can accept. Keep them for the whole recovery window.
  3. 3
    Decide the retention window
    For example 7 or 30 days. Keep both the backups and the log for that long, and no gap in the log.
  4. 4
    Watch the archive
    Alert when a log file is missing or the archive falls behind. A gap quietly ends your recovery range.
  5. 5
    Stop the damage first
    Block the bad job or the bad client before you recover, or the problem will happen again.
  6. 6
    Restore to a new instance
    Restore the right base backup, then replay the log to your target. Leave the damaged database untouched so you can compare.
  7. 7
    Check, then cut over or copy the good data
    Look at the restored data. Either switch applications to it, or copy only the missing rows back into production.
  8. 8
    Practise it
    Run a restore drill regularly. A backup you have never restored is a hope, not a plan.
The idea in PostgreSQL terms (illustrative, check your version's docs)
# 1. restore the base backup into a new, empty data directory
# 2. tell the server where the archived log files are
restore_command = 'copy-from-archive %f %p'
# 3. tell it where to stop
recovery_target_time = '2026-03-14 15:06:00'
# 4. start the server: it replays the log up to that time
๐Ÿ’กMark a point before risky work
Before a big migration, create a named restore point. Recovery to it is then exact and needs no guessing about times.
โš ๏ธTime zones and clocks
Be careful which time zone your target time uses. A one-hour mistake here recovers to the wrong moment.

08Key Terms to Know

TermMeaning in easy words
Base backupA full copy of the database taken at one moment
Transaction logThe ordered record of every change (WAL in PostgreSQL, binary log in MySQL)
Log archivingCopying finished log files to safe storage as they are produced
Replay (redo)Applying logged changes again, in order, to rebuild a state
Recovery targetThe time, position or named point where replay stops
Recovery windowHow far back you can recover, set by how long you keep backups and log
RPORecovery Point Objective: how much recent data you can afford to lose
RTORecovery Time Objective: how long you can afford to be down while recovering
Restore pointA named marker you create in the log before a risky change
SnapshotA fast, frozen copy of a disk or database, often used as a base backup
Log gapA missing log file in the chain, which stops replay at that point

09Pros, Cons and Trade-offs

โœ… Pros
  • Recover to almost any second in the window
  • Very little data loss compared with backup-only
  • Undoes mistakes that replicas copy
  • Restoring to a new instance is safe and lets you inspect
  • Supported by most mature databases
โš ๏ธ Cons
  • Needs continuous log archiving to work
  • Recovery time grows with the amount of log to replay
  • Extra storage for backups and logs
  • A missing log file breaks the chain
  • Needs practice and regular restore drills

Trade-offs

ChoiceWe gainWe lose
Frequent base backupsA faster recovery, a shorter log chainMore storage and backup work
Rare base backupsCheaperA long replay, so a long wait to recover
Long retention windowYou can go back furtherMore storage cost, more data to protect
Short retention windowCheaper and smallerA mistake found late cannot be undone
Restore to a new instanceSafe, and you can compareTakes time and extra capacity
Restore in placeNo extra machineRisky: a wrong target destroys the evidence

10Common Problems and Mistakes

Common mistakes

  1. Never testing a restore. The first real restore finds the broken archive, the wrong password or a missing file.
  2. Backups without the log. You can only go back to backup moments, so a mistake loses hours of data.
  3. Storing the log on the same disk. A lost disk takes the database and its history together.
  4. Relying on a replica. The replica copies the bad delete within seconds.
  5. Window too short. The mistake is noticed on day 9 and you only kept 7 days.
  6. Restoring in place first. If the target time is wrong you cannot try again.
  7. Forgetting the rest of the system. A recovered database may disagree with caches, queues or other services that kept moving.

Things to watch

๐ŸงฉLog gaps
A single missing log file ends the replay there. Monitor the archive for gaps.
โณSlow replay
A busy day means a lot of log. Measure how long a real replay takes.
๐ŸŒTime zones
Write the target time with its zone to avoid being off by hours.
๐Ÿ”—Other systems
Downstream copies and caches may need to be rebuilt after a recovery.

11Real-World Examples

๐Ÿง‘โ€๐Ÿ’ปThe missing WHERE
A developer runs a delete against production with no filter. The team recovers to the minute before, into a new copy, and copies the lost rows back.
๐Ÿš€A bad release
A deploy writes wrong values for 20 minutes. The team restores to just before the release and replays only the good data.
โ˜๏ธManaged databases
Cloud services keep backups and logs for you, so a restore to a chosen time within the retention period is a button or a command.
๐ŸฆFinance
Teams practise restores to prove that they can meet recovery targets.
๐ŸงชDebugging
Engineers restore a copy as it was at the time of a customer complaint, to see what really happened.
๐ŸงฏRansomware
A restore to before the attack, from a copy the attacker could not reach, gets the system back.
๐Ÿ“Note
Retention periods and features depend on the product and its plan. Check the documentation of the database you use.
Mini case: a bad delete at 15:07
๐Ÿ’ฅ15:07 bad delete
โ†’โ†“stop writes
๐Ÿ’พ02:00 backupnewest before 15:06
โ†’โ†“replay log
๐ŸŽฏ15:06new instance
โ†’โ†“copy rows back
๐Ÿ—„๏ธProduction
Mistake Backup and result Log replay
Recovery goes to a new instance. The good data comes back, and the mistake stays behind.

12Related Topics to Read Next

Write-Ahead Log (WAL)โ†’

The log that makes replay possible.

Backups, RPO, RTO and Disaster Recoveryโ†’

The wider plan: how much loss and how much downtime you accept.

Replicationโ†’

Protects from machine loss, but copies mistakes too.

Change Data Capture (CDC)โ†’

Also reads the log, to send changes to other systems.

ACID Transactionsโ†’

Why a replay never leaves half a transaction.

Data Archiving and TTLโ†’

How long to keep old data and logs.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • โœ”What: restore a base backup, replay the log, stop at a chosen time.
  • โœ”Needs: base backups + continuous log archive + safe storage + a retention window.
  • โœ”Why: undo mistakes that replicas copy. Lose seconds, not hours.
  • โœ”Speed: more frequent base backups mean less log to replay and faster recovery.
  • โœ”Where: restore to a new instance, then cut over or copy good rows back.
  • โœ”Danger: a gap in the log, or the log on the same disk as the database.
  • โœ”Always: practise restores. Measure the real recovery time.
  • โœ”One line: โ€œA backup is a photo, the log is the film: together you can pause at any second.โ€
โ† Previous
Backups, RPO, RTO and Disaster Recovery
Next โ†’
Data Archiving and TTL