Eight isolation levels that web development must know

Source: Internet
Author: User

ACID properties are the cornerstone of database theory, which defines the four properties that a theoretically reliable database must have: atomicity, consistency, isolation, and persistence. Although these four properties are important, isolation is the most flexible. Most databases offer some isolation levels to choose from, and many libraries now add additional layers to create finer-grained isolation. Isolation levels are so widely applied that the ease of isolation constraints tends to increase scalability and performance by several orders of magnitude.

Serial consistency is one of the oldest and highest isolation levels available, and it is favored because it provides a simple programming model that allows only one transaction to operate on a given resource at a time, which avoids a lot of potential resource problems. However, most applications, especially Web applications, do not take this very high level of isolation because it is impractical from an end-user perspective-any application that has a large user base will have a few minutes of delay in accessing the shared resources, which can result in a rapid reduction in user volume. Weak consistency and eventual consistency are ubiquitous in large-scale distributed data sources, such as the web. Several successful large Web applications (for example, ebay and Amazon) have shown that optimistic (optimistic) weak consistency is much better than the traditional pessimistic (pessimistic) mechanism in terms of extensibility. This article will take a peek at eight different isolation levels. Learn to ease the constraints of data consistency, and you can use these eight isolation levels in your own applications for better performance and scalability.

The primary goal of concurrency control is to ensure that transactions are isolated and do not affect other transactions. Achieving high levels of isolation is at the expense of performance. Concurrency control can be implemented in a pessimistic or optimistic mechanism. Most relational databases use a pessimistic mechanism to implement write optimizations. The pessimistic mechanism uses locks, which can block some operations or perform some form of conflict detection by using locks. When a table, page, or row is modified, a lock in the pessimistic mechanism can be used to block other potential transactions that access the modified resource. However, the optimistic mechanism does not take any locks, it relies solely on conflict detection to maintain transaction isolation. The optimistic mechanism uses conflict detection to allow all read operations and to verify their consistency at the end of a transaction. If a conflict is detected, the transaction is rolled back or redo. Most Web servers are read-optimized, so the optimistic mechanism is used. By allowing all read-in operations, the optimistic mechanism can guarantee both high read and write throughput, and the consistency of the data if the resource is not constantly changing.

The isolation levels listed below are used to help web developers better understand the constraints placed in their programming model, helping system architects and developers discuss how to choose the most effective isolation level while maintaining the necessary data integrity. They are listed in the Order of least isolated (unread) to most isolated (serialized).

1. Non-submitted reading (READ UNCOMMITTED)

Uncommitted read isolation levels require very little isolation between transactions. Each read operation can see the pending write operation (Dirty read) in the transaction. However, write operations that have already been committed must have a serial order to prevent Dirty writes. The pessimistic mechanism blocks conflicting writes until other writes have been committed or have been rolled back. The optimistic mechanism will not lock these operations, it will allow all operations to pass. If a connection is rolled back, then other operations that modify the same piece of data are rolled back. In this level, shared buffering can be used without validation. This isolation level is best used in situations where transactions are not required (such as read-only datasets), or if transactions are modified only when the database is exclusive.

Example : An archive database that is updated only offline, or an audit/login (audit/logging) table that is not used in transactions.

2, has been submitted reading (read Committed)

Read Committed can read any state that has been committed in the system and can be buffered without validation (mixed state), just as the changes that occur in the current connection can be reflected in the results. The pessimistic mechanism implements it as a monotone view. Optimistic transactions isolate and store all changes so that they are not available until they are committed. Read committed using a very optimistic mechanism, it defers writing all changes until the transaction is committed. This form of optimistic isolation enables complex write operations without blocking read operations, and it does not have a validation pattern. A shared buffer can only be used in a committed state. This isolation level is best used in cases where the result can use the old value and the transaction can only be used for write operations.

Example : An online forum that does not have to display the current latest posts, and it does not conflict with the data between posts.

3. Monotone view (monotonic views)

A monotone view is an extension of a read-committed, where a transaction observes a monotonically rising state in the database while it executes. At this level, if there is a noticeable write transaction, the pessimistic transaction is blocked in the read-in operation. Optimistic Transactions act as if they were in read-committed, isolate and save all changes, and validate their buffers to ensure they are still legitimate. This level synchronizes database copies on a regular basis and is best used in situations where transactions are not required or only write transactions are present.

Example : A user preference table that can be modified only by one person.

4, Snapshot read (Snapshot Reads)

Snapshot reads extend the monotony view, which guarantees that the query results are reflected in a consistent snapshot of the database. The pessimistic mechanism blocks other write operations that affect the results when read operations. The optimistic mechanism allows other write operations and notifies the read transaction that a part has changed and is rolled back. To implement an optimistic mechanism, you must verify that there are any parallel write operations that modify the results before the end of the read operation, and if so, the results may be re-made or rolled back. This verification process may simply check for write operations in the same table, or simply check for changes to the query results. Optimistic isolation levels can easily detect conflicts and support write operations in the process of allowing concurrent read-in operations. This level synchronizes the database copy on a regular basis as long as the snapshot can be read. It is best to use this isolation level when the write operation is minimal, you do not want to conflict with the read-in operation, and the query results require consistency.

Example : A currency transposition table or query table with a query that is more frequent than modified and retains only the latest values.

5, cursor stability (cursor stability)

Cursor stability Isolation extends read-committed and is the default isolation level for many relational data. In this isolation level, a pessimistic transaction, if executed in a separate statement, must specify the record it will modify. This can usually be implemented with the "for UPDATE" keyword appended to the "select" Query. In this case, other conflicting read-write pessimistic transactions will be blocked until the transaction ends. The optimistic transaction tracks the version number of all modified records/entities that were validated at the time of submission. This is a popular optimistic isolation level and is therefore supported by all the mainstream object relational mapping libraries. In the Java Persistence API, you can use Flush_on_commit (although the query may not affect local changes) to reach this level, and you can throw a Optimisticlockexception exception if a conflict is detected. This isolation can also be used in HTTP header domain If-match or if-unmodified-since, which can be used to compare the previous resource's version or timestamp before updating. This level is best used when the entity is changed by external information (not read from the database), or if the changes do not overwrite each other.

Example : A shared company directory or a wiki.

6, Repeatable READ (REPEATABLE Read)

The repeatable read level extends the stability of the cursor, which guarantees that any data within a transaction is not modified or removed during the transaction. Pessimistic transactions need to read the locks on all records and block other services to modify those records. Optimistic transactions track all records or entities and check if they have been modified at commit time. This level is best used in situations where the entity state can affect other entities, or if the transaction is composed of read and write operations.

Example : An order Tracking database that reads values from an entity and uses it to calculate other entity values.

7. Snapshot Isolation (Snapshot isolation)

Snapshot isolation extends snapshot reads and repeatable reads, which guarantee that all read operations in a transaction will see a consistent snapshot in the database. Any read operations performed by a transaction will have the same result, regardless of whether they are executed in the transaction sooner or later. This is different from repeatable reads because snapshot isolation prevents Phantom reads (query results are constantly changing). Many relational databases use multiple versions of concurrency control (also called SERIALIZABLE) to support this level, implemented through a combination of lock and conflict detection. In this level, the transaction must be prepared for rollback, given that it may conflict with a pessimistic mechanism or an optimistic mechanism. The pessimistic mechanism attempts to reduce the chance of conflict by locking resources, but must merge the changes after the transaction is committed. The optimistic mechanism also uses multiple versions of concurrency control, but it does not block other transactions that could potentially cause conflicting operations, but instead rolls back conflicting transactions. This level of isolation is best used in situations where transactions can read and modify multiple records.

Example : A workflow system based on System state rules.

8. Serializable (serializability)

Serialization is an extension of snapshot isolation, which requires that all transactions must appear one after the other, as if they were serialized. The pessimistic mechanism needs to lock down all evaluated queries to prevent write operations from affecting these results. The optimistic mechanism keeps track of all evaluated queries and, at the end of a transaction, uses a back-up or forward-validation pattern to check if there is a parallel write operation that affects the parallel read-in operation, and if so, it rolls back all transactions outside the conflicting transaction. In this isolation level, any COMMIT transaction does not alter the system's representational state. It is best to use this level of isolation in situations where full data consistency is required.

Example : An accounting system that makes range queries to calculate new values.

Summarize

The following is a summary of the isolation levels mentioned in this article, which can help you find the level that best suits your application.

Possible types of conflicts for transactions at different isolation levels:

Dirty Write Dirty Read Mixed state Inconsistent read Overwrite Non-repeatable Phantom reading Inconsistency
Non-committed read No OK OK OK OK OK OK OK
Read Committed No No OK OK OK OK OK OK
Monotone view No No No OK OK OK OK OK
Snapshot read No No No Do not use d OK OK OK OK
Cursor stability No No OK OK No OK OK OK
REPEATABLE READ No No OK OK No No OK OK
Snapshot isolation No No No No No No No OK
Serializable No No No No No No No No

The best prerequisites for different isolation levels:

Buffer Data synchronization Optimistic conflict mode Recommended actions Example
Non-committed read Allow buffering Intermittent. Detecting Dirty Writes Cannot read and write concurrently File
Read Committed Allow buffering Intermittent. No conflict detection Monotonous reading/writing Web Forum
Monotone view Must be verified Cycle of No conflict detection Combination read-In User Preferences
Snapshot read Must be verified Cycle of Compare read-In and modify content Consistent read-in Query table
Cursor stability Allow buffering Intermittent. Compare modified entity versions CRUD Services Directory
REPEATABLE READ Allow buffering Intermittent. Compare read-in entity versions Read/write Entities Order Tracking
Snapshot isolation Must be verified Cycle of Compare read-in entity versions Synchronizing entities Work flow
Serializable Must be verified Full sync Compare query and modify content Improve data consistency Accounts

Data consistency is critical in database applications-it allows developers to use data in a distributed environment. Although strong consistency levels such as serializable provide a simple programming model, they can lead to excessive overhead, blocked operations, or rollback of transactions, which is unnecessary for many applications. If there are other issues, you can use a more appropriate isolation level to help developers and system architects to better understand the need for data consistency while maintaining performance and cost balance.

View English text: Eight isolation levels every Web Developer should Know.

Thank Almaer for the review of this article.

Eight isolation levels that web development must know

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.