7. Transactions Flashcards
Transaction meaning
Transaction is to simplify the programming model for applications accessing a database
Transaction isolation level
- Read committed isolation (weak)
- Snapshot isolation (medium)
- Serializable isolation (strong)
ACID - Atomicity (Abortability)
No partial failure.
ACID - Consistency
You have certain statements about your data that must always be true.
Atomicity, isolation and durability are properties of the database, whereas consistency is the property of the application.
ACID - Isolation (Serializability)
Concurrently executing transactions are isolated from each other.
ACID - Durability
Promise that once a transaction has committed successfully, any data it has written will not be forgotten.
Race condition - Dirty reads
One client reads another client’s writes before they have been committed.
The read committed isolation level and stronger levels precent dirty reads.
Race condition - Dirty writes
One client overwrites data that another client has written, but not yet committed.
Almost all transaction implementations prevent dirty writes.
Race condition - Read skew (non-repeatable reads) (compared with phantom reads)
A client sees different parts of the database at different points in time.
e.g. Alice see 500 in one account, but 400 in the other…
This issue is most commonly prevented with snapshot isolation, which allows a transaction to read from a consistent snapshot at one point in time. It is usually implemented with multi-version concurrency control (MVCC).
Race condition - Lost updates
Two clients concurrently perform a read-modify-write cycle. One overwrites the other’s write without incorporating its changes, so data is lost.
Some implementations of snapshot isolation prevent this anomaly automatically, while others require a manual lock (SELECT FOR UPDATE).
Race condition - Write skew
A transaction reads something, makes a decision based on the value it saw, and writes the decision to the database. However, by the time the write is made, the premise of the decision is no longer true.
Examples:
- Two on-call doctors take sick leave at the same time;
- Meeting room booking system;
- Multiplayer game;
- Claiming a username;
- Preventing double-spending;
You can think of write skew as a generalization of the lost update problem. Write skew can occur if two transactions read the same objects, and then update some of those objects (different transactions may update different objects). In the special case where different transactions update the same object, you get a dirty write or lost update anomaly (depend on the timing).
Only serializable isolation prevents this anomaly. If not able to, the second-best option in this case is probably to explicitly lock the rows that the transaction depends on.
Race condition - Phantom reads (compared with read skew)
A transaction reads objects that match some search condition. Another client makes a write that affects the results of that search.
Snapshot isolation prevents straightforward phantom reads, but phantoms in the context of write skew require special treatment, such as index-range locks.
Isolation level - Serializable isolation - meaning
Serializable isolation means database guarantees that transactions have the same effect as if they run serially.
It is usually regarded as the strongest isolation level.
Isolation level - Read committed isolation - meaning
Read committed isolation means:
- When reading from database, you will only see data that has been committed (no dirty reads).
- When writing to database, you will only overwrite data that has been committed (no dirty writes).
Read committed isolation can handle dirty reads and dirty writes. But can not handle read skew, phantom reads, write skew, lost updates.
Isolation level - Read committed isolation - implementation
- To prevent dirty write, use row-level write locks.
When a transaction wants to modify a particular object, it must first acquire a lock on that object.
- To prevent dirty read, it’s NOT performable to use read lock because one long-running write transaction can force many read-only transactions to wait until it’s completed.
Instead, database remembers both old committed and new value set by transaction that currently holds the write lock. While transaction is ongoing, any other transactions that read the object are simply given the old value.