This post is dedicated to another important topic related to DB, the 'Transaction Isolation Levels'. There are four different types of transaction isolation levels. While most of the databases support all these four, but some like PostGRES do not support all. Isolation levels controls how much of the uncommitted data from a particular transaction is visible to other transactions. Before delving more into various isolation levels, lets try to understand various scenarios of inconsistencies that occur when two or more transactions operate on the same data at the same time.
Dirty Read: This is the case when one transaction reads the data which contains uncommitted data from other transaction. If the other transaction rolls back the operation, the first transaction still has that invalid data which leads to inconsistencies.
Non-repeatable Read: This occurs when a same query inside same transaction leads to different results if executed repeatedly. In such situations, one transaction continuously retrieves the data while a different transaction modifies that data.
Phantom Read: This is similar to non-repeatable reads with only exception being that the transaction repeatedly performing the data retrieval operation finds new rows because the other transaction has done that insertion.
As is evident from the various inconsistencies mentioned above, interleaving transactions can create havoc if not managed properly. To help with data, the database engines come with various preventive measures which are known as ISOLATION LEVELs. All these isolation levels along with their approach to handle the inconsistencies mentioned above are discussed below.
READ_UNCOMMITTED: This is the very basic isolation level with the best performance at the cost of several issues. As the name suggests, it allows the uncommitted data from other transactions to be read.
READ_COMMITTED: This doesn't allow reading of uncommitted data from other transactions. But it still has non-repeatable read and phantom read issues.
REPEATABLE_READ: Using this isolation level makes sure that same query inside the same transaction always results in the same data if executed repeatedly. In this way, it eliminates both the dirty reads and non-repeatable reads.
SERIALIZABLE: It is the most restrictive level of the all four and works by applying locks for both reading and writing, basically by making sure the transactions do not work in parallel, but instead in a serialized way. Using this isolation level should be avoided as it takes a toll in the performance of the application.
The table below summarises the relation between isolation levels and their effects on various DB read scenarios.
Dirty Read: This is the case when one transaction reads the data which contains uncommitted data from other transaction. If the other transaction rolls back the operation, the first transaction still has that invalid data which leads to inconsistencies.
Non-repeatable Read: This occurs when a same query inside same transaction leads to different results if executed repeatedly. In such situations, one transaction continuously retrieves the data while a different transaction modifies that data.
Phantom Read: This is similar to non-repeatable reads with only exception being that the transaction repeatedly performing the data retrieval operation finds new rows because the other transaction has done that insertion.
As is evident from the various inconsistencies mentioned above, interleaving transactions can create havoc if not managed properly. To help with data, the database engines come with various preventive measures which are known as ISOLATION LEVELs. All these isolation levels along with their approach to handle the inconsistencies mentioned above are discussed below.
READ_UNCOMMITTED: This is the very basic isolation level with the best performance at the cost of several issues. As the name suggests, it allows the uncommitted data from other transactions to be read.
READ_COMMITTED: This doesn't allow reading of uncommitted data from other transactions. But it still has non-repeatable read and phantom read issues.
REPEATABLE_READ: Using this isolation level makes sure that same query inside the same transaction always results in the same data if executed repeatedly. In this way, it eliminates both the dirty reads and non-repeatable reads.
SERIALIZABLE: It is the most restrictive level of the all four and works by applying locks for both reading and writing, basically by making sure the transactions do not work in parallel, but instead in a serialized way. Using this isolation level should be avoided as it takes a toll in the performance of the application.
The table below summarises the relation between isolation levels and their effects on various DB read scenarios.
|
|
Dirty
Read
|
Non-repeatable
Read
|
Phantom
Read
|
|
READ_UNCOMMITTED
|
O
|
O
|
O
|
|
READ_COMMITTED
|
X
|
O
|
O
|
|
REPEATABLE_READ
|
X
|
X
|
O
|
|
SERIALIZABLE
|
X
|
X
|
X
|
Comments