2. Transactions
ā¢ A transaction is a unit of work that is
treated in a coherent and reliable manner. All
transactions must be independent of one
another
3. Transactions
ā¢ A transaction is a unit of work that is
treated in a coherent and reliable manner. All
transactions must be independent of one
another
ā¢ Provide concurrency support (next lecture!)
4. Transactions
ā¢ A transaction is a unit of work that is
treated in a coherent and reliable manner. All
transactions must be independent of one
another
ā¢ Provide concurrency support (next lecture!)
ā¢ Fundamental in computer science
7. Atomicity
ā¢ Strong guarantee that changes to a
database are applied āall or nothingā
ā¢ Atomicity must be maintained in the face of
failures (even from OS, hardware etc...)
8. Atomicity
ā¢ Strong guarantee that changes to a
database are applied āall or nothingā
ā¢ Atomicity must be maintained in the face of
failures (even from OS, hardware etc...)
ā¢ E.g.: move Ā£100 from my bank account to
your bank account. If transfer to your
account fails, I must be credited back!
9. Consistency
ā¢ Transactions should not violate integrity
constraints leaving database in illegal state
ā¢ E.g.: age cannot be negative number
11. Isolation
ā¢ Transactions should not affect one another
ā¢ More precisely it deļ¬nes when the changes
resulting from a transaction will be seen by
other concurrent transactions
12. Durability
ā¢ Outcome of transaction must persist
beyond its completion
ā¢ i.e.: given a system crash, the state of the
database can be recovered
13. Transaction Example
ā¢ Debit Ā£100 from Aliceās bank account and
credit it to Bobās account
read(Alice)
Alice = Alice - 100
write(Alice)
read(Bob)
Bob = Bob + 100
write(Bob)
14. Transaction Example
ā¢ Debit Ā£100 from Aliceās bank account and
credit it to Bobās account
} Ā£1950
Aliceās balance = Ā£2000
Bobās balance = Ā£-50
read(Alice)
Alice = Alice - 100
write(Alice)
read(Bob)
Bob = Bob + 100
write(Bob)
15. Transaction Example
ā¢ Debit Ā£100 from Aliceās bank account and
credit it to Bobās account
} Ā£1950
Aliceās balance = Ā£2000
Bobās balance = Ā£-50
read(Alice)
Alice = Alice - 100
} Ā£1850
Aliceās balance = Ā£1900
write(Alice) Bobās balance = Ā£-50
read(Bob)
Bob = Bob + 100
write(Bob)
16. Transaction Example
ā¢ Debit Ā£100 from Aliceās bank account and
credit it to Bobās account
} Ā£1950
Aliceās balance = Ā£2000
Bobās balance = Ā£-50
read(Alice)
Alice = Alice - 100
} Ā£1850
Aliceās balance = Ā£1900
write(Alice) Bobās balance = Ā£-50
read(Bob)
Bob = Bob + 100
} Ā£1950
Aliceās balance = Ā£1900
write(Bob) Bobās balance = Ā£50
18. Transaction Example (2)
ā¢ What if Bobās account does not exist? Or
permissions are set wrong?
read(Alice)
Alice = Alice - 100
write(Alice)
read(Bob)
Bob = Bob + 100
write(Bob)
19. Transaction Example (2)
ā¢ What if Bobās account does not exist? Or
permissions are set wrong?
} Ā£1950
Aliceās balance = Ā£2000
Bobās balance = Ā£-50
read(Alice)
Alice = Alice - 100
write(Alice)
read(Bob)
Bob = Bob + 100
write(Bob)
20. Transaction Example (2)
ā¢ What if Bobās account does not exist? Or
permissions are set wrong?
} Ā£1950
Aliceās balance = Ā£2000
Bobās balance = Ā£-50
read(Alice)
Alice = Alice - 100
} Ā£1850
Aliceās balance = Ā£1900
write(Alice) Bobās balance = Ā£-50
read(Bob)
Bob = Bob + 100
write(Bob)
21. Transaction Example (2)
ā¢ What if Bobās account does not exist? Or
permissions are set wrong?
} Ā£1950
Aliceās balance = Ā£2000
Bobās balance = Ā£-50
read(Alice)
Alice = Alice - 100
} Ā£1850
Aliceās balance = Ā£1900
write(Alice) Bobās balance = Ā£-50
read(Bob)
Bob = Bob + 100 Read fails, must
credit Alice back!
write(Bob)
30. Durability
ā¢ Transaction logs used to ensure durability
ā¢ Log transaction writes, then update
database
ā¢ Logs must be written ļ¬rst or else updates
can be lost given a system crash!
31. Durability
ā¢ Transaction logs used to ensure durability
ā¢ Log transaction writes, then update
database
ā¢ Logs must be written ļ¬rst or else updates
can be lost given a system crash!
ā¢ Disks can still fail! (disk mirroring?)
32. Failures
ā¢ Possible failures include:
ā¢ System crash
ā¢ Hardware malfunctions
ā¢ Disk failure
ā¢ Deadlocks
ā¢ System errors: out of memory, program errors (see
āman 2 writeā)
ā¢ etc...
33. State Machine
Partial Commit-
commit ted
Active
Failed Aborted
34. State Machine
Partial Commit-
commit ted
Active
Initial state Failed Aborted
36. State Machine
Partial Commit-
commit ted
Completion
Active
(database updated)
Failed Aborted
37. State Machine
Partial Commit-
commit ted
Active
Failed Aborted
Processing can no
longer proceed
38. State Machine
Partial Commit-
commit ted
Active
Failed Aborted
Transaction rolled
back to prior state
39. Transaction Logging
ā¢ Transaction start: <T , start>
i
ā¢ Transaction write: <T , X ,V ,V >
i j 1 2
ā¢ Transaction commit: <T , commit>
i
40. Transaction Logging
Transaction ID
ā¢ Transaction start: <T , start>
i
ā¢ Transaction write: <T , X ,V ,V >
i j 1 2
ā¢ Transaction commit: <T , commit>
i
41. Transaction Logging
Transaction ID
Data item ID
ā¢ Transaction start: <T , start>
i
ā¢ Transaction write: <T , X ,V ,V >
i j 1 2
ā¢ Transaction commit: <T , commit>
i
42. Transaction Logging
Transaction ID
Data item ID
ā¢ Transaction start: <T , start>
i
ā¢ Transaction write: <T , X ,V ,V >
i j 1 2
ā¢ Transaction commit: <T , commit>
i
Data values before
and after write
44. Transaction Logging
Transaction, āTā
<T, start>
read(Alice)
Alice = Alice - 100
<T, Alice, 2000, 1900>
write(Alice)
read(Bob)
Bob = Bob + 100
write(Bob) <T, Bob, -50, 50>
<T, commit>
45. Transaction Logging
Transaction, āTā
<T, start>
read(Alice)
Alice = Alice - 100
<T, Alice, 2000, 1900>
write(Alice)
read(Bob)
Bob = Bob + 100
write(Bob) <T, Bob, -50, 50>
<T, commit>
46. Transaction Logging
Transaction, āTā
<T, start>
read(Alice)
Alice = Alice - 100
<T, Alice, 2000, 1900>
write(Alice)
read(Bob)
Bob = Bob + 100
write(Bob) <T, Bob, -50, 50>
<T, commit>
47. Transaction Logging
Transaction, āTā
<T, start>
read(Alice)
Alice = Alice - 100
<T, Alice, 2000, 1900>
write(Alice)
read(Bob)
Bob = Bob + 100
write(Bob) <T, Bob, -50, 50>
<T, commit>
48. Deferred Modiļ¬cation
ā¢ Ensure atomicity by deferring all writes
until partial commit of transaction
ā¢ <T , commit> is a partial commit (only
i
affected log so far, not database)
ā¢ Given logs are in stable storage, writes can
be made to the database itself !
49. Failure Recovery
ā¢ If the system fails we can use the
transaction log to recover the database
ā¢ For all <T , start>, <T , commit> in log redo
i i
writes sequentially
55. Immediate Modiļ¬cation
ā¢ Instead of waiting for a commit, we could
just perform the writes as they happen (but
still post-log writing)
ā¢ Incomplete transactions written to
database require old value V1
56. Failure Recovery
ā¢ Again, if the system fails we use the
transaction log to recover the database
ā¢ For all <T , start>, <T , commit> in log redo
i i
database write
ā¢ For all <T , start>, without <T , commit> in
i i
log undo write
63. Check-points
ā¢ Speed up recovery!
ā¢ Create a ācheck-pointā to reduce work to
be done given a failure
ā¢ Flush all buffers to disk
ā¢ Write <checkpoint> to log
ā¢ On failure: read log until <checkpoint>
found, then start recovery