πΉ What are Redo Logs?
Redo Logs are a mechanism used by databases to ensure durability and recoverability.
- They record all changes made to the database so that in case of a crash, the database can redo the changes and recover to a consistent state.
- Conceptually, they are very similar to Write-Ahead Logs (WAL).
πΉ How Redo Logs Work
- Transaction begins β changes are made in memory (buffer/cache).
- Changes are written to redo log first (sequentially).
- Actual database files are updated later (asynchronously).
-
On crash recovery:
- Database reads redo logs.
- Re-applies all committed changes to bring database back to a consistent state.
πΉ Example
Suppose you execute an UPDATE accounts SET balance = balance - 100 WHERE id=1;
Step 1: Append to redo log
[Redo Log] UPDATE accounts SET balance = balance - 100 WHERE id=1;
Step 2: Update in-memory database buffer
[Buffer] balance = balance - 100
Step 3: Flush buffer to disk (DB file)
Crash happens? Recovery reads redo log:
- Re-applies update β ensures transaction is not lost
πΉ Key Properties
-
Durability (ACID) β
- Once transaction is written to redo log, itβs considered durable.
-
Crash Recovery π
- Redo logs allow database to replay committed changes after a crash.
-
Sequential Writes π
- Writing to redo log is sequential β faster than random disk writes.
-
Buffering Optimization
- Database can flush multiple redo log entries together β improves performance.
πΉ Redo Log vs WAL
| Feature | Redo Log | WAL (Write-Ahead Log) |
|---|---|---|
| Purpose | Recover committed changes | Recover committed changes |
| Write Order | Log written before data file | Same: log written before data file |
| Typical Use | MySQL (InnoDB), Oracle | PostgreSQL, SQLite, MongoDB (journal) |
| Granularity | Usually at transaction level | Transaction or statement level |
| Storage | Usually fixed-size circular logs | Append-only sequential logs |
πΉ Usage in Databases
- MySQL (InnoDB) β Uses redo logs to implement durability.
- Oracle β Redo logs are core to transaction recovery.
- PostgreSQL / SQLite β Equivalent concept via WAL.
- MongoDB β Journaling acts like redo logs.
π‘ Summary:
- Redo Log = sequential log of all changes that lets DB recover committed transactions after a crash.
- Itβs basically the implementation of WAL in databases like MySQL/Oracle.