Transactions
SQL Writes
UPDATE Product�SET Price = Price – 1.99�WHERE pname = ‘Gizmo’
INSERT INTO SmallProduct(name, price)
SELECT pname, price� FROM Product
WHERE price <= 0.99
DELETE Product� WHERE price <=0.99
SQL Writes
UPDATE Product�SET Price = Price – 1.99�WHERE pname = ‘Gizmo’
INSERT INTO SmallProduct(name, price)
SELECT pname, price� FROM Product
WHERE price <= 0.99
DELETE Product� WHERE price <=0.99
How?
Example
Game App
DB v0
(Recap lectures)
DBMS
Real-Time
User Events
Report & Share�Business/Product Analysis
Q1: 1000 users/sec?
Q2: Offline?
Q3: Support v1, v1’ versions?
Q4: Which user cohorts?
Q5: Next features to build? Experiments to run?
Q6: Predict ads demand?
Mobile Game
Q7: How to model/evolve game data?
Q8: How to scale to millions of users?
Q9: When machines die, restore game state gracefully?
App designer
Systems designer
DB
Product/Biz designer
How?
Example
Game App
DB v0
(Recap lectures)
DBMS
Real-Time
User Events
Report & Share�Business/Product Analysis
Q1: 1000 users/sec?
Q2: Offline?
Q3: Support v1, v1’ versions?
Q4: Which user cohorts?
Q5: Next features to build? Experiments to run?
Q6: Predict ads demand?
Mobile Game
Q7: How to model/evolve game data?
Q8: How to scale to millions of users?
Q9: When machines crash, restore game state gracefully?
App designer
Systems designer
DB
Product/Biz designer
Today’s Lecture
Example
Unpack
ATM DB:
Transaction
Read Balance
Give money
Update Balance
Read Balance
Update Balance
Give money
vs
Visa does > 60,000 TXNs/sec with users & merchants
Want your 4$ Starbucks transaction to wait for a stranger’s 10k$ bet in Las Vegas ?
⇒ Transactions can (1) be quick or take a long time, (2) unrelated to you
Transactions are at the core of
-- payment, stock market, banks, ticketing
-- Gmail, Google Docs (e.g., multiple people editing)
Example Visa DB
Flush/Write to SSD or HDD
Visa
DB
Transaction Queue
Design#1 [RAM only]
For each Transaction in Queue
[Obviously bad → e.g., turn off power, lose $$s]
Design#2 [RAM + Write/Flush on HDD/SSD]
For each Transaction in Queue
[Let’s study tradeoffs in next few slides]
Example
Monthly
Visa bank interest transaction
‘T-Monthly-423’
Monthly Interest 10%
4:28 am Starts run on 100M bank accounts
Takes 24 hours to run
Money
Money (@4:29 am day+1)
UPDATE Money
SET Balance = Balance * 1.1
Example
Monthly bank interest transaction
With crash
‘T-Monthly-423’
Monthly Interest 10%
4:28 am Starts run on 100M bank accounts
Takes 24 hours to run
Network outage at 10:29 am,
System access at 10:45 am
Money
Money (@10:45 am)
??
??
??
??
Did T-Monthly-423 complete?
Which tuples are bad?
Case1: T-Monthly-423 crashed
Case2: T-Monthly-423 completed 4002 deposited 20$ at 10:45 am
Problem 1: Wrong :(
Example
Monthly Visa bank interest transaction
Performance
Money
Money (@4:29 am day+1)
Cost to update all data
100M bank accounts → 100M updates?
Worst case
(@10 msec/HDD seek, that’s 1 million secs)
(@10 musec/SSD access, ~1000 secs)
Problem2: SLOW to run :(
Problem3: How does user access money in this time?
Problems
Problem1: How can a {Bank, Visa, Fintech, NASDAQ} update 100 million records correctly?
Problem2: How to update 100 million records in Seconds (not 1000s to 1 million secs)?
Problem3: How to let users have access their accounts?
Problem4: Make it easy for the application engineer!!!
Preview – Our eventual solution (after 2 weeks)
Commit
WAL Flush to disk
Visa
DB
Transaction Queue
Design#3 VisaDB
For each Transaction in Queue
[Note: We’ll focus on the BLUE parts in next lectures]
Motivation for Transactions
Group user actions (reads & writes) into Transactions (TXNs) helps with two goals:
This lecture!
Next lecture
Idea: Use LOGS. Support to “commit” or “rollback” TXNs
Idea: Use LOCKS. Run several user TXNs concurrently.
Today’s Lecture
Transactions: Basic Definition
A transaction (“TXN”) is a sequence of one or more operations (reads or writes) which reflects a single real-world transition.
START TRANSACTION
UPDATE Product� SET Price = Price – 1.99� WHERE pname = ‘Gizmo’
COMMIT
In the real world, a TXN either happened completely or not at all (e.g., you withdrew 100$ from bank. Or not.)
Transactions in SQL
START TRANSACTION
UPDATE Bank SET amount = amount – 100
WHERE name = ‘Bob’
UPDATE Bank SET amount = amount + 100
WHERE name = ‘Joe’
COMMIT
Example 1: Protection against crashes / aborts
Client 1:
INSERT INTO CheapProduct(name, price)
SELECT pname, price� FROM Product
WHERE price <= 0.99
DELETE Product
WHERE price <=0.99
What goes wrong?
Crash / abort!
Scenario: Make a CheapProducts table, from a Products table
Client 1:
START TRANSACTION
INSERT INTO CheapProduct(name, price)
SELECT pname, price� FROM Product
WHERE price <= 0.99
DELETE Product� WHERE price <=0.99
COMMIT
Now we’d be fine! We’ll see how / why this lecture
Example 2: Multiple users: single statements
Client 1: [at 10:01 am]
UPDATE Product� SET Price = Price – 1.99� WHERE pname = ‘Gizmo’
��
Client 2: [at 10:01 am]
UPDATE Product� SET Price = Price*0.5� WHERE pname=‘Gizmo’
��
Two managers attempt to change prices at same time -
What could go wrong?
Client 1: START TRANSACTION
UPDATE Product� SET Price = Price – 1.99� WHERE pname = ‘Gizmo’
COMMIT��
Now works like a charm- we’ll see how / why next lecture…
Client 2: START TRANSACTION
UPDATE Product� SET Price = Price*0.5� WHERE pname=‘Gizmo’
COMMIT��
3. Properties of Transactions
What you will learn about in this section
ACID: Atomicity
ACID: Consistency
(E.g., Account number is unique, Sum of debits and of credits is 0)
ACID: Isolation
Conceptually,
ACID: Durability
ACID Summary
A Note: ACID is one popular option!
⇒ Usually, depends on what consistency and performance your application needs
ACID is an extremely important & successful paradigm, but still debated!
4. Atomicity & Durability via Logging
Conceptual
Idea:
Plan a trip with friends.
⇒ TODOs in NY, LA, SF, and Berlin
Execute your TODO list in random sequence, as soon as the ideas flow in from friends?
OR
Plan your TODOs in NY, in LA, in Berlin? And commit by buying Tickets?
Big Idea
LOGS!
(aka TODO/
ledger)
Recall (on disks)
Many kinds of LOGs. We’ll study a few key ones!
Big Idea: LOGs (or log files or ledger)
Basic Idea: (Physical) Logging
Idea:
<TransactionID, &reference, old value, new value>
(e.g., key)
What DB does?
This is sufficient to UNDO any transaction!
Couple of changes,
Planning ahead for next 4 weeks
Bigger CS class
Transactions are important, and tricky
Why are Transactions tricky, and important?
Problems
Problem1: How can a {Bank, Visa, Fintech, NASDAQ} update 100 million records correctly?
Problem2: How to update 100 million records in Seconds (not 1000s to 1 million secs)?
Problem3: How to let users have access their accounts?
Problem4: Make it easy for the application engineer!!!
Example1: “TXN-INC5”
1 TXN, 1 value
numLikes = 30 // Stored in an IO block in DB
numLikes++ 5 times
Possible values for numLikes: // Updated in IO block in DB
TXN Aborts (fails/crashes) ⇒ 30
TXN Commit (aka succeeds) ⇒ 35
End to end Example
Key questions
Recall: A TXN can either COMMIT or ABORT. DB guarantees below, whether the TXN succeeds or fails.
For 1 TXN, 1 value, we saw a full run. But . . .
⇒ Let’s focus on A and D in ACID.
Example1
Review TXN if there’s a crash
How to recover?
Extending to 1 TXN, 2 values
Extension #1
7
7
7
13
Consider updating <numLikes, numViews>.
⇒ Same process as earlier. (2) Read each value from Blocks, (3) update in LOG.
numViews = f(...)
numViews = f(...)
7 -> 13
General Algorithm. . .
WAL Logging
Definitions
<Tid, &numLikes, 31,32>
Update record
(aka Undo or Undo/Redo logs)
COMMIT record
Flushing to disk
Transaction from DB’s POV
What if no more space in RAM? Or in LOG-RAM?
Extension #2
We may need to append partial results of TXNs on LOG-DISK due to
⇒ We could use the same LOG-DISK for UNDOing partial TXNs. That is, we use the LOG for Atomicity (in addition to Durability).
Formalizing
Write-Ahead Logging (WAL)�TXN Commit Protocol
Write-Ahead Logging (WAL)
→ Atomicity
→ Durability
Data Flush
Flush Update
Record to LOG-Disk
Rule1: For each tuple update
Flush COMMIT Record to LOG-Disk
TXN
COMMIT
Rule2: Before TXN commits
Time > 0
Time > 0
Algorithm: WAL
For each tuple update, write Update Record into LOG-RAM
Follow two Flush rules for LOG
Transaction is committed once COMMIT record is on stable storage
Note: fixing an unintended visual association. We get the combo of Durabiity + Atomicity, from Rules 1 and 2.Adding a ‘curly brace’
Records
Logs
A, B
R(A), R(B)
A, B
W(A), W(B)
A’, B’
U(A’)
…
Start TXN
U(A’)
U(B’)
A’, B
U(B’)
A’, B’
CommitRecord
CommitRecord
COMMIT TXN
1. Can A’ be flushed to disk before U(A’)?
2. Can A’ be flushed to disk before CommitRecord?
3. Can B’ be flushed to disk after CommitRecord?
UpdateRecord U(A’) for
<A, A’, TXN>
Example WAL Sequence
Time
Later …
No [rule 1]
Yes
Yes
Rows A, B are in 2 IOblocks
Example: Bank Transaction
Example
Monthly bank interest transaction
Full run
‘T-Monthly-423’
Monthly Interest 10%
4:28 am Starts run on 100M bank accounts
Takes 24 hours to run
WA Log (@4:29 am day+1)
Money
Money (@4:29 am day+1)
START TRANSACTION
UPDATE Money� SET Amt = Amt * 1.10
COMMIT
Update
Records
Commit
Record
Example
Monthly bank interest transaction
With crash
TXN ‘T-Monthly-423’
Monthly Interest 10%
4:28 am Starts run on 100M bank accounts
Takes 24 hours to run
Network outage at 10:29 am,
System access at 10:45 am
Money
Money (@10:45 am)
WA Log (@10:29 am)
??
??
??
??
Did T-Monthly-423 complete?
Which tuples are bad?
Case1: T-Monthly-423 was crashed
Case2: T-Monthly-423 completed. 4002 deposited 20$ at 10:45 am
Can you infer from RED Update logs?
⇒ Yup. There is NO TXN Commit.As part of recovery, undo any data updates already flushed.
Example
Monthly bank interest transaction
Recovery
Money (@10:45 am)
Money (after recovery)
WA Log (@10:29 am)
System recovery (after 10:45 am)
- Restore old values from Log-Disk (if any)
- Notify developers about ABORTed TXN
Example
Monthly bank interest transaction
Performance
WAL (@4:29 am day+1)
Money
Money (@4:29 am day+1)
Cost to update all data
100M bank accounts → 100M seeks? (worst case)
Cost to Append to log
+ 1 access to get ‘end of log’
+ write 100M log entries sequentially
(fast!!! < 10 sec)
[Lazily update data on disk later,
when convenient.]
(@10 msec/seek, that’s 1 Million secs)
Extensions of TXNs
LOG on Cluster model
A popular alternative (with tradeoffs)
Commit
WAL Flush to HDD/SSD
COMMIT by flushing log and/or data to ‘n’ other machines (e.g. n =2 )
[On same rack, different rack or different datacenter]
Example
Cluster LOG model
Performance
Failure model
Main model: RAM could fail, Disk is durable
VS
Cluster LOG model:
RAM on different machines don’t fail at same time
Power to racks is uncorrelated
Example: Youtube DB
Example
Youtube writes
Performance
Critique?
Correct?
Write Speed? Cost? Storage?
Bottlenecks?
Design 1: WAL Log for Video Likes
<videoid, numLikescount, new numLikes count>
Example: Increment number of LIKEs per Video
Example
Youtube writes
Performance
Critique?
Correct?
Write Speed? Cost? Storage?
Bottlenecks?
System recovery?
Design 2:
Flush to disk
Log “+n”
Update RAM on
n=3 machines
(<videoid, #likes>)
Example
Youtube writes
Performance
Vs Cost
Critique?
Correct?
Write Speed? Cost? Storage?
Bottlenecks?
System recovery?
Popular video
World’s most boring video (Unpopular)
Design #3
For most videos, Design 1 (full WAL logs)
For popular videos, Design 2
Summary
Design Questions?
Correctness: Need true ACID? Pseudo-ACID? What losses are OK?
Design parameters:
Any data properties you can exploit? (e.g., ‘+1’, popular vs not)
How much RAM, disks and machines?
How many writes per sec?
How fast do you want system to recover?
Choose: WAL logs, Replication on n-machines, Hybrid? (More in cs345)
Transactions
Summary
Why study Transactions?
Good programming model for parallel applications on shared data !
Atomic
Consistent
Isolation
Durable
Write Logs
WAL
RAM
Cluster
Serializable
Conflict
Serializable
S2PL
Note: this is an intro
Next: Take 346 (Distributed Transactions) or read Jim Gray’s classic
Next Lecture
Logging Summary