When it comes transaction, we think of ACID property, we think of a bunch of actions which should be done in all or nothing.
Atomicity: guarantees that each transaction is treated as a single “unit”
Consistency: ensures that a transaction can only bring the database from one valid state to another
Isolation: ensures that concurrent execution of transactions leaves the database in the same state that would have been obtained if the transactions were executed sequentially
Durability: guarantees that once a transaction has been committed, it will remain committed even in the case of a system failure
It seems all four rules of ACID are the equally property for a transaction, but it is not.
Why We Need Transaction
To make it clear, the very first goal of transactions is to ensure that all of the objects managed by a server remain in a consistent state when they are accessed by multiple transactions and in the presence of server crashes.
The core is to keep state consistent when
- there are multiple transactions access object concurrent;
- there are server crash/failure;
So, we need a transaction to be
- isolated from other transaction (
Isolation & Atomicity
):- serially: one transaction at a time, in some arbitrary order
- serially equivalent or serializable: this would have the same effect as a serial execution
- applied on recoverable object (
Durability
):when a server process crashes unexpectedly due to a hardware fault or a software error, the changes due to all completed transactions must be available in permanent storage so that when the server is replaced by a new process, it can recover the objects to reflect the all-or-nothing effect.
We have method Isolation & Atomicity & Durability
, but no Consistency
, because programmers it is generally responsible to ensure that transactions leave the database consistent, i.e. we have to write right/enough unit of operations (follow is a wrong unit).
transation: transfer
accountA: withdraw 10
accountB: deposit 9
Failure Types
As an application programmer, we can understand Isolation & Atomicity
very well because we also met those kinds of problems in application program, when we are working in multi-thread/process world. We need to coordinate different threads to access shared object in sequence.
But we don’t always have chance to consider Durability
. In order to have this property, we first have to understand what kinds of failure/crash server may encounter:
-
Writes to permanent storage may fail, either by writing nothing or by writing a wrong value – for example, writing to the wrong block is a disaster. File storage may also decay.
-
Servers may crash occasionally. When a processor is faulty, it is made to crash so that it is prevented from sending erroneous messages and from writing wrong values to permanent storage – that is, so it cannot produce arbitrary failures. Crashes can occur at any time; in particular, they may occur during recovery.
-
There may be an arbitrary delay before a message arrives. A message may be lost, duplicated or corrupted. Both forged messages and undetected corrupt messages are regarded as disasters.
In order to solve storage failure, server failure and message failure, we can:
- Reads from permanent storage can detect (by a
checksum
) when a block of data is bad; Failure to write storage will abort all transaction and shutdown server. - When a crashed server is replaced by a new process, its volatile memory is first set to a state in which it knows none of the values (for example, of objects) from before the crash. After that it carries out a recovery procedure (kinds of
operation log
) using information in permanent storage and obtained from other processes to set the values of objects including those related to the two-phase commit protocol. - The recipient can detect corrupted messages using a checksum & lost message is re-sent by by reliable channel (like
TCP
) & dup messages should be handled by different instances of server.
Ref
- ACID
- DISTRIBUTED SYSTEMS: Concepts and Design
Written with StackEdit.
评论
发表评论