CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos – A. Pavlo Lecture#20: Overview of Transaction Management.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Topic 6.3: Transactions and Concurrency Control Hari Uday.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. –Because disk accesses are.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 16 – Intro. to Transactions.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
Quick Review of May 1 material Concurrent Execution and Serializability –inconsistent concurrent schedules –transaction conflicts serializable == conflict.
1 Concurrency Control and Recovery Module 6, Lecture 1.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
1 Transaction Management Overview Yanlei Diao UMass Amherst March 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 Transaction Management Database recovery Concurrency control.
CS194-3/CS16x Introduction to Systems Lecture 8 Database concurrency control, Serializability, conflict serializability, 2PL and strict 2PL September.
1 Lecture 08: Transaction management overview
Transaction Management and Concurre cy Overview R & G Chapter There are three side effects of acid. Enhanced long term memory, decreased short term.
Transaction Processing
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Transaction Management Overview Chapter Transactions  A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Lecture 21 Ramakrishnan - Chapter 18.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
CS 162 Discussion Section Week 9 11/11 – 11/15. Today’s Section ●Project discussion (5 min) ●Quiz (10 min) ●Lecture Review (20 min) ●Worksheet and Discussion.
Database Systems/COMP4910/Spring05/Melikyan1 Transaction Management Overview Unit 2 Chapter 16.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
1 Transaction Processing Chapter Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.
II.I Selected Database Issues: 2 - Transaction ManagementSlide 1/20 1 II. Selected Database Issues Part 2: Transaction Management Lecture 4 Lecturer: Chris.
The Relational Model1 Transaction Processing Units of Work.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#20: Overview of Transaction Management.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
Overview of Transaction Management
Transaction Management and Recovery, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
1 Database Systems ( 資料庫系統 ) December 27/28, 2006 Lecture 13 Merry Christmas & New Year.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 16 – Intro. to Transactions.
Database Applications (15-415) DBMS Internals- Part XI Lecture 22, April 10, 2016 Mohammad Hammoud.
MULTIUSER DATABASES : Concurrency and Transaction Management.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
Transaction Management Overview
Database Systems (資料庫系統)
Transaction Management Overview
Transaction Management
Transaction Management Overview
Lecture#20: Overview of Transaction Management
CS122B: Projects in Databases and Web Applications Winter 2018
Transaction Management Overview
Transaction Management Overview
Transaction Management Overview
Transaction Management
Transaction Management Overview
Lecture#21: Concurrency Control – Part 1
Lecture 21: Concurrency & Locking
Lecture 21: Intro to Transactions & Logging III
Transaction Management
Database Management systems Subject Code: 10CS54 Prepared By:
Transaction Management Overview
Transaction Management Overview
CS122B: Projects in Databases and Web Applications Winter 2019
CS122B: Projects in Databases and Web Applications Spring 2018
Transaction Management Overview
Presentation transcript:

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#20: Overview of Transaction Management

CMU SCS Administrivia HW7 (Phase 1) is due Tues March 31 st Recitations (always in WEH 5302): –Wed April 1 st 1:30-2:20 Faloutsos/PavloCMU SCS /6152

CMU SCS Last Class Database Design Database Tuning Faloutsos/PavloCMU SCS /6153

CMU SCS Today’s Class Transactions Overview Concurrency Control Recovery Faloutsos/PavloCMU SCS /6154

CMU SCS Motivation We both change the same record (“Smith”); how to avoid race condition? You transfer $100 from savings→checking; power failure – what happens? Faloutsos/PavloCMU SCS /6155 Lost Updates Concurrency Control Durability Recovery

CMU SCS Motivation We both change the same record (“Smith”); how to avoid race condition? You transfer $100 from savings→checking; power failure – what happens? Faloutsos/PavloCMU SCS /6156 Lost Updates Concurrency Control Durability Recovery DBMSs automatically handle both issues: ‘transactions’

CMU SCS Concurrency Control & Recovery Valuable properties of DBMSs. Based on concept of transactions with ACID properties. Let’s talk about transactions… Faloutsos/PavloCMU SCS /6157

CMU SCS Transactions A transaction is the execution of a sequence of one or more operations (e.g., SQL queries) on a shared database to perform some higher-level function. It is the basic unit of change in a DBMS: –Partial transactions are not allowed! Faloutsos/PavloCMU SCS /6158

CMU SCS Transaction Example Move $100 from Christos’ bank account to his bookie’s account. Transaction: –Check whether Christos has $100. –Deduct $100 from his account. –Add $100 to his bookie’s account. Faloutsos/PavloCMU SCS /6159

CMU SCS Strawman System Execute each txn one-by-one (i.e., serial order) as they arrive at the DBMS. –One and only one txn can be running at the same time in the DBMS. Before a txn starts, copy the entire database to a new file and make all changes to that file. –If the txn completes successfully, overwrite the original file with the new one. –If the txn fails, just remove the dirty copy. Faloutsos/PavloCMU SCS /61510

CMU SCS Problem Statement Better approach is to allow concurrent execution of independent transactions. Q: Why do we want that? –Utilization/throughput (“hide” waiting for I/Os) –Increased response times to users. But we also would like: –Correctness –Fairness Faloutsos/PavloCMU SCS /61511

CMU SCS Transactions Hard to ensure correctness… –What happens if Christos only has $100 and tries to pay off two bookies at the same time? Hard to execute quickly… –What happens if Christos needs to pay off his gambling debts very quickly all at once? Faloutsos/PavloCMU SCS /61512

CMU SCS Problem Statement Arbitrary interleaving can lead to –Temporary inconsistency (ok, unavoidable) –Permanent inconsistency (bad!) Need formal correctness criteria. Faloutsos/PavloCMU SCS /61513

CMU SCS Definitions A txn may carry out many operations on the data retrieved from the database However, the DBMS is only concerned about what data is read/written from/to the database. –Changes to the “outside world” are beyond the scope of the DBMS. Faloutsos/PavloCMU SCS /61514

CMU SCS Formal Definitions Database: A fixed set of named data objects (A, B, C, …) Transaction: A sequence of read and write operations (R(A), W(B), …) –DBMS’s abstract view of a user program Faloutsos/PavloCMU SCS /61515

CMU SCS Transactions in SQL A new txn starts with the begin command. The txn stops with either commit or abort : –If commit, all changes are saved. –If abort, all changes are undone so that it’s like as if the txn never executed at all. Faloutsos/PavloCMU SCS /61516 A txn can abort itself or the DBMS can abort it.

CMU SCS Correctness Criteria: ACID Atomicity: All actions in the txn happen, or none happen. Consistency: If each txn is consistent and the DB starts consistent, then it ends up consistent. Isolation: Execution of one txn is isolated from that of other txns. Durability: If a txn commits, its effects persist. Faloutsos/PavloCMU SCS /61517

CMU SCS Correctness Criteria: ACID Atomicity: “all or nothing” Consistency: “it looks correct to me” Isolation: “as if alone” Durability: “survive failures” Faloutsos/PavloCMU SCS /61518

CMU SCS Transaction Demo Faloutsos/PavloCMU SCS /61519

CMU SCS Overview Problem definition & ‘ACID’ A tomicity C onsistency I solation D urability Faloutsos/PavloCMU SCS /61520

CMU SCS Atomicity of Transactions Two possible outcomes of executing a txn: –Txn might commit after completing all its actions. –or it could abort (or be aborted by the DBMS) after executing some actions. DBMS guarantees that txns are atomic. –From user’s point of view: txn always either executes all its actions, or executes no actions at all. Faloutsos/PavloCMU SCS /61521 A

CMU SCS Mechanisms for Ensuring Atomicity We take $100 out of Christos’ account but then there is a power failure before we transfer it to his bookie. When the database comes back on-line, what should be the correct state of Christos’ account? Faloutsos/PavloCMU SCS /61522 A

CMU SCS Mechanisms for Ensuring Atomicity One approach: LOGGING –DBMS logs all actions so that it can undo the actions of aborted transactions. Think of this like the black box in airplanes… Faloutsos/PavloCMU SCS /61523 A

CMU SCS Mechanisms for Ensuring Atomicity Logging used by all modern systems. Q: Why? Faloutsos/PavloCMU SCS /61524 A

CMU SCS Mechanisms for Ensuring Atomicity Logging used by all modern systems. Q: Why? A: Audit Trail & Efficiency Reasons What other mechanism can you think of? Faloutsos/PavloCMU SCS /61525 A

CMU SCS Mechanisms for Ensuring Atomicity Another approach: SHADOW PAGING –DBMS makes copies of pages and txns make changes to those copies. Only when the txn commits is the page made visible to others. –Originally from System R. Few systems do this: –CouchDB –LMDB (OpenLDAP) Faloutsos/PavloCMU SCS /61526 A

CMU SCS Overview Problem definition & ‘ACID’ A tomicity C onsistency I solation D urability Faloutsos/PavloCMU SCS /61527

CMU SCS Database Consistency Database Consistency: Data in the DBMS is accurate in modeling the real world and follows integrity constraints Faloutsos/PavloCMU SCS /61528 C

CMU SCS Transaction Consistency Transaction Consistency: if the database is consistent before the txn starts (running alone), it will be after also. Transaction consistency is the application’s responsibility. –We won’t discuss this further… Faloutsos/PavloCMU SCS /61529 C

CMU SCS Strong vs. Weak Consistency In a distributed DBMS, the consistency level determines when other nodes see new data in the database: – Strong: Guaranteed to see all writes immediately, but txns are slower. – Weak/Eventual: Will see writes at some later point in time, but txns are faster. Faloutsos/PavloCMU SCS /61530 C

CMU SCS Application Servers DBMS Servers Strong Consistency Faloutsos/PavloCMU SCS /61531 Master Replicas Update Profile Get Profile C

CMU SCS Application Servers DBMS Servers Eventual Consistency Faloutsos/PavloCMU SCS /61532 Master Replicas Update Profile Get Profile C ? ?

CMU SCS Overview Problem definition & ‘ACID’ A tomicity C onsistency I solation D urability Faloutsos/PavloCMU SCS /61533

CMU SCS Isolation of Transactions Users submit txns, and each txn executes as if it was running by itself. Concurrency is achieved by DBMS, which interleaves actions (reads/writes of DB objects) of various transactions. Q: How do we achieve this? Faloutsos/PavloCMU SCS /61534 I

CMU SCS Isolation of Transactions A: Many methods - two main categories: – Pessimistic – Don’t let problems arise in the first place. – Optimistic – Assume conflicts are rare, deal with them after they happen. Faloutsos/PavloCMU SCS /61535 I

CMU SCS Example Consider two txns: –T1 transfers $100 from B’s account to A’s – T2 credits both accounts with 6% interest. Faloutsos/PavloCMU SCS /61536 BEGIN A=A+100 B=B–100 COMMIT BEGIN A=A+100 B=B–100 COMMIT T1 BEGIN A=A*1.06 B=B*1.06 COMMIT BEGIN A=A*1.06 B=B*1.06 COMMIT T2 I

CMU SCS Example Assume at first A and B each have $1000. Q: What are the legal outcomes of running T1 and T2? Faloutsos/PavloCMU SCS /61537 BEGIN A=A+100 B=B–100 COMMIT BEGIN A=A+100 B=B–100 COMMIT T1 BEGIN A=A*1.06 B=B*1.06 COMMIT BEGIN A=A*1.06 B=B*1.06 COMMIT T2 I

CMU SCS Example Q: What are the possible outcomes of running T1 and T2 together? A: Many! But A+B should be: $2000*1.06=$2120 There is no guarantee that T1 will execute before T2 or vice-versa, if both are submitted together. But, the net effect must be equivalent to these two transactions running serially in some order. Faloutsos/PavloCMU SCS /61538 I

CMU SCS Example Legal outcomes: –A=1166, B=954 –A=1160, B=960 The outcome depends on whether T1 executes before T2 or vice versa. Faloutsos/PavloCMU SCS /61539 I →$2120

CMU SCS Serial Execution Example Faloutsos/PavloCMU SCS /61540 I ≡ A=1166, B=954A=1160, B=960 TIME BEGIN A=A+100 B=B–100 COMMIT T1 T2 BEGIN A=A*1.06 B=B*1.06 COMMIT BEGIN A=A+100 B=B–100 COMMIT T1 T2 BEGIN A=A*1.06 B=B*1.06 COMMIT Schedule

CMU SCS Interleaving Transactions We can also interleave the txns in order to maximize concurrency. –Slow disk/network I/O. –Multi-core CPUs. Faloutsos/PavloCMU SCS /61541 I

CMU SCS Interleaving Example (Good) Faloutsos/PavloCMU SCS /61542 I ≡ A=1166, B=954 TIME BEGIN A=A+100 B=B–100 COMMIT T1 T2 BEGIN A=A*1.06 B=B*1.06 COMMIT BEGIN A=A+100 B=B–100 COMMIT T1 T2 BEGIN A=A*1.06 B=B*1.06 COMMIT Schedule

CMU SCS Interleaving Example (Bad) Faloutsos/PavloCMU SCS /61543 I ≢ A=1166, B=960 TIME A=1166, B=954 or A=1160, B=960 BEGIN A=A+100 B=B–100 COMMIT T1 T2 BEGIN A=A*1.06 B=B*1.06 COMMIT Schedule The bank lost $6!

CMU SCS Interleaving Example (Bad) Faloutsos/PavloCMU SCS /61544 I Schedule A=1166, B=960 TIME DBMS’s View BEGIN R(A) W(A) R(B) W(B) COMMIT T1 T2 BEGIN R(A) W(A) R(B) W(B) COMMIT BEGIN A=A+100 B=B–100 COMMIT T1 T2 BEGIN A=A*1.06 B=B*1.06 COMMIT

CMU SCS Correctness Q: How do we judge that a schedule is correct? A: If it is equivalent to some serial execution Faloutsos/PavloCMU SCS /61545 I

CMU SCS Formal Properties of Schedules Serial Schedule: A schedule that does not interleave the actions of different transactions. Equivalent Schedules: For any database state, the effect of executing the first schedule is identical to the effect of executing the second schedule.* Faloutsos/PavloCMU SCS /61546 I (*) no matter what the arithmetic operations are!

CMU SCS Formal Properties of Schedules Serializable Schedule: A schedule that is equivalent to some serial execution of the transactions. Note: If each transaction preserves consistency, every serializable schedule preserves consistency. Faloutsos/PavloCMU SCS /61547 I

CMU SCS Formal Properties of Schedules Serializability is a less intuitive notion of correctness compared to txn initiation time or commit order, but it provides the DBMS with significant additional flexibility in scheduling operations. Faloutsos/PavloCMU SCS /61548 I

CMU SCS Interleaved Execution Anomalies Read-Write conflicts (R-W) Write-Read conflicts (W-R) Write-Write conflicts (W-W) Q: Why not R-R conflicts? Faloutsos/PavloCMU SCS /61549 I

CMU SCS Reading Uncommitted Data, “Dirty Reads”: BEGIN R(A) W(A) R(B) W(B) ABORT T1 T2 BEGIN R(A) W(A) COMMIT Write-Read Conflicts Faloutsos/PavloCMU SCS /61550 I $10 $12 $14

CMU SCS Read-Write Conflicts Unrepeatable Reads Faloutsos/PavloCMU SCS /61551 I BEGIN R(A) COMMIT T1 T2 BEGIN R(A) W(A) COMMIT $10 $19

CMU SCS Overwriting Uncommitted Data BEGIN W(A) W(B) COMMIT T1 T2 BEGIN W(A) W(B) COMMIT Write-Write Conflicts Faloutsos/PavloCMU SCS /61552 I $10 Christos $19 Bieber

CMU SCS Solution Q: How could you guarantee that all resulting schedules are correct (i.e., serializable)? A: Use locks! Faloutsos/PavloCMU SCS /61553 I

CMU SCS Executing without Locks Faloutsos/PavloCMU SCS /61554 I TIME BEGIN R(A) W(A) COMMIT T1 T2 BEGIN R(A) W(A) COMMIT

CMU SCS BEGIN LOCK(A) R(A) W(A) UNLOCK(A) COMMIT T1 T2 BEGIN LOCK(A) R(A) W(A) UNLOCK(A) COMMIT Executing with Locks Faloutsos/PavloCMU SCS /61555 I TIME Lock Manager Granted (T1→A) Denied! Granted (T2→A) Released (T1→A) Released (T2→A)

CMU SCS Executing with Locks Q: If a txn only needs to read ‘A’, should it still get a lock? A: Yes, but you can get a shared lock. Faloutsos/PavloCMU SCS /61556 I

CMU SCS Lock Types Basic Types: –S-LOCK – Shared Locks (reads) –X-LOCK – Exclusive Locks (writes) Faloutsos/PavloCMU SCS /61557 I SharedExclusive Shared ✔ X Exclusive XX Compatibility Matrix

CMU SCS Executing with Locks Transactions request locks (or upgrades) Lock manager grants or blocks requests Transactions release locks Lock manager updates lock-table But this is not enough… Faloutsos/PavloCMU SCS /61558 I

CMU SCS BEGIN X-LOCK(A) R(A) W(A) UNLOCK(A) S-LOCK(A) R(A) UNLOCK(A) COMMIT T1 T2 BEGIN X-LOCK(A) W(A) UNLOCK(A) COMMIT Executing with Locks Faloutsos/PavloCMU SCS /61559 I TIME Lock Manager Granted (T1→A) Granted (T2→A) Released (T1→A) Released (T2→A) Granted (T1→A) Released (T1→A)

CMU SCS Concurrency Control We need to use a well-defined protocol that ensures that txns execute correctly. Two categories: – Two-Phase Locking (2PL) – Timestamp Ordering (T/O) Faloutsos/PavloCMU SCS /61560 I We will discuss T/O methods in future classes. Pessimistic Optimistic

CMU SCS Two-Phase Locking Phase 1: Growing –Each txn requests the locks that it needs from the DBMS’s lock manager. –The lock manager grants/denies lock requests. Phase 2: Shrinking –The txn is allowed to only release locks that it previously acquired. It cannot acquire new locks. Faloutsos/PavloCMU SCS /61561

CMU SCS Two-Phase Locking The txn is not allowed to acquire/upgrade locks after the growing phase finishes. Faloutsos/PavloCMU SCS /61562 I Growing PhaseShrinking Phase TIME Transaction Lifetime

CMU SCS Two-Phase Locking The txn is not allowed to acquire/upgrade locks after the growing phase finishes. Faloutsos/PavloCMU SCS /61563 I Growing PhaseShrinking Phase TIME Transaction Lifetime 2PL Violation!

CMU SCS BEGIN X-LOCK(A) R(A) W(A) R(A) UNLOCK(A) COMMIT T1 T2 BEGIN X-LOCK(A) W(A) UNLOCK(A) COMMIT Executing with 2PL Faloutsos/PavloCMU SCS /61564 I TIME Lock Manager Granted (T1→A) Denied! Released (T2→A) Released (T1→A) Granted (T2→A)

CMU SCS 2PL Observations There are schedules that are serializable but would not be allowed by 2PL. Locking limits concurrency. May lead to deadlocks. May still have “dirty reads” –Solution: Strict 2PL Faloutsos/PavloCMU SCS /61565 I

CMU SCS Strict Two-Phase Locking A schedule is strict if a value written by a txn is not read or overwritten by other txns until that txn finishes. Advantages: –Recoverable. –Do not require cascading aborts. –Aborted txns can be undone by just restoring original values of modified tuples. Faloutsos/PavloCMU SCS /61566 I

CMU SCS Strict Two-Phase Locking Txns hold all of their locks until commit. Good: –Avoids “dirty reads” etc Bad: –Limits concurrency even more –And still may lead to deadlocks Faloutsos/PavloCMU SCS /61567 I

CMU SCS Strict Two-Phase Locking The txn is not allowed to acquire/upgrade locks after the growing phase finishes. Faloutsos/PavloCMU SCS /61568 I Growing PhaseShrinking Phase TIME Transaction Lifetime

CMU SCS Q: Why is avoiding “dirty reads” important? Strict Two-Phase Locking Faloutsos/PavloCMU SCS /61569 I BEGIN R(A) W(A) R(B) W(B) ABORT T1 T2 BEGIN R(A) W(A) COMMIT $10 $12

CMU SCS Strict Two-Phase Locking Q: Why is avoiding “dirty reads” important? A: If a txn aborts, all actions must be undone. Any txn that read modified data must also be aborted. Faloutsos/PavloCMU SCS /61570 I

CMU SCS Locking in Practice You typically don’t set locks manually. Sometimes you will need to provide the DBMS with hints to help it to improve concurrency. Also useful for doing major changes. Faloutsos/PavloCMU SCS /61571 I

CMU SCS Overview Problem definition & ‘ACID’ A tomicity C onsistency I solation D urability Faloutsos/PavloCMU SCS /61572

CMU SCS Transaction Durability Records are stored on disk. For updates, they are copied into memory and flushed back to disk at the discretion of the O.S. –Unless forced-output: W(B)→fsync() Faloutsos/PavloCMU SCS /61573 D This is slow! Nobody does this!

CMU SCS Transaction Durability Faloutsos/PavloCMU SCS /61574 BEGIN R(A) W(A) ⋮ COMMIT T1 D Buffer Pool Disk A=1 Page A=1 Memory

CMU SCS Transaction Durability Faloutsos/PavloCMU SCS /61575 BEGIN R(A) W(A) ⋮ COMMIT T1 D Buffer Pool Disk A=1 Page A=2 Memory Buffer is added to output queue but is not flushed immediately

CMU SCS Write-Ahead Log Record the changes made to the database in a log before the change is made. Assume that the log is on stable storage. Q: What to replicate? –The complete page? –Single tuple? Faloutsos/PavloCMU SCS /61576 D

CMU SCS Write-Ahead Log Log record format: – –Each transaction writes a log record first, before doing the change When a txn finishes, the DBMS will: –Write a record on the log –Make sure that all log records are flushed before it returns an acknowledgement to application. Faloutsos/PavloCMU SCS /61577 D

CMU SCS Write-Ahead Log After a failure, DBMS “replays” the log: –Undo uncommited transactions –Redo the committed ones Faloutsos/PavloCMU SCS /61578 D

CMU SCS BEGIN W(A) W(B) ⋮ COMMIT T1 Write-Ahead Log Faloutsos/PavloCMU SCS /61579 D ⋮ CRASH! Before Value After Value TxnIdObjectId The DBMS hasn’t flushed memory to disk at this point. We have to redo T1! Safe to return result to application.

CMU SCS Write-Ahead Log Faloutsos/PavloCMU SCS /61580 D ⋮ CRASH! BEGIN W(A) W(B) ⋮ COMMIT T1 We have to undo T1

CMU SCS Recovering After a Crash At the end – all committed updates and only those updates are reflected in the database. Some care must be taken to handle the case of a crash occurring during the recovery process! Faloutsos/PavloCMU SCS /61581 D

CMU SCS WAL Problems The log grows infinitely… We have to take checkpoints to reduce the amount of processing that we need to do. We will discuss this in further detail in upcoming classes. Faloutsos/PavloCMU SCS /61582

CMU SCS ACID Properties Atomicity: All actions in the txn happen, or none happen. Consistency: If each txn is consistent, and the DB starts consistent, it ends up consistent. Isolation: Execution of one txn is isolated from that of other txns. Durability: If a txn commits, its effects persist. Faloutsos/PavloCMU SCS /61583

CMU SCS Summary Concurrency control and recovery are among the most important functions provided by a DBMS. Concurrency control is automatic –System automatically inserts lock/unlock requests and schedules actions of different txns. –Ensures that resulting execution is equivalent to executing the txns one after the other in some order. Faloutsos/PavloCMU SCS /61584

CMU SCS Summary Write-ahead logging (WAL) and the recovery protocol are used to: –Undo the actions of aborted transactions. –Restore the system to a consistent state after a crash. Faloutsos/PavloCMU SCS /61585

CMU SCS Overview A tomicity C onsistency I solation D urability Faloutsos/PavloCMU SCS /61586 Recovery Concurrency Control