DBMS/Lesson 9 of 9/6 min read
Recovery, Storage and NoSQL
Failure types, log-based recovery, write-ahead logging, deferred vs immediate update, checkpoints, ARIES, shadow paging, backups, OLTP vs OLAP, and NoSQL database types with BASE.
On this page
Failure types
- Transaction failure: logical error (constraint violation) or system error (deadlock victim).
- System crash: power loss or OS/DBMS crash. Main memory is lost, disk survives.
- Disk failure: media is damaged. Needs backups or replicas.
Storage hierarchy for recovery: volatile (RAM), non-volatile (disk, SSD), and stable storage (an abstraction: data replicated so it is never lost).
Log-Based Recovery
The DBMS keeps a log: a sequential file of records describing every change.
<T1 start>
<T1, A, 1000, 900> (transaction, item, old value, new value)
<T1, B, 2000, 2100>
<T1 commit>- Undo(T): restore old values, for transactions that did not commit.
- Redo(T): reapply new values, for transactions that committed but whose changes may not be on disk.
After a crash: redo every transaction with both start and commit in the log; undo every transaction with start but no commit.
Deferred vs immediate update
- Deferred update: changes go to disk only after commit. No undo needed, only redo (NO-UNDO/REDO).
- Immediate update: changes may reach disk before commit. Needs both undo and redo (UNDO/REDO). Most real systems.
Buffer policies: steal (dirty pages of uncommitted transactions may be written to disk, needs undo) and no-force (committed pages need not be written immediately, needs redo). Steal + no-force gives the best performance and is what ARIES supports.
Write-Ahead Logging
Write-Ahead Logging (WAL) rules:
- Before a modified data page is written to disk, its log records must be on stable storage (enables undo).
- Before a transaction commits, all its log records, including the commit record, must be on stable storage (enables redo, hence durability).
Logs are sequential appends, which are fast, so a commit only needs a log flush, not random writes of data pages. PostgreSQL calls it WAL; MySQL InnoDB has a redo log. WAL is also streamed to replicas for replication.
Checkpoint
Without checkpoints, recovery would scan the entire log. A checkpoint periodically:
- Flushes log records in memory to stable storage.
- Flushes dirty buffer pages to disk.
- Writes a
<checkpoint L>record listing active transactions.
Recovery then only needs to process the log from the last checkpoint onward (plus active transactions listed in it). Fuzzy checkpoints avoid stopping all activity while flushing.
ARIES
ARIES (Algorithm for Recovery and Isolation Exploiting Semantics, IBM) is the standard recovery algorithm. Every log record has a Log Sequence Number (LSN); every page stores the LSN of its last update (pageLSN).
Three phases after a crash:
- Analysis: scan forward from the last checkpoint to find dirty pages and transactions active at the crash.
- Redo: "repeat history", reapplying all logged updates (committed or not) from the earliest needed LSN, skipping pages whose pageLSN shows the update is already there.
- Undo: roll back uncommitted transactions in reverse order, writing Compensation Log Records (CLRs) so undo is never repeated if the system crashes again during recovery.
Shadow Paging
Shadow paging keeps two page tables: the shadow (current committed state, on disk) and the current (being modified). Updates go to new pages. Commit atomically switches the pointer to the current page table. No log needed for undo, but it fragments data, makes garbage collection necessary, and handles concurrency poorly. Rarely used in modern RDBMSs (SQLite's rollback journal and copy-on-write file systems use similar ideas).
Backups
- Full backup: everything.
- Incremental: changes since the last backup of any kind.
- Differential: changes since the last full backup.
- Point-in-time recovery: restore a base backup, then replay archived WAL up to a chosen moment.
OLTP
Online Transaction Processing: many short reads and writes (orders, payments). Normalized schemas, row storage, ACID, low latency.
OLAP
Online Analytical Processing: few complex read queries over huge data (reports, BI). Denormalized schemas, column storage, aggregations.
Data Warehouse
A data warehouse collects data from operational systems through ETL (Extract, Transform, Load) for analysis. Schemas: star (central fact table plus denormalized dimension tables) and snowflake (normalized dimensions). Columnar storage (Redshift, BigQuery, ClickHouse) reads only needed columns and compresses well.
| OLTP | OLAP |
|---|---|
| Current data | Historical data |
| Many small transactions | Few large queries |
| Normalized | Denormalized (star schema) |
| Row store | Column store |
NoSQL
NoSQL ("not only SQL") databases trade some relational features (joins, fixed schemas, strict ACID across rows) for horizontal scalability, flexible schemas and high availability. Motivated by web-scale data and the CAP theorem.
BASE
Many NoSQL systems follow BASE instead of ACID:
- Basically Available: the system responds even during partial failures.
- Soft state: state can change over time even without input (replicas converging).
- Eventually consistent: replicas become consistent if writes stop.
Key-Value Store
Map from key to opaque value. Extremely fast, simple API (get, put, delete). Examples: Redis, DynamoDB, Riak. Use for caches, sessions, carts.
Document Database
Stores JSON-like documents with nested fields; queries can filter on fields inside documents. Flexible schema. Examples: MongoDB, Couchbase, Firestore. Use for content, catalogs, user profiles.
Column-Family Store
Rows with dynamic columns grouped into column families, partitioned across nodes, optimized for huge write volumes. Examples: Cassandra, HBase, Bigtable. Use for time series, logs, IoT.
Graph Database
Nodes and edges with properties; queries traverse relationships efficiently (Cypher, Gremlin). Examples: Neo4j, Neptune. Use for social graphs, recommendations, fraud detection.
LSM Tree
Write-heavy NoSQL stores often use a Log-Structured Merge (LSM) tree instead of a B+ tree:
- Writes go to an in-memory sorted structure (memtable) and a WAL.
- When full, the memtable is flushed as an immutable sorted file (SSTable).
- Background compaction merges SSTables.
- Reads check the memtable then SSTables, using Bloom filters to skip files.
Very fast sequential writes; reads and compaction cost more. Used by Cassandra, RocksDB, LevelDB, ScyllaDB.
SQL vs NoSQL
| SQL (relational) | NoSQL |
|---|---|
| Fixed schema | Flexible schema |
| Joins, complex queries | Usually denormalized, access-pattern driven |
| Strong ACID | Often BASE, tunable consistency |
| Vertical scaling first | Built for horizontal scaling (sharding) |
| Banking, ERP, most apps | Huge scale, high write throughput, flexible data |
Modern lines blur: PostgreSQL has JSONB, MongoDB has multi-document transactions, and NewSQL (Spanner, CockroachDB) offers SQL plus horizontal scale. Scaling techniques are in databases at scale.
Quick revision
- Undo uncommitted, redo committed. WAL: log before data, log before commit.
- Checkpoints bound recovery time; ARIES = analysis, redo, undo with CLRs.
- OLTP row store and normalized; OLAP column store and star schema.
- NoSQL types: key-value, document, column-family, graph. BASE vs ACID.