Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery &...
Transcript of Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery &...
![Page 1: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/1.jpg)
Lecture20:Transactions
Lecture20
![Page 2: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/2.jpg)
Announcements• MostimportantlessonfromProject3!??!!
• Project2gradesshouldbeup• Letusknowofquestions
• Project4comingoutlatertoday:• Re-useyourdatabasefromProject1andaddanicewebinterfaceontoptosupporttransactions.
• Evaluationshouldbeopenpleasedonotforget
• December6th:GuestlecturebyStratis Viglas fromGoogle• ATTEND!!!
2
Lecture20
![Page 3: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/3.jpg)
Goalsforthispairoflectures
• Transactions areaprogrammingabstractionthatenablestheDBMStohandlerecovery andconcurrency forusers.
• Application:Transactionsarecriticalforusers• Evencasualusersofdataprocessingsystems!
• Fundamentals:Thebasicsofhow TXNswork• Transactionprocessingispartofthedebatearoundnewdataprocessingsystems
• GiveyouenoughinformationtounderstandhowTXNswork,andthemainconcernswithusingthem
Lectures20&21
![Page 4: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/4.jpg)
Lecture20:IntrotoTransactions&Logging
Lecture20
![Page 5: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/5.jpg)
Today’sLecture
1. Transactions
2. PropertiesofTransactions:ACID
3. Logging
5
Lecture20
![Page 6: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/6.jpg)
1.Transactions
6
Lecture20>Section1
![Page 7: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/7.jpg)
Whatyouwilllearnaboutinthissection
1. Our“model”oftheDBMS/computer
2. Transactionsbasics
3. Motivation:Recovery&Durability
4. Motivation:Concurrency[nextlecture]
7
Lecture20>Section1
![Page 8: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/8.jpg)
High-level:Diskvs.MainMemory
• Disk:
• Slow• Sequentialaccess
• (althoughfastsequentialreads)
• Durable• Wewillassumethatonceondisk,dataissafe!
• Cheap
8
Lecture20>Section3>Ourmodel
Platters
SpindleDisk head
Arm movement
Arm assembly
Tracks
Sector
Cylinder
![Page 9: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/9.jpg)
• RandomAccessMemory(RAM)orMainMemory:
• Fast• Randomaccess,byteaddressable
• ~10xfasterforsequentialaccess• ~100,000xfasterforrandomaccess!
• Volatile• Datacanbelostife.g.crashoccurs,powergoesout,etc!
• Expensive• For$100,get16GBofRAMvs.2TBofdisk!
9
Lecture20>Section3>Ourmodel
High-level:Diskvs.MainMemory
![Page 10: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/10.jpg)
Ourmodel:ThreeTypesofRegionsofMemory
1. Local: InourmodeleachprocessinaDBMShasitsownlocalmemory,whereitstoresvaluesthatonlyit“sees”
2. Global:Eachprocesscanreadfrom/writetoshareddatainmainmemory
3. Disk:Globalmemorycanreadfrom/flushtodisk
4. Log:Assumeonstablediskstorage- spansbothmainmemoryanddisk…
Local GlobalMain
Memory(RAM)
Disk
“Flushing todisk”=writingtodiskfrommainmemory
1 2
3
Lecture20>Section3>Ourmodel
Logisasequence frommainmemory->disk
4
![Page 11: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/11.jpg)
• Keepinmindthetradeoffshereasmotivationforthemechanismsweintroduce
• Mainmemory:fastbutlimitedcapacity,volatile
• Vs.Disk:slowbutlargecapacity,durable
11
Lecture20>Section3>Ourmodel
High-level:Diskvs.MainMemory
Howdoweeffectivelyutilizeboth ensuringcertaincriticalguarantees?
![Page 12: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/12.jpg)
Transactions
12
Lecture20>Section1>TransactionsBasics
![Page 13: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/13.jpg)
Transactions:BasicDefinition
Atransaction(“TXN”)isasequenceofoneormoreoperations (readsorwrites)whichreflectsasinglereal-worldtransition.
START TRANSACTIONUPDATE ProductSET Price = Price – 1.99WHERE pname = ‘Gizmo’
COMMIT
Lecture20>Section1>TransactionsBasics
Intherealworld,aTXNeitherhappenedcompletelyornotatall
![Page 14: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/14.jpg)
Transactions:BasicDefinition
Atransaction(“TXN”)isasequenceofoneormoreoperations (readsorwrites)whichreflectsasinglereal-worldtransition.
Lecture20>Section1>TransactionsBasics
Intherealworld,aTXNeitherhappenedcompletelyornotatall
Examples:
• Transfermoneybetweenaccounts
• Purchaseagroupofproducts
• Registerforaclass(eitherwaitlistorallocated)
![Page 15: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/15.jpg)
15
TransactionsinSQL
• In“ad-hoc”SQL:• Default:eachstatement=onetransaction
• Inaprogram,multiplestatementscanbegroupedtogetherasatransaction:
Lecture20>Section1>TransactionsBasics
START TRANSACTIONUPDATE Bank SET amount = amount – 100WHERE name = ‘Bob’UPDATE Bank SET amount = amount + 100 WHERE name = ‘Joe’
COMMIT
![Page 16: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/16.jpg)
ModelofTransactionforthisClass
Note:WeassumethattheDBMSonlyseesreadsandwritestodata
• Usermaydomuchmore
• Inrealsystems,databasesdohavemoreinfo...
Lecture20>Section1>TransactionsBasics
![Page 17: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/17.jpg)
MotivationforTransactionsGroupinguseractions(reads&writes)intotransactionshelpswithtwogoals:
1. Recovery&Durability:KeepingtheDBMSdataconsistentanddurableinthefaceofcrashes,aborts,systemshutdowns,etc.
2. Concurrency: AchievingbetterperformancebyparallelizingTXNswithout creatinganomalies
Lecture20>Section1>Motivation
Thislecture!
Nextlecture
![Page 18: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/18.jpg)
Motivation1.Recovery&Durability ofuserdataisessentialforreliableDBMSusage
• TheDBMSmayexperiencecrashes(e.g.poweroutages,etc.)
• IndividualTXNsmaybeaborted(e.g.bytheuser)
Lecture20>Section1>Motivation:Recovery&Durability
Idea:MakesurethatTXNsareeitherdurablystoredinfull,ornotatall;keeplogtobeableto“roll-back”TXNs
![Page 19: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/19.jpg)
19
Protectionagainstcrashes/aborts
Client 1:INSERT INTO SmallProduct(name, price)
SELECT pname, priceFROM ProductWHERE price <= 0.99
DELETE ProductWHERE price <=0.99
Whatgoeswrong?
Crash/abort!
Lecture20>Section1>Motivation:Recovery&Durability
![Page 20: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/20.jpg)
20
Protectionagainstcrashes/abortsClient 1:
START TRANSACTIONINSERT INTO SmallProduct(name, price)
SELECT pname, priceFROM ProductWHERE price <= 0.99
DELETE ProductWHERE price <=0.99
COMMIT OR ROLLBACK
Nowwe’dbefine!We’llseehow/whythislecture
Lecture20>Section1>Motivation:Recovery&Durability
![Page 21: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/21.jpg)
Motivation2.Concurrent executionofuserprogramsisessentialforgoodDBMSperformance.
• Diskaccessesmaybefrequentandslow- optimizeforthroughput(#ofTXNs),tradeforlatency(timeforanyoneTXN)
• UsersshouldstillbeabletoexecuteTXNsasifinisolation andsuchthatconsistencyismaintained
Idea:HavetheDBMShandlerunningseveraluserTXNsconcurrently,inordertokeepCPUshumming…
Lecture20>Section1>Motivation:Concurrency
![Page 22: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/22.jpg)
22
Multipleusers:singlestatements
Client 1: UPDATE ProductSET Price = Price – 1.99WHERE pname = ‘Gizmo’
Client 2: UPDATE ProductSET Price = Price*0.5WHERE pname=‘Gizmo’
Twomanagersattempttodiscountproductsconcurrently-Whatcouldgowrong?
Lecture20>Section1>Motivation:Concurrency
![Page 23: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/23.jpg)
23
Multipleusers:singlestatementsClient 1: START TRANSACTION
UPDATE ProductSET Price = Price – 1.99WHERE pname = ‘Gizmo’
COMMIT
Client 2: START TRANSACTIONUPDATE ProductSET Price = Price*0.5WHERE pname=‘Gizmo’
COMMIT
Nowworkslikeacharm- we’llseehow/whynextlecture…
Lecture20>Section1>Motivation:Concurrency
![Page 24: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/24.jpg)
2.PropertiesofTransactions
24
Lecture20>Section2
![Page 25: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/25.jpg)
Whatyouwilllearnaboutinthissection
1. Atomicity
2. Consistency
3. Isolation
4. Durability
5. ACTIVITY?
25
Lecture20>Section2
![Page 26: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/26.jpg)
26
TransactionProperties:ACID
• Atomic• Stateshowseitheralltheeffectsoftxn,ornoneofthem
• Consistent• Txn movesfromastatewhereintegrityholds,toanotherwhereintegrityholds
• Isolated• Effectoftxns isthesameastxns runningoneafteranother(ie lookslikebatchmode)
• Durable• Onceatxn hascommitted,itseffectsremaininthedatabase
ACIDcontinuestobeasourceofgreatdebate!
Lecture20>Section2
![Page 27: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/27.jpg)
27
ACID:Atomicity
• TXN’sactivitiesareatomic:allornothing
• Intuitively:intherealworld,atransactionissomethingthatwouldeitheroccurcompletely ornotatall
• TwopossibleoutcomesforaTXN
• Itcommits:allthechangesaremade
• Itaborts:nochangesaremade
Lecture20>Section2>Atomicity
![Page 28: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/28.jpg)
28
ACID:Consistency
• Thetablesmustalwayssatisfyuser-specifiedintegrityconstraints• Examples:
• Accountnumberisunique• Stockamountcan’tbenegative• Sumofdebitsandofcredits is0
• Howconsistencyisachieved:• Programmermakessureatxn takesaconsistentstatetoaconsistentstate• Systemmakessurethatthetxn isatomic
Lecture20>Section2>Consistency
![Page 29: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/29.jpg)
29
ACID:Isolation
• Atransactionexecutesconcurrentlywithothertransactions
• Isolation:theeffectisasifeachtransactionexecutesinisolation oftheothers.
• E.g.Shouldnotbeabletoobservechangesfromothertransactionsduringtherun
Lecture20>Section2>Isolation
![Page 30: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/30.jpg)
30
ACID:Durability
• TheeffectofaTXNmustcontinuetoexist(“persist”)aftertheTXN
• Andafterthewholeprogramhasterminated• Andeveniftherearepowerfailures,crashes,etc.• Andetc…
• Means:WritedatatodiskChangeonthehorizon?Non-VolatileRam(NVRam).Byteaddressable.
Lecture20>Section2>Durability
![Page 31: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/31.jpg)
ChallengesforACIDproperties
• Inspiteoffailures:Powerfailures,butnotmediafailures
• Usersmayaborttheprogram:needto“rollbackthechanges”• Needtolog whathappened
• Manyusersexecutingconcurrently• Canbesolvedvialocking(we’llseethisnextlecture!)
Andallthiswith…Performance!!
Lecture20>Section2
Thislecture
Nextlecture
![Page 32: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/32.jpg)
ANote:ACIDiscontentious!
• ManydebatesoverACID,bothhistoricallyand currently
• Manynewer“NoSQL”DBMSsrelaxACID
• Inturn,now“NewSQL”reintroducesACIDcompliancetoNoSQL-styleDBMSs…
Lecture20>Section2
ACIDisanextremelyimportant&successfulparadigm,butstilldebated!
![Page 33: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/33.jpg)
Goalforthislecture:EnsuringAtomicity&Durability• Atomicity:
• TXNsshouldeitherhappencompletelyornotatall
• Ifabort/crashduringTXN,no effectsshouldbeseen
33
Lecture20>Section3>Motivation&basics
ACID
TXN1
TXN2
No changespersisted
All changespersisted
We’llfocusonhowtoaccomplishatomicity(vialogging)
Crash/abort
• Durability:• IfDBMSstopsrunning,changesduetocompletedTXNsshouldallpersist
• Juststoreonstabledisk
![Page 34: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/34.jpg)
TheLog• Isalistofmodifications
• Logisduplexedandarchived onstablestorage.
• Canforcewrite entriestodisk• Apagegoestodisk.
• Alllogactivitieshandledtransparently theDBMS.
Assumewedon’tloseit!
Lecture20>Section3>Motivation&basics
![Page 35: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/35.jpg)
BasicIdea:(Physical)Logging• RecordUNDOinformationforeveryupdate!
• Sequentialwritestolog• Minimalinfo(diff)writtentolog
• Thelog consistsofanorderedlistofactions• Logrecordcontains:
<XID,location,olddata,newdata>
ThisissufficienttoUNDOanytransaction!
Lecture20>Section3>Motivation&basics
![Page 36: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/36.jpg)
Whydoweneedloggingforatomicity?• Couldn’twejustwriteTXNtodiskonly oncewholeTXNcomplete?
• Then,ifabort/crashandTXNnotcomplete,ithasnoeffect- atomicity!• Withunlimitedmemoryandtime,thiscouldwork…
• However,weneedtologpartialresultsofTXNs becauseof:• Memoryconstraints(enoughspaceforfullTXN??)• Timeconstraints(whatifoneTXNtakesverylong?)
Weneedtowritepartialresultstodisk!…Andsoweneedalog tobeabletoundo thesepartialresults!
Lecture20>Section3>Motivation&basics
![Page 37: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/37.jpg)
3.Atomicity&DurabilityviaLogging
37
Lecture20>Section3
![Page 38: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/38.jpg)
Whatyouwilllearnaboutinthissection
1. Logging:Ananimationofcommitprotocols
38
Lecture20>Section3
![Page 39: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/39.jpg)
APictureofLogging
Lecture20>Section3>Loggingcommitprotocol
![Page 40: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/40.jpg)
Apictureoflogging
DataonDisk
MainMemory
LogonDisk
LogT A=0
B=5
A=0
T:R(A),W(A)
Lecture20>Section3>Loggingcommitprotocol
![Page 41: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/41.jpg)
Apictureoflogging
DataonDisk
MainMemory
LogonDisk
LogT A=1
B=5
A=0
T:R(A),W(A)A:0à1
Lecture20>Section3>Loggingcommitprotocol
![Page 42: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/42.jpg)
Apictureoflogging
DataonDisk
MainMemory
LogonDisk
LogT A=1
B=5
A=0
T:R(A),W(A)A:0à1
Lecture20>Section3>Loggingcommitprotocol
Operationrecordedinloginmainmemory!
![Page 43: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/43.jpg)
Whatisthecorrectwaytowritethisalltodisk?• We’lllookattheWrite-AheadLogging(WAL)protocol
• We’llseewhyitworksbylookingatotherprotocolswhichareincorrect!
43
Remember:Keyideaistoensuredurabilitywhilemaintainingourabilityto“undo”!
![Page 44: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/44.jpg)
Write-AheadLogging(WAL)TXNCommitProtocol
Lecture20>Section3>Loggingcommitprotocol
![Page 45: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/45.jpg)
TransactionCommitProcess
1. FORCEWritecommit recordtolog
2. AlllogrecordsuptolastupdatefromthisTXareFORCED
3. Commit()returns
Transactioniscommittedoncecommitlogrecordisonstablestorage
Lecture20>Section3>Loggingcommitprotocol
![Page 46: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/46.jpg)
IncorrectCommitProtocol#1
DataonDisk
MainMemory
LogonDisk
LogT A=1
B=5
A=0
T:R(A),W(A) A:0à1
Lecture20>Section3>Loggingcommitprotocol
Let’strycommittingbefore we’vewritteneitherdataorlogtodisk…
Ifwecrashnow,isTdurable?
OK,Commit!
LostT’supdate!
![Page 47: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/47.jpg)
IncorrectCommitProtocol#2
DataonDisk
MainMemory
LogonDisk
LogT A=1
B=5
A=0
T:R(A),W(A) A:0à1
Lecture20>Section3>Loggingcommitprotocol
Let’strycommittingafter we’vewrittendatabutbefore we’vewrittenlogtodisk…
Ifwecrashnow,isTdurable?Yes!Except…
OK,Commit!
HowdoweknowwhetherTwascommitted??
![Page 48: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/48.jpg)
ImprovedCommitProtocol(WAL)
Lecture20>Section3>Loggingcommitprotocol
![Page 49: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/49.jpg)
Write-aheadLogging(WAL)CommitProtocol
DataonDisk
MainMemory
LogonDisk
LogT A=1
B=5
A=0
T:R(A),W(A) A:0à1
Lecture20>Section3>Loggingcommitprotocol
Thistime,let’strycommittingafter we’vewrittenlogtodiskbutbefore we’vewrittendatatodisk…thisisWAL!
Ifwecrashnow,isTdurable?
OK,Commit!
![Page 50: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/50.jpg)
Write-aheadLogging(WAL)CommitProtocol
DataonDisk
MainMemory
LogonDisk
T
A=0
T:R(A),W(A)
A:0à1
Lecture20>Section3>Loggingcommitprotocol
Thistime,let’strycommittingafter we’vewrittenlogtodiskbutbefore we’vewrittendatatodisk…thisisWAL!
Ifwecrashnow,isTdurable?
OK,Commit!
USETHELOG!A=1
![Page 51: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/51.jpg)
Write-AheadLogging(WAL)
• DBusesWrite-AheadLogging(WAL) Protocol:
1. Mustforcelogrecord foranupdatebefore thecorrespondingdatapagegoestostorage
2. MustwritealllogrecordsforaTXbefore commit
Lecture20>Section3>Loggingcommitprotocol
Eachupdateislogged!Whynotreads?
à Atomicity
à Durability
![Page 52: Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery & Durability Idea: Make sure that TXNs are either durably stored in full, or not at all; keep](https://reader034.fdocuments.us/reader034/viewer/2022043007/5f94d9c00bccc022ee19774e/html5/thumbnails/52.jpg)
LoggingSummary
• IfDBsaysTXcommits,TXeffectremains afterdatabasecrash
• DBcanundoactionsandhelpuswithatomicity
• Thisisonlyhalfthestory…
Lecture20>Section3>Loggingcommitprotocol