Wednesday, January 27, 2010

ACID Property in Database

ACID (atomicity, consistency, isolation, durability) is a set of properties that guarantee that database transactions are processed reliably. In the context of databases, a single logical operation on the data is called a transaction. An example of a transaction is a transfer of funds from one bank account to another, even though it might consist of multiple individual operations (such as debiting one account and crediting another).

Atomicity
Atomicity refers to the ability of the DBMS to guarantee that either all of the tasks of a transaction are performed or none of them are. For example, the transfer of funds from one account to another can be completed or it can fail for a multitude of reasons, but atomicity guarantees that one account won't be debited if the other is not credited.

Atomicity states that database modifications must follow an “all or nothing” rule. Each transaction is said to be “atomic” if when one part of the transaction fails, the entire transaction fails. It is critical that the database management system maintains the atomic nature of transactions in spite of any DBMS, operating system or hardware failure.

Either all Transactions are carried out or none are. The meaning is the transaction cannot be subdivided, and hence, it must be processed in its entirety or not at all. Users should not have to worry about the effect of incomplete Transactions in case of any system crash occurs. Transactions can be incomplete for three kinds of reasons: 1)Transaction can be aborted, or terminated unsuccessfully. This happens due to some anomalies arises during execution. If a transaction is aborted by the DBMS for some internal reason, it is automatically restarted and executed as new. 2)Due to system crash. This may be happen due to Power Supply failure while one or more Transactions in execution. 3)Due to unexpected situations. This may be happen due to unexpected data value or be unable to access some disk. So the transaction will decide to abort. (Terminate it).

Consistency
The consistency property ensures that the database remains in a consistent state; more precisely, it says that any transaction will take the database from one consistent state to another consistent state.

The consistency property does not say how the DBMS should handle an inconsistency other than ensure the database is clean at the end of the transaction. If, for some reason, a transaction is executed that violates the database’s consistency rules, the entire transaction could be rolled back to the pre-transactional state - or it would be equally valid for the DBMS to take some patch-up action to get the database in a consistent state. Thus, if the database schema says that particular field is for holding integer numbers, the DBMS could decide to reject attempts to put fractional values in there, or it could round the supplied values to the nearest whole number: both options maintain consistency.

A DBMS that claims to enforce consistency is only responsible for those rules that are known to it. Thus, if a DBMS allows fields of a record to act as references to another record, then consistency implies the DBMS should enforce referential integrity: by the time any transaction ends, each and every reference in the database must be valid. If a transaction consisted of an attempt to delete a record referenced by another, each of the following mechanisms would maintain consistency:
  • abort the transaction, rolling back to the consistent, pre-transactional state;
  • delete all records that point at the deleted record (this is known as cascaded deletes); or,
  • clear the relevant fields for all records that point at the deleted record.
Isolation
Isolation refers to the requirement that other operations cannot access or see the data in an intermediate state during a transaction. This constraint is required to maintain the performance as well as the consistency between transactions in a DBMS. Thus, each transaction is unaware of other transactions executing concurrently in the system. In a DBMS, many transactions may be executed simultaneously. These transactions should be isolated from each other. One’s execution should not affect the execution of other transactions. To enforce this concept DBMS has to maintain certain scheduling algorithms.

Durability
Durability refers to the guarantee that once the user has been notified of success, the transaction will persist, and not be undone. This means it will survive system failure, and that the database system has checked the integrity constraints and won't need to abort the transaction. Many databases implement durability by writing all transactions into a transaction log that can be played back to recreate the system state right before a failure. A transaction can only be deemed committed after it is safely entered in the log.
Durability does not imply a permanent state of the database. Another transaction may overwrite any changes made by the current transaction without hindering durability.

Examples

Atomicity failure

Assume that a transaction attempts to subtract 10 from A and add 10 to B. If it were to succeed, this would be a valid transaction because A+B would still be 100. However, assume there is a problem with the system. After 10 is removed from A, the attempt to add 10 to B fails. The problem might be network failure, disk failure, power outage, program bug, etc. Atomicity requires that both parts of this transaction complete or none at all. There are two options: Attempt to add 10 to B again or undo the change to A. By undoing the change to A (adding 10 back to A), atomicity is accomplished.

Consistency failure
Consistency is a very general term that demands the data meets all validation rules that the overall application expects - but to satisfy the consistency property a database system only needs to enforce those rules that are within its scope. In the previous example, one rule was a requirement that A + B = 100; most database systems would not allow such a rule to be specified, and so would have no responsibility to enforce it - but they would be able to ensure the values were whole numbers. Example of rules that can be enforced by the database system are that the primary keys values of a record uniquely identify that record, that the values stored in fields are the right type (the schema might require that both A and B are integers, say) and in the right range, and that foreign keys are all valid.

Validation rules that cannot be enforced by the database system are the responsibility of the application programs using the database.

Isolation failure
To demonstrate isolation, at least two transactions must be executed at the same time. Isolation is easy to achieve if only one transaction is executed at a time. However, an extremely long transaction will block access to the database if it must run to completion before other transactions may begin. Therefore, the independent actions of each transaction are run in an interleaved manner.

Consider two transactions. One will transfer 10 from A to B. The other will transfer 10 from B to A. There are four actions. The first transaction will subtract 10 from A and add 10 to B. The second transaction will subtract 10 from B and add 10 to A. By interleaving the transactions, the actual order of actions will be: A-10, B-10, B+10, A+10. If isolation is maintained, the result after the first transaction is finished adding 10 to B will be identical to the result if the second transaction is not run. However, B will be 10 less due to the first action of the second transaction. This is known as a write-write failure because two transactions attempted to write to the same data field.

Durability failure
Assume that a transaction transfers 10 from A to B. It removes 10 from A. It then adds 10 to B. At this point, a "success" message is sent to the user. However, the changes are still queued in the disk buffer waiting to be committed to the disk. Power fails and the changes are lost. The user assumes that the changes have been made, but they are lost.

To satisfy the durability constraint, the database system must ensure the success message is delayed until the transaction is safely on disk. (Depending on the architecture of the database system, it may be enough to ensure that a transaction log has been fully written to disk; in the event of a crash and restart, the log will be replayed as far as possible before allowing applications to query or update the database.)

No comments:

Post a Comment