Post

DBS101 Unit7

DBS101 Unit7

Unit VII: Transactions, Concurrency & Recovery – Databases That Heal Themselves

πŸ“Œ Introduction

This unit uncovers the superpowers that keep our data consistent, available, and durable β€” even when the system crashes, power goes out, or users mess up.

Welcome to the world of:

  • βœ… Transactions (Lesson 18)
  • πŸ”’ Concurrency Control (Lesson 19)
  • πŸ” Database Recovery (Lesson 20)

🧾 Lesson 18: Transactions & ACID – Making Promises You Can Keep

🧩 What is a Transaction?

A transaction = a unit of work (like a bank transfer or an inventory update) that must either complete fully or not at all.

ACID Properties

PropertyPurpose
AtomicityAll or nothing
ConsistencyNo data corruption
IsolationNo interference from other transactions
DurabilityChanges survive crashes

πŸ’‘ SQL Example:

1
2
3
4
BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE account_name = 'A';
UPDATE accounts SET balance = balance + 50 WHERE account_name = 'B';
COMMIT;

Storage Types That Support ACID

TypeDescription
VolatileRAM – fast but not crash-proof
Non-VolatileDisks – durable but slower
StableRedundant, reliable storage (RAID)

πŸ’‘ Durability relies on writing to stable storage before committing.

Transaction Lifecycle

StateWhat Happens
ActiveExecution in progress
Partially CommittedFinal operations done
FailedError occurred, can’t proceed
AbortedRolled back to safe state
CommittedSuccessfully completed, changes saved

Serializability β€” Order in the Chaos

To preserve consistency with multiple transactions, databases ensure serializability β€” the outcome is the same as if transactions were run one-by-one.

  • βœ… Serial Schedule: T1 β†’ T2
  • βœ… Serializable: Looks like a serial execution
  • ❌ Non-serial but inconsistent: Database disaster!

Conflict Serializability and Precedence Graphs

Use Precedence Graphs:

  • Nodes = transactions
  • Edge T1 ➝ T2 = T1 must come before T2 due to a conflict
  • πŸ” No cycles = βœ… Serializable
  • πŸ”„ Cycles = ❌ Not serializable

Recoverable vs. Cascadeless Schedules

Schedule TypeRule
RecoverableDon’t commit if you read uncommitted data
CascadelessDon’t even read uncommitted data!

Isolation Levels

LevelIsolation Strength
SerializableStrongest – like serial
Repeatable ReadStable reads, no phantom rows
Read CommittedCan’t read dirty data
Read Uncommitted😱 May read dirty data

πŸ”€ Lesson 19: Concurrency Control – Keeping Transactions from Colliding

πŸ” Why Locks?

Locks help transactions access data safely and prevent conflicts:

  • Shared (S): Read-only access
  • Exclusive (X): Read + Write access

⚠️ Many S-locks allowed, only one X-lock!

Two-Phase Locking (2PL)

PhaseRule
GrowingAcquire locks only
ShrinkingRelease locks only

βœ… Guarantees serializability ❌ May cause deadlocks

Deadlocks & Solutions

StrategyAction
DetectionUse waits-for graph & pick a victim
PreventionUse timestamp rules (Wait-Die, Wound-Wait)

Lock Granularity & Intention Locks

LevelExample
TupleSpecific row
PageBlock of rows
TableEntire table

🧠 Use IS/IX/SIX intention locks for hierarchy control.

Timestamp Ordering (TO)

  • Assign timestamps to transactions
  • Maintain order to ensure serializability
  • Use Thomas Write Rule to reduce aborts

Multi-Version Concurrency Control (MVCC)

  • DBMS keeps multiple versions of data items
  • Readers see consistent snapshot
  • Writers don’t block readers (and vice versa!)

πŸ•° Great for time-travel queries and snapshots

Snapshot Isolation (SI)

Every transaction sees a frozen version of the database from when it began.

⚠️ Beware of Write Skew Anomalies β€” two transactions update different values based on outdated shared state.


♻️ Lesson 20: Recovery – When Things Go Wrong, Databases Fight Back πŸ’ͺ

😱 Why Recovery Matters

  • Power failures
  • Crashes
  • Transaction errors
  • Natural disasters

βœ… Transactions must be atomic and durable even after disaster!

Log-Based Recovery

Every change is recorded in a log BEFORE modifying the actual database.

Log Format ExampleMeaning
<Ti, Xj, V1, V2>Ti changed Xj from V1 to V2
<Ti start>Ti started
<Ti commit>Ti committed
<Ti abort>Ti aborted

Undo & Redo

OperationAction
Undo(Ti)Rollback all changes using old values
Redo(Ti)Reapply changes using new values

πŸ”„ After a crash:

  • Undo incomplete transactions
  • Redo committed transactions

Checkpoints – Recovery Time Boosters

TypeBenefit
StandardSnapshot of active transactions
FuzzyDoesn’t block transactions

βœ… Reduces the log scanning needed during recovery

Log Record Buffering & WAL

  • Logs are kept in memory temporarily
  • Write-Ahead Logging (WAL) ensures log is written before data is updated

Steal & No-Force Policies

PolicyDescription
StealDirty data may be written before commit
No-ForceCommitted data may not be written immediately

Recovery from Disk Failures

  • Periodic full dumps to backup media
  • Use dump + redo log to recover from complete disk loss

Remote Backup Systems

TechniqueUse Case
Log ShippingSend logs to remote replica
Hot-Spare ModeBackup is nearly always ready
Distributed SystemsMulti-site redundancy

ARIES Algorithm (IBM)

A 3-phase recovery strategy:

  1. πŸ” Analysis – What happened?
  2. πŸ”Redo – Replay history
  3. βͺ Undo – Rollback incomplete work

Uses STEAL + NO-FORCE policies and logs every step

Recovery in Main-Memory DBs

  • Main memory = volatile β†’ risk of loss
  • Recovery must reload DB into memory and replay logs

βœ… Optimizations:

  • Skip redo for indexes
  • Do recovery in parallel across cores

πŸ’‘ What I’ve Learned

  • πŸ”„ Transactions must be safe and consistent
  • πŸ” Concurrency control is a balancing act
  • πŸ’Ύ Recovery = logs, checkpoints, ARIES
  • 🌍 High availability = backup systems and replication

🌱 Reflection

From safe updates to post-crash recovery, this unit taught me that databases aren’t just about data β€” they’re about trust.

Transactions, locks, logs, and backups: The unsung heroes keeping our apps alive πŸ’š


πŸ”š Conclusion

ACID, 2PL, ARIES, MVCC β€” they sound complex, but they all serve a simple purpose: Protect the data. Always. πŸ›‘οΈ

πŸŽ“ β€œA reliable system isn’t the one that never fails. It’s the one that knows how to recover.”

This post is licensed under CC BY 4.0 by the author.

Trending Tags