cs 839: design the next-generation database lecture 2:...

48
Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation Database Lecture 2: Transaction Basics 1

Upload: others

Post on 17-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Xiangyao Yu1/23/2020

CS 839: Design the Next-Generation DatabaseLecture 2: Transaction Basics

1

Page 2: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

AnnouncementsCourse website

http://pages.cs.wisc.edu/~yxy/cs839-s20/index.html

Email me if you are not in HotCRPhttps://wisc-cs839-ngdb20.hotcrp.com

2

Page 3: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Today’s AgendaOLTP vs. OLAP

ACID properties• Atomicity

• Consistency

• Isolation

• Durability

3

Page 4: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

OLTP vs. OLAPOLTP: On-Line Transaction Processing• Users submit transactions that contain simple read/write operations• Example: banking, online shopping, etc.

OLAP: On-Line Analytical Processing• Complex analytics queries that reveal insights behind data• Example: business report, marketing, forecasting, etc.

4

Page 5: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

OLTP vs. OLAP

OLTP database

Transactions

(Update Intensive)

Page 6: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

OLTP vs. OLAP

OLTP database

Transactions

OLAP database(Update Intensive) (Read Intensive, rare updates)

6

Page 7: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

OLTP vs. OLAP

OLTP database

Transactions

OLAP database(Update Intensive) (Read Intensive, rare updates)

• Takes hours for conventional databases

• Takes seconds for Hybrid transactional/analytical processing (HTAP) systems

7

Page 8: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

OLTP vs. OLAP

OLTP database OLAP database(Update Intensive) (Read Intensive, rare updates)

This lecture Next lecture

8

Page 9: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Transaction Definition

9

Page 10: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Transaction DefinitionWhat are the required properties of a database transaction?

ACID

10

Page 11: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Transaction DefinitionWhat are the required properties of a database transaction?

ACID

Example transactions:

If checking.balance > 100checking.balance -= 100saving.balance += 100

BeginRead(X)Write(Y)Insert(Z)Commit

11

Page 12: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Atomicity

If checking.balance > 100

checking.balance -= 100

saving.balance += 100

Atomicity: Either all operations occur, or nothing occurs (All or nothing)

12

Page 13: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: ConsistencyIf checking.balance > 100

checking.balance -= 100saving.balance += 100

Consistency: Integrity constraints must be maintained.

Example Integrity constraint: balance of checking account must be above $0

Checking

Saving

$50

$1000

-$50

$1100RunTxn()

13

Page 14: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: DurabilityExample transaction:

If checking.balance > 100checking.balance -= 100saving.balance += 100

Checking

Saving

$1000

$1000

14

Page 15: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: DurabilityExample transaction:

If checking.balance > 100checking.balance -= 100saving.balance += 100

Checking

Saving

$1000

$1000

$900

$1100RunTxn()

15

Page 16: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: DurabilityExample transaction:

If checking.balance > 100checking.balance -= 100saving.balance += 100

Checking

Saving

$1000

$1000

$900

$1100RunTxn()

CRASH !!

???

???

16

Page 17: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: DurabilityExample transaction:

If checking.balance > 100checking.balance -= 100saving.balance += 100

Durability: A transaction’s updates persist in case of system failure

Checking

Saving

$1000

$1000

$900

$1100RunTxn()

CRASH !!

???

???

17

Page 18: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Isolation

Initaillychecking.balance = 1000

18

Page 19: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Isolation

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

Initaillychecking.balance = 1000

19

Page 20: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Isolation

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

1

Initaillychecking.balance = 1000

bal = 1000

20

Page 21: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Isolation

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

1 2

Initaillychecking.balance = 1000

bal = 1000 bal = 1000

21

Page 22: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Isolation

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

1 2

Initaillychecking.balance = 1000

bal = 1000 bal = 1000

bal = 900 bal = 900

22

Page 23: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Isolation

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

1 2

3

Initaillychecking.balance = 1000

bal = 1000 bal = 1000

bal = 900 bal = 900

checking = 90023

Page 24: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Isolation

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

If checking.balance > 100

bal = checking.balance

bal = bal – 100

checking.balance = bal

1 2

3 4

Initaillychecking.balance = 1000

bal = 1000 bal = 1000

bal = 900 bal = 900

checking = 900 checking = 90024

Page 25: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: IsolationStrong isolation: Serializability (Focus of this course)

Weak isolation: Snapshot Isolation, Read Committed, Read Uncommitted, etc.

Weaker isolation levels allow more interleaving of transactions

Schedule of concurrent transactions is equivalent to some serial schedule

25

Page 26: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

ACID: Isolation – Why Strong Isolation?

Attackers stole 896 Bitcoins ≈ 3 million US dollars

April 2014

MongoDB & Bitcoin: How NoSQL design flaws brought down two exchanges

Why you should pick strong consistency, whenever possible

Systems that don't provide strong consistency … create a burden for application developers

January 2018

SQL (before 2000) -> NoSQL (since 2000) -> NewSQL (since 2010s) 26

Page 27: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

How to Enforce ACIDAtomic & Isolation: Concurrency control

Consistency: Check integrity for transactions

Durability: Logging

27

Page 28: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Concurrency Control

Pessimistic Optimistic

28

Page 29: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Two Phase Locking (2PL)T1

Begin

Read(X)

Write(Y)

Commit

29

Time

Page 30: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Two Phase Locking (2PL)T1

Begin

Read(X)

Write(Y)

Commit

30

Time

Page 31: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Two Phase Locking (2PL)T1

Begin

Read(X)

Write(Y)

Commit

31

Time

Page 32: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Two Phase Locking (2PL)T1

Begin

Read(X)

Write(Y)

Commit

Begin

Write(X)

T2

32

Time

Page 33: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Two Phase Locking (2PL)T1

Begin

Read(X)

Write(Y)

Commit

Begin

Write(X)

T2

33

Time

Page 34: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Two Phase Locking (2PL)T1

Begin

Read(X)

Write(Y)

Commit

Begin

Write(X)

Commit

T2

34

Time

Page 35: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Deadlock in 2PLT1

Begin

Read(X)

Write(Y)

Begin

Write(Y)

Write(X)

T2

35

Time

Page 36: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Deadlock in 2PLT1

Begin

Read(X)

Write(Y)

Begin

Write(Y)

Write(X)

T2

36

Time

Page 37: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Deadlock in 2PLT1

Begin

Read(X)

Write(Y)

Begin

Write(Y)

Write(X)

T2

37

Time

Page 38: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Deadlock in 2PLT1

Begin

Read(X)

Write(Y)

Begin

Write(Y)

Write(X)

T2

38

Time

Page 39: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Deadlock in 2PLT1

Begin

Read(X)

Write(Y)

Begin

Write(Y)

Write(X)

T2

39

Time

Page 40: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Deadlock in 2PLT1

Begin

Read(X)

Write(Y)

Begin

Write(Y)

Write(X)

T2

Deadlock ! 40

Time

Page 41: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Pessimistic – Deadlock ResolutionT1

Begin

Read(X)

Write(Y)

Begin

Write(Y)

Write(X)

T2

Deadlock !

Detect and break cycles

Allow only certain waits• NoWait• WaitDie• WoundWait• LimitedDepth• etc.

41

Wait-for

Wait-for

Page 42: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Optimistic Concurrency Control (OCC)T1

Begin

Read(X)

Write(Y)

Validate(X)

Abort

Begin

Write(X)

Commit

T2

42

Time

Page 43: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

How to Enforce ACIDAtomic & Isolation: Concurrency control

Consistency: Check integrity for transactions

Durability: Logging

43

Page 44: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Durability: Logging

Begin

Initiallychecking = 1000

Write(Checking =

900)Commit

CRASH

Recovery Read(Checking = 900)

44

Page 45: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Durability: Logging

Begin

Initiallychecking = 1000

Write(Checking =

900)Commit

CRASH

Recovery Read(Checking = 900)

Logging

T1 T2 T3 T4 T5Log(on disk)

…45

Page 46: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

SummaryA transaction in an OLTP system has ACID properties

Atomicity, Consistency, Isolation, Durability

Concurrency control (enforces Atomicity and Isolation)Two Phase Locking (2PL)Optimistic Concurrency Control (OCC)

Logging (enforces Durability)

46

Page 47: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Group DiscussionDiscuss the relative advantages and disadvantages of 2PL and OCC.

What is your opinion on the debate between SQL and NoSQL (strong guarantee vs. high performance)?

Do you see any scalability problem with logging? If so, any potential solution?

47

Page 48: CS 839: Design the Next-Generation Database Lecture 2: …pages.cs.wisc.edu/~yxy/cs839-s20/slides/L2.pdf · 2020-01-23 · Xiangyao Yu 1/23/2020 CS 839: Design the Next-Generation

Before Next LectureSubmit discussion summary to https://wisc-cs839-ngdb20.hotcrp.com• One summary per group• Authors: group members• Any format is ok (e.g., pdf, doc, txt)• Feel free to comment on others’ discussion

Submit review for[optional] C-Store: A Column-oriented DBMS

48