Transactions and Concurrency Control Distribuerade Informationssystem, 1DT060, HT 2013 Adapted from, Copyright, Frederik Hermans.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Advertisements

1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
CSC271 Database Systems Lecture # 32.
1 CS 194: Elections, Exclusion and Transactions Scott Shenker and Ion Stoica Computer Science Division Department of Electrical Engineering and Computer.
Lock-Based Concurrency Control
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
CMPT 401 Summer 2007 Dr. Alexandra Fedorova Lecture X: Transactions.
Database Systems, 8 th Edition Concurrency Control with Time Stamping Methods Assigns global unique time stamp to each transaction Produces explicit.
Quick Review of Apr 29 material
CMPT Dr. Alexandra Fedorova Lecture X: Transactions.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Distributed Systems Fall 2010 Transactions and concurrency control.
Transaction Management and Concurrency Control
Transaction Management and Concurrency Control
Persistent State Service 1 Distributed Object Transactions  Transaction principles  Concurrency control  The two-phase commit protocol  Services for.
©Silberschatz, Korth and Sudarshan15.1Database System ConceptsTransactions Transaction Concept Transaction State Implementation of Atomicity and Durability.
Chapter 8 : Transaction Management. u Function and importance of transactions. u Properties of transactions. u Concurrency Control – Meaning of serializability.
Transaction Processing: Concurrency and Serializability 10/4/05.
Transaction Management
1 Transaction Management Database recovery Concurrency control.
1 Minggu 8, Pertemuan 15 Transaction Management Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Database Management Systems I Alex Coman, Winter 2006
9 Chapter 9 Transaction Management and Concurrency Control Hachim Haddouti.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Distributed Systems Fall 2009 Distributed transactions.
Transaction. A transaction is an event which occurs on the database. Generally a transaction reads a value from the database or writes a value to the.
Transactions and concurrency control
Transactions and Recovery
TRANSACTIONS AND CONCURRENCY CONTROL Sadhna Kumari.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Transaction Processing Concepts
08_Transactions_LECTURE2 DBMSs should guarantee ACID properties (Atomicity, Consistency, Isolation, Durability). This is typically done by guaranteeing.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Transaction Communications Yi Sun. Outline Transaction ACID Property Distributed transaction Two phase commit protocol Nested transaction.
Distributed Transactions
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Concurrency Control Steve Ko Computer Sciences and Engineering University at Buffalo.
Concurrency Control Lectured by, Jesmin Akhter, Assistant professor, IIT, JU.
Chapter 11 Concurrency Control. Lock-Based Protocols  A lock is a mechanism to control concurrent access to a data item  Data items can be locked in.
Transactions CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley 2002)
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
Concurrency Control in Database Operating Systems.
II.I Selected Database Issues: 2 - Transaction ManagementSlide 1/20 1 II. Selected Database Issues Part 2: Transaction Management Lecture 4 Lecturer: Chris.
XA Transactions.
Distributed Transactions Chapter – Vidya Satyanarayanan.
Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness.
Transactions and Concurrency Control Fall 2007 Himanshu Bajpai
CSIS 7102 Spring 2004 Lecture 3 : Two-phase locking Dr. King-Ip Lin.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
IM NTU Distributed Information Systems 2004 Distributed Transactions -- 1 Distributed Transactions Yih-Kuen Tsay Dept. of Information Management National.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
9 1 Chapter 9_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
Lecture 8 Transactions & Concurrency UFCE8K-15-M: Data Management.
Multidatabase Transaction Management COP5711. Multidatabase Transaction Management Outline Review - Transaction Processing Multidatabase Transaction Management.
3 Database Systems: Design, Implementation, and Management CHAPTER 9 Transaction Management and Concurrency Control.
Distributed Transactions What is a transaction? (A sequence of server operations that must be carried out atomically ) ACID properties - what are these.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Atomic Tranactions. Sunmeet Sethi. Index  Meaning of Atomic transaction.  Transaction model Types of storage. Transaction primitives. Properties of.
Transaction Management
Transaction Management and Concurrency Control
Database Management System
Part- A Transaction Management
Two phase commit.
Chapter 10 Transaction Management and Concurrency Control
Lecture 21: Concurrency & Locking
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Distributed Transactions
Transaction management
UNIVERSITAS GUNADARMA
Transaction Communication
Transactions, Properties of Transactions
Presentation transcript:

Transactions and Concurrency Control Distribuerade Informationssystem, 1DT060, HT 2013 Adapted from, Copyright, Frederik Hermans

Concurrency concurrency noun \kən- ˈ kə-rən(t)-sē\ the simultaneous occurrence of events or circumstances Happens at various scales Enables efficient use of resources

A Christmas example Your grandmother puts 1000 SEK in your account. Your uncle puts 500 SEK in your account. y 1 =readBalance(Y) g=readBalance(G) g=g-1000 y 1 =y writeBalance(Y,y 1 ) writeBalance(G,g) y 2 =readBalance(Y) u=readBalance(U) u=u-500 y 2 =y writeBalance(Y,y 2 ) writeBalance(U,u)

Uncontrolled concurrency What if both execute the transfer at the same time? Operations of the transactions are interleaved y 1 =readBalance(Y) g=readBalance(G) y 2 =readBalance(Y) u=readBalance(U) g=g-1000 u=u-500 y 1 =y y 2 =y writeBalance(Y,y 1 ) writeBalance(G,g) writeBalance(Y,y 2 ) writeBalance(U,u) Your account is only credited 500 SEK! Oops!

Dealing with concurrency Uncontrolled concurrency can (and has) lead to severe problems Problems often hard to reproduce Understanding concurrency is a crucial skill for any designer of a computer system

Outline Motivation Transactions & ACID properties Isolation  Conflicts & serializability  2-phase locking  Deadlocks Atomicity  2-phase commit protocol

Transactions Definition: A transaction is a sequence of operations that must to be treated as an undivided unit. Operations read and write  read(X): access resource X without modifying it  write(X): modify resource X A transaction must be either committed or aborted Server Client read(X) read(Y) write(X) write(Y) commit

ACID properties Desirable properties for transactions Atomicity Consistency Isolation Durability

read(X) read(Y) write(X) write(Y) commit Atomicity “Either all or none of the effects a transaction are applied” Ancient greek ἄ τομος (atomos): indivisible If a transaction commits, all of its effects are visible If a transaction aborts, none of its effects are visible Server ✓ ✓ ✓ After reboot of server, modifications of X must not be visible!

Consistency “Transactions maintain any internal invariant” Invariant: a property of a system that always holds true Consistent ⇔ all invariants hold withdraw 5 trillion SEK from account G Client Server abort Transaction would violate the invariant “Balance on G must be larger than 0.”

Isolation “Concurrent transactions do not interfere with each other.” Each transaction is executed as if it was the only transaction Initial example violates isolation property read(X) read(Y) read(X) read(Z) write(X) write(Y) write(X) write(Z) commit T 1 T 2

Durability “The effects of a committed transaction are permanent.” If a server crashes and restarts, the effects of all committed transactions must be preserved Requires permanent storage (hard disks)

Quiz 1 Are the following statements true or false? Uncontrolled concurrent execution can cause unintended behavior in a computer system. A transaction is a sequence of operations. A system should avoid to implement ACID properties. The isolation property requires that concurrent transactions do not interfere with each other.

Outline Motivation Transactions & ACID properties Isolation  Conflicts & serializability  2-phase locking  Deadlocks Atomicity  2-phase commit protocol

Conflicting operations What was the problem in the initial example?  Because of concurrency, T2 overwrote effect of T1  Isolation property was violated “Conflicting” operations “Conflict” means things may go wrong  They don’t have to!  We cannot get rid of conflicts, but need to handle them read(X)write(X) read(X) ✓ Conflict write(X) Conflict T2 does T1 does

Conflicting operations, example Conflicts exist regardless of ordering! read(X) read(Y) write(X) write(Y) T1 read(X) read(Y) T2 read(X) read(Y) write(X) read(Y) write(Y) Interleaving 1 read(X) read(Y) write(X) write(Y) read(Y) Interleaving 2 read(X) read(Y) write(X) read(X) write(Y) read(Y) Interleaving 3 Two conflicts  Conflict 1: write(X), read(X)  Conflict 2: write(Y), read(Y)

Conflict ordering Conflicts exist regardless of ordering  We cannot get rid of them! But what does the ordering tell us? read(X) read(Y) write(X) read(Y) write(Y) read(X) read(Y) write(X) write(Y) read(Y) read(X) read(Y) write(X) read(X) write(Y) read(Y) Conflict 1: T2 before T1 Conflict 2: T2 before T1 Conflict 1: T2 before T1 Conflict 2: T1 before T2 Conflict 1: T1 before T2 Conflict 2: T1 before T2 Same as T2 T1!Same as T1 T2!??? Something weird is going on here!

Serializability If the order on all conflicting operations is the same,...  I.e., T1’s operation first for all conflicts  or T2’s operation first for all conflicts... then the transactions are isolated!  they have the same effect as if the transactions were executed after one another! Definition: An interleaving is called serializable if all conflicting operations are executed in the same order

More examples read(X) read(Y) write(X) write(Y) T1 read(X) read(Y) T2 Two conflicts  Conflict 1: write(X), read(X)  Conflict 2: write(Y), read(Y) read(X) read(Y) write(X) write(Y) Interleaving 4 read(X) read(Y) write(X) write(Y) read(X) read(Y) Interleaving 5 read(X) read(Y) read(X) write(X) write(Y) read(Y) Interleaving 6 ✓ Serializable ✓ Serializable X Not serializable

Back to the first example y 1 =readBalance(Y) g=readBalance(G) y 2 =readBalance(Y) u=readBalance(U) g=g-1000 u=u-500 y 1 =y y 2 =y writeBalance(Y,y 1 ) writeBalance(G,g) writeBalance(Y,y 2 ) writeBalance(U,u) Two conflicts  Conflict 1: read(Y), write(Y)  Conflict 2: read(Y), write(Y) Conflict 1: Grandma before uncle Conflict 2: Uncle before grandma X Not serializable

Non-solution: Global lock Idea: To execute a transaction, a client acquires a global lock Only two possible interleavings  read(A),read(B),write(A),write(B), read(A),read(B)  read(A),read(B), read(A),read(B),write(A),write(B) Good: They are serializable! Very very bad: Global locks prevent all concurrency read(X) read(Y) write(X) write(Y) T1 read(X) read(Y) T2 Lock - do not enter until T2 is finished. Lock - do not enter until T1 write are finished.

Locks System-wide locking disallows concurrency More fine-grained locks per resource  Read lock: rlock(X)  Write lock: wlock(X)  Unlock: unlock(X) (for both lock types) If lock cannot be acquired, wait until it is available No lockRead lockWrite lock Read lock ✓✓ Wait Write lock ✓ Wait Another transaction has We want

2-phase locking Each transaction goes through two phases 1. A growing phase, in which it may acquire locks 2. A shrinking phase, in which it releases locks Always creates a serializable interleaving! Number of locks Time Phase 1Phase 2

Example Execute T1, T2 using 2-phase locking read(X) read(Y) write(X) write(Y) T1 read(X) read(Y) T2 rlock(X) read(X) rlock(X) read(X) rlock(Y) read(Y) wlock(X) rlock(Y) read(Y)... unlock(X) unlock(Y) write(X) wlock(Y) write(Y) unlock(X) unlock(Y) Time wait!

Deadlock What if two (or more) transactions wait for each other to release a lock? rlock(X) read(X) rlock(Y) read(Y) wlock(Y) wlock(X) wait! Both transactions will wait forever!

Wait-for graph  Each transaction is a node  Edge from T to U if T waits for a lock held by U Deadlock occurs if wait-for graph has a cycle T1 T2 T3 Deadlock! T1 T2 T3 No deadlock To resolve a deadlock, abort any one transaction in the cycle

Beyond 2-phase locking 2-phase locking ensures serializable interleavings Problems with 2-phase locking  Deadlocks can occur  Can be inefficient for write transactions given long read- only transaction Non-locking approaches  Optimistic concurrency control

Quiz time!

Quiz 2 Are the following statements true or false? If two transactions both execute write(X), then these two write operations are in conflict. An interleaving is called serializable if all pairs of conflicting operations are executed in the same order. Global locks are an efficient means of ensuring isolation. 2-phase locking ensures isolation. If two transactions are in a deadlock, only one of them will wait, while the other one continues.

Outline Motivation Transactions & ACID properties Isolation  Conflicts & serializability  2-phase locking  Deadlocks Atomicity  2-phase commit protocol

Atomicity in distributed systems Transaction involves multiple servers Server 1 Server 2 write(X) write(Y) Coordinator Client accesses servers via a coordinator What about atomicity? Client write(X) write(Y)

2-phase commit protocol Client asks coordinator to commit Must reach consensus on commit or abort  Even in the presence of failures! 2-phase commit protocol  Protocol to ensure atomic distributed transactions  Phase 1: Voting  Phase 2: Completion (2-phase commit ≠ 2-phase locking)

Phase 1: voting Coordinator sends “can commit?” message to servers A server that cannot commit sends “no” A server that can commit sends “yes”  Before it sends “yes”, it must save all modifications to permanent storage Server 1 Server 2 Coordinator can commit? no yes

Phase 2: completion Coordinator collects votes (a) Failure or a “no” vote → coordinator sends “do abort” to all servers that have voted “yes” (b) All “yes” → coordinator sends “do commit” to all servers Servers handle “do abort” or “do commit”, respectively

Phase 2: completion Coordinator collects votes (a) Failure or a “no” vote → coordinator sends “do abort” to all servers that have voted “yes” (b) All “yes” → coordinator sends “do commit” to all servers Servers handle “do abort” or “do commit”, respectively Server 1 Server 2 Coordinator no yes do abort

Phase 2: completion Coordinator collects votes (a) Failure or a “no” vote → coordinator sends “do abort” to all servers that have voted “yes” (b) All “yes” → coordinator sends “do commit” to all servers Servers handle “do abort” or “do commit”, respectively Server 1 Server 2 Coordinator yes do commit

Failures A server crashes (and reboots) before voting  Server votes “no” after reboot  Or: coordinator times out waiting, sends “do abort” to all A server crashes (and reboots) after voting “no”  No problem A server crashes (and reboots) after voting “yes”  Server restores from permanent storage, waits for instruction from coordinator  Or: coordinator times out waiting, sends “do abort” to all The coordinator crashes after phase 1...

Server uncertainty Server that voted “yes” cannot abort  “Yes” means “I promise to commit when you ask me to.” Server uncertainty: time between a server’s “yes” vote and the coordinator’s “do commit” or “do abort” request  Server may not unlock resources! Coordinator yes can commit? Server 2 do abort ???

Quiz time!

Quiz 3 Are the following statements true or false? The purpose of the 2-phase commit protocol is to ensure atomicity of distributed transactions. 2-phase commit is a variant of 2-phase locking. The 2-phase commit protocol is unable to cope with server failure. In phase 2, the coordinator sends a “do abort” message to all servers if it receives at least one “no” vote.

Reading Coulouris et al., Chapter Coulouris et al., Chapter 17.3, 2-phase commit protocol (without nested transactions)