





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science — University of San Francisco – p. 1/
??
Features of transactions
Serial equivalence
Locking and deadlock
Distributed transactions
Two-phase commit
Distributed deadlock
Department of Computer Science — University of San Francisco – p. 2/
??
A transaction is a sequence of operations between aclient and a server.
Goal: make sure that:
Objects remain in a consistent state
System is tolerant to crash failures
Transaction effects are independent of othertransactions
Transactions are either completed or not started.
Department of Computer Science — University of San Fra
As an example, we’ll look at an interface to a bankingsystem.
We’d like to be able to do the following operations onaccounts:
deposit(amt)
withdraw(amt)
getBalance()
setBalance(amt)
We’d also like the following operations to be availablefor branches:
CreateAccount(name)
lookUpAccount(name)
totalAccounts()
Department of Computer Science — University of San Francisco – p. 4/
??
A transaction may involve several operations, each ofwhich changes the state of a different object:
Transaction T:
We can’t stop in the middle, lose any of theoperations, or do them in the wrong order.
Department of Computer Science — University of San Francisco – p. 5/
??
A transaction may be either committed or aborted.
When all operations are complete and the transactionis ready to be accepted, it is
committed
Written to permanent storage
After this point, it cannot be undone
If the server decided that a transaction cannot beprocessed (undone by the client, or it will leave thesystem in an inconsistent state), it is
aborted
All operations are undone
Department of Computer Science — University of San Fra
The desirable features of a transactional DBMS aresometimes referred to as ACID
Atomicity
Consistency
Isolation
Durability
Department of Computer Science — University of San Francisco – p. 7/
??
Atomicity is sometimes referred to as “all-or-nothing”.
Either a transaction completes successfully, and alleffects are applied to all objects, or it has no effect atall.
Either all withdraws and deposits are made, or noneof them are.
Department of Computer Science — University of San Francisco – p. 8/
??
A transaction must move the system from consistentstate to consistent state.
For example, the sum of all the accountBalancesmust always be equal to the branch’s totalAccounts
Depending on the application, at database may haveother constraints
No negative balances on an account
No post-dated transactions
If the system is in an inconsistent state after atransaction, the transaction must be undone, so as torestore consistency.
Department of Computer Science — University of San Fra
The intermediate effects of a transaction must not bevisible to other transactions.
In our example, Nancy’s bank account balancebriefly went up by $100. (the money was thentransferred to Brooks’ account)
No other process or transaction should see thatbalance.
In other words, to the outside world, a transactionmust appear as a single operation.
Department of Computer Science — University of San Francisco – p. 10/
??
How to provide isolation?
Perform all transactions in a single thread
Works, but doesn’t scale.
Use locks to control concurrent access.
Better, although now we need to detect (and undo)deadlocks.
Department of Computer Science — University of San Francisco – p. 11/
??
After a transaction has been processed, its effectsare saved to permanent storage.
The transaction will never be undone or lost, even inthe presence of a server crash.
This typically also requires some guarantees aboutthe nature of the permanent storage.
Department of Computer Science — University of San Fran
The trick to achieving serial equivalence is to identifyoperations that conflict with each other.
Read/write and write/write (read/read is OK)
For two transactions to be serially equivalent, allpairs of conflicting operations must be executed inthe same order on all objects they both access.
Department of Computer Science — University of San Francisco – p. 19/
??
For example:
T: x=read(i); write(i,10); write(j,20)
U: read(j); write(j,30); z=read(i)
To have serial equivalence, one of the followingconditions must hold:
T accesses i before U does and T accesses jbefore U does
U accesses I before T does and U accesses Jbefore T does
Department of Computer Science — University of San Francisco – p. 20/
??
The most common way to achieve serial equivalenceis through the use of locks.
Before a client transaction uses an object, it requestsan associated lock.
The object cannot be used until the lock is acquired.
To ensure serial equivalence, a process may notacquire new locks within a transaction once a lockhas been released.
This is called two-phase locking.
There is a “lock-growing phase” and a“lock-shrinking” phase
Department of Computer Science — University of San Fran
T: begin transaction
(T) = balance1 = B.getBalance() ($200) T locks B
(T) = b.setBalance(balance1 * 1.1)($220)
U begins transaction
(T) = A.withdraw(balance1 / 10)T locks A
(U) = balance2 = B.getBalance() ($200) B is locked -U must wait.
T ends transaction: A and B unlocked.
(U) = b.setBalance(balance2 * 1.1)($220) U locks B
(U) = C.withdraw(balance2 / 10) U locks C
U ends transaction: B and C unlocked.
Department of Computer Science — University of San Francisco – p. 22/
??
To prevent “dirty reads” and “premature writes”, alllocks must be held until the transaction is committed.
This is called strict two-phase locking.
“dirty read” - one transaction sees a value that is partof another transaction that is later aborted.
T: b1 = a.getBalance() ($100)
T: a.setBalance(b1 + 10) ($110)
U: b2 = a.getBalance() ($110)
U: a.setBalance(b2 + 20) ($130)
U committed.
T aborted - U cannot be undone.
Department of Computer Science — University of San Francisco – p. 23/
??
To prevent “dirty reads” and “premature writes”, alllocks must be held until the transaction is committed.
This is called strict two-phase locking.
“premature writes” - an aborted operation is reset tothe wrong value.
Some systems will store the ’before image’ with awrite and roll back to that in the event of an abort
A: initial balance: $
T: a.setBalance(105) (’before image’: 100)
U: a.setBalance(110) (’before image’: 105)
U commits.
T aborts: A reset to 100. (should remain 110)
What if T aborts, then U aborts (balance will be 105, should be 100)
Department of Computer Science — University of San Fran
There is a tradeoff between the number of locks asystem has and the degree of concurrency allowed
This is called the
granularity
of the system
Goal: allow many objects the ability to read an object,but only one the ability to write the object.
Provide read locks and write locks.
Department of Computer Science — University of San Francisco – p. 25/
??
Whenever locks are used, a system can reach deadlock
Two or more processes are each waiting for a lockheld by the other process.
Neither can proceed until the other gives up itslock.
Neither can give up its lock until it proceeds.
Department of Computer Science — University of San Francisco – p. 26/
??
T wants to transfer $100 from A to B. U wants totransfer $50 from B to A.
T: A.deposit(100) - write lock on A.
U: B.deposit(50) - write lock on B.
T: B.withdraw(100) - wait for write lock for B
U: A.withdraw(50) - wait for write lock on A.
Since neither process will release its locks withoutcommitting or aborting, we are in deadlock.
Department of Computer Science — University of San Fran
One simple (but not efficient) method to preventdeadlock is to require a transaction to (atomically)acquire all locks at the beginning.
Restricts access to shared resources
May not be possible to predict at the beginning of atransaction, as in interactive applications.
Can also specify a static order.
Department of Computer Science — University of San Francisco – p. 28/
??
A more common approach is to have the processresponsible for administering locks detect deadlockand force transactions to abort.
We can represent processes as nodes and lockdependencies as edges in a
wait-for graph
When a lock is requested and must be waited on, anedge is added.
When a lock is freed, the edge is removed.
If the graph has a cycle, then we have deadlock.
The transaction at one of the nodes in the cycle canthen be aborted.
Choosing which transaction to abort can be tricky
Department of Computer Science — University of San Francisco – p. 29/
??
So far, we’ve focused on how to provide concurrenttransactions within a single server.
Distributed transactions
involve multiple servers that
must maintain a consistent state.
If a transaction is committed at one server, it mustcommit at all servers.
If a transaction is aborted at one server, it must abortat all servers.
Department of Computer Science — University of San Fran
Phase 1: The coordinator asks each participantwhether it is able to commit.
A participant may not change its vote to abort onceit has voted to commit.
This means that a participant can’t vote until itknows that it can complete its portion of thetransaction.
Department of Computer Science — University of San Francisco – p. 37/
??
Phase 2: Once all participants have voted, if anyparticipant voted to abort, the doAbort message issent to all participants.
If all participants voted to commit, the doCommitmessage is sent to all participants.
Clients each inform the coordinator that they havecommitted.
Once all participants have replied, the client isinformed that the transaction has been processed.
Department of Computer Science — University of San Francisco – p. 38/
??
Client
Branch 1
A Branch 2 Branch 3
B
C^
D
JoinTransaction
Id: T
a.withdraw(10)c.deposit(10)b.withdraw(5)d.deposit(10)
a.withdraw(T,10) b.withdraw(T,10) c.deposit(T,10)c.deposit(T,5)
join
join
Coordinator
Client wants to performa transaction involvingfour accounts at threebranches.
Sends openTransactionto coordinator at A andreceives a transactionID.
Includes transaction IDwith each request toother branches.
As each branch is con-tacted, they send a joinmessage to A.
Department of Computer Science — University of San Fran
Client
Branch 1
A Branch 2 Branch 3
B
C
D
JoinTransaction
Id: T
a.withdraw(10)c.deposit(10)b.withdraw(5)d.deposit(10)
a.withdraw(T,10) b.withdraw(T,10) c.deposit(T,10)c.deposit(T,5)
join
join
Coordinator
If the client wants toabort, it sends adoAbort message tothe coordinator.
This is forwarded to allgroup
members,
who
immediately abort.
Department of Computer Science — University of San Francisco – p. 40/
??
Client
Branch 1
A Branch 2 Branch 3
B
C
D
JoinTransaction
Id: T
a.withdraw(10)c.deposit(10)b.withdraw(5)d.deposit(10)
a.withdraw(T,10) b.withdraw(T,10) c.deposit(T,10)c.deposit(T,5)
join
join
Coordinator
If the client wants tocommit, it sends adoCommit message tothe coordinator.
The coordinator sendsa canCommit messageto all participants.
When they are ready tocommit, they send backyes.
If they must abort, theysend back no.
Department of Computer Science — University of San Francisco – p. 41/
??
Client
Branch 1
A Branch 2 Branch 3
B
C^
D
JoinTransaction
Id: T
a.withdraw(10)c.deposit(10)b.withdraw(5)d.deposit(10)
a.withdraw(T,10) b.withdraw(T,10) c.deposit(T,10)c.deposit(T,5)
join
join
Coordinator
Once all votes have been collected, ifthere are any aborts, the coordinatorsends doAbort to all participants.
Otherwise, a doCommit is sent to allparticipants.
Once a participant commits, it returnshaveCommitted to the coordinator.
When all haveCommit messages arereceived, the client is updated and thetransaction closed.
Department of Computer Science — University of San Fran
We can extend two-phase commit to work with nestedtransactions.
When a subtransaction is started, a coordinator forthat subtransaction is selected.
If one of the participants in that subtransactionaborts, the coordinator can choose whether to abort,based on the specifics of the transaction.
Example: withdraw fees from all accounts. If oneaccount becomes overdrawn, that subtransactionis aborted, but others may proceed.
If the parent is aborted, the subtransactions mustabort.
Department of Computer Science — University of San Francisco – p. 43/
??
Message loss is dealt with via timeouts.
Server failure is dealt with by writing progress out topermanent storage.
As operations are processed, they are logged.
A replica is then able to start up and recreateprogress.
Couloris has a great deal of detail on the way logscan be managed to do recovery.
Department of Computer Science — University of San Francisco – p. 44/
??
In a distributed transactional system, a lock managerthat is local to the resource manages access to thatresource.
Remote processes request access to the locks asusual.
Since there are many independent lock managers,deadlock can arise.
Transaction T locks variable X on server A.
Transaction U locks variable Y on server B.
Now each transaction wants to modify the othervariable.
Department of Computer Science — University of San Fran
In a centralized system, we would detect deadlock byfinding a cycle in the wait-for graph.
This is the simplest approach in a distributed system
All lock managers send their information to acentral authority.
All the usual problems with this approach apply.
Department of Computer Science — University of San Francisco – p. 46/
??
When a server that is acting as a lock managerreceives a request for a locked resource, it adds anedge to its local wait-for graph.
For example, say server X controls access todatabase A.
Initially, transaction T wants to executeA.withdraw(10)
X grants T the lock to A.
Then transaction U wants to execute A.deposit(20)
A is locked, so U
→
T is added to X’s wait-for
graph.
Department of Computer Science — University of San Francisco – p. 47/
??
When a transaction begins waiting for a lock, thecoordinator is informed.
When a process receives a request for a lock, andanother transaction is holding that lock, it checks withthe coordinator to see if the other process is waiting.
if it is, it sends a probe to the server administeringthat lock.
Includes the edge in the wait-for graph
When the probe is received, if there are waitingprocesses, those edges are added and the probe isforwarded.
As edges are added, cycles are checked for.
Department of Computer Science — University of San Fran