Lecture 20: Transactions - GitHub Pages · Lecture 20 > Section 1 > Motivation: Recovery &...

52
Lecture 20: Transactions Lecture 20

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

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

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

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

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

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

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

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

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

• 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

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

• 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

LoggingSummary

• IfDBsaysTXcommits,TXeffectremains afterdatabasecrash

• DBcanundoactionsandhelpuswithatomicity

• Thisisonlyhalfthestory…

Lecture20>Section3>Loggingcommitprotocol