Detailed Lecture Notes
Visuals by Vasiliki Kalavri and Anna Arpaci-Dusseau
Consider the following example:
A need in certain applications arises: we want to execute a group of operations as one logical unit, so that either all of them succeed or all of them fail.
A transaction is a group of several reads and writes that will appear to run as one unit– either all succeeding or all failing.
Distributed systems that support transactions make building applications easier. Clients of a system with distributed transactions can then be assured that the transactions they define will appear as logical units, uninterruptible and unbreakable.
A client can use an API to mark the start and end of their transaction with TXN_START and TXN_END operations.
Now let’s consider how our ticket service may want to utilize transactions:
Alice submits a transaction which corresponds to reserving a ticket, consisting of a get tickets operations, a matching update to a specific seat to mark it as not available, and a final decrement of get tickets.
Bob submits a get tickets transaction.
Consider– how do we want these transactions to interact with each other?
Ideally, it should be impossible for Bob’s transaction to see the status that 1 ticket is left, but B2 is not available. We also may not want it to be possible for Alice’s transaction to fail part way through– reserving B2 but not updating the ticket value.
Both of the above behaviors are formally defined within a transaction’s ACID properties.
A transaction is specifically defined as a group of operations that have the following properties:
These properties are often referred to by the acronym ACID.
Today we will focus on how a system could support isolated distributed transactions. We define isolated to mean that results of incomplete transactions will not be visible to other transactions.
Consider that we have two concurrent transactions.
Our starting conditions: x=100,y=100
| TXNs | |
|---|---|
T1: |
T2: |
Consider that we have a T2 print that x=110 and y=100. How could this occur? It seems that what happened was between line 2 and 3 of T1, T2 ran on our distributed store and observed a partial result of T1. This would mean that our transactions are not isolated.
Thus, in implementing distributed transactions we must build a system where such behavior is impossible to observe.
Isolation is synonymous with a property known as serializability. Serializability is a consistency model that pertains to transactions. Linearizability and all other consistency models we discussed in the previous section only pertained to single operations and made no guarantees about transactions (groups of operations).
Serializability guarantees that even if transactions execute concurrently, the results are the same as if each transaction had executed in some serial order. Strict Serializability is a stronger model, which also guarantees that the serial order for the transactions respects the real time the transactions were committed by the client.
How do transactions enforce isolation and implement serializability? We will see two categories of methodologies.
Pessimistic concurrency control (PCC) assumes most concurrent transactions acting on the same data objects will conflict (have unserializable results). Thus, PCC uses locks, or a similar mechanism, to enforce that only one transaction at a time (serially) can access some particular data object.
Optimistic concurrency control (OCC) assumes most concurrent transactions will not conflict (have unserializable results). Thus, OCC lets two transactions run optimistically, verifies the results are serializable, then, if they are, finalizes/commits those transaction results.
One strategy to ensure transactions are isolated is via pessimistic concurrency control (PCC).
This approach assumes that transactions will conflict (they read/write the same data in a way that would not be isolated), and thus enforces a serial order in which the transactions must run.
Locking is PCC as it enforces that only one thread of execution in our system can access some specific data.
Two Phase Locking is one technique to achieve isolation via PCC.
In 2PL:
The two phases in 2PL refer to the expanding phase (when locks are acquired) and the shrinking phase (when locks are released).
After a txn has acquired the lock, it must continue to hold the lock until the end of the transaction (commit or abort)
Consider the following two transactions:
In 2PL several things could occur:
Consider that 2PL is vulnerable to deadlock.
How could deadlock occur in the above example? What happens if T1 gets the exclusive lock for x and T2 gets the exclusive lock for y?
https://15445.courses.cs.cmu.edu/fall2025/notes/19-timestampordering.pdf
The main downside to PCC is performance. We have to spend time waiting for, acquiring and releasing locks. OCC aims to reduce this overhead by allowing transactions proceed concurrently and check for conflicts in the end
The steps of OCC are as follows:
How do we approach validation in OCC?
In centralized validation, all clients send the results of their read/write operations (that comprise the transaction) to a centralized validation server.
The centralized validation server then must assess the results of these received transactions, determine if they are serializable or not, and then respond back to the clients if they should abort or commit.
Consider:
| TXNs | |
|---|---|
x=0 y=0 z=0 |
Our centralized validation server can find the following serializable order: T4;T1;T3;T2 (note that T2 could go anywhere) Thus, our centralized validation server can respond to the txn coordinator(s) that T1,T2,T3,T4 can commit. |
x=0 y=0 |
Our centralized validation server can find no serializable order: T1->T3: required (via x) T3->T2: required (via y) BUT T2,T1 required (via x) Thus, our centralized validation server must issue an “abort” response to at least one txn in order to remedy the situation. |
Ideally, we would not need a centralized authority in order to perform this validation step in OCC. Let’s instead propose that each individual server (that comprises some distributed data store) validates the transaction based only on receiving operations that pertain to data in their local store.
Let’s consider what occurs in the following scenario:
| TXNs | |
|---|---|
x=0 y=0 |
S1 stores x. S2 stores y. S1 finds serializable order T2, T1, T3 based on the operations pertaining only to x [Rx0], [Rx0 Wx1], [Rx1]. S2 finds serializable order T3, T2, T1 based on the operations pertaining to x. Notice, that the results of these transactions are not serializable, and thus should be aborted. Clients/txn coordinator will receive ok response from all servers and will commit the transaction incorrectly! |
We see that separating the information onto two servers means that no server has enough information to correctly abort the transactions. Can we include some additional information such that servers will always abort unserializable results?
Our final plan will be as follows:
The key observation with the above approach is that the metadata of a timestamp with each txn defines a particular serializable order, and every server in our system will check if the results of the operations are consistent given this particular order.
If our timestamps correspond to real time– we get strictly serializable results. But notice for serializability alone we do not need this guarantee and using any unique, comparable, value as a timestamp will work.
| TXNs | |
|---|---|
x=0 y=0 |
S1 stores x. S2 stores y. S1 enforces timestamp order T1, T3, T2 and notes that T2’s Rx0 is impossible with this order. S2 enforces timestamp order T1, T3, T2 and sees no issue. However, clients/txn coordinator receiving one S1 “abort” and S2 “ok” will abort the txn. |
x=0 |
S1 stores x. S1 enforces timestamp order T2, T1 and notes that T1’s Rx0 is impossible with this order. The client/txn coordinator receiving S1 “abort” will abort the txn. However, note that the txns are serializable with the order T1, T2 and our approach will abort anyway. |
Notice, the above approach will always abort unserializable results. However, it may also abort serializable results at times as well. If the latter situation is rare enough, however, the benefit of distributed transaction validation is worth the few unnecessary “abort” results.