Introduction
In distributed systems, the use of transactions and the concurrency control of shared resources are very common. Simply put, a transaction is a collection of operations of some atoms. Atomic manipulation means that either the operation succeeds or the operation fails, and there is no other option. Concurrency control is present in high concurrency scenarios, this article mainly describes 3 common means, 1, locking measures. 2, optimistic concurrency control means. 3, timestamp sequence mode. Before doing this, review the usual means of synchronization when writing a program, such as:
1, with the Synchronize keyword, the word used in Java, to ensure that only one thread can execute the inside of the method.
2. The wait () and notify () methods are used to coordinate synchronous use, such as in the Blockingqueue blocking queue.
The failure of a transaction can be resumed through permanent storage, which can be restored again and can be done offsite by means of a copy.
Transactions
There are 2 characteristics of a transaction: 1, the independence of the operation, and other operations of the isolation, unaffected. 2, his operation either succeeds or fails, and does not have any effect on other operations. Because the transaction requires the atomicity of the operation, so to ensure the synchronization of the operation, the goal of the server is to improve the concurrency of the transaction program does not affect his atomicity.
1. Each transaction is managed by a box created by a coordinator.
2, in the case of a transaction concurrency, there will be 2 typical problems: 1, the loss of the update problem. 2, the State inconsistency problem, is typical is the data reads the old data, but actually the data has been updated.
3. The transaction resumes from the abort, then the server must record the records of all committed transactions or save many staged versions of the transaction.
Nested Transactions
Nested transactions (Nested transaction) are extended from a single transaction. He is composed of a series of transactions, divided into the top-level transaction and Subtransaction 2 kinds of affairs, subtransction refers to his child affairs. These 2 types of transactions constitute a tree-like structure.
1, nested transactions compared to the advantages of ordinary transactions: 1, support more concurrency, his child transactions can be on different servers in parallel. 2. The child transaction can commit or abort the transaction independently.
2. Some rules for nested transactions: 1. A transaction can only be committed or aborted until his child's transaction is complete. 2. When the parent transaction is aborted, all child transactions will be aborted.
Lock
Locks are a very common measure to control the concurrency of resource accesses, which we generally refer to as exclusive locks, only exclusive. Of course, the lock can be fine-grained division. For example, can be divided into read-lock and write-lock separation, read the lock is actually shared, also known as a shared lock.
1, in the nested transaction, his lock mode is a little different, when the parent transaction holds the lock of the transaction, his child transaction can take the parent transaction lock execution, completed, and then return the lock to the parent transaction.
2, the use of locks the most annoying situation is the deadlock, the simple point is that we are waiting for each other in the hands of the resources, who will not let go, after the group has been holding a stalemate.
3, the prevention of deadlocks are: 1, the transaction involved in the transaction all locked. So the other things will not be with you to seize resources, but obviously this, the efficiency of the operation is greatly reduced, only run the unit time to perform an operation. 2, to carry out deadlock monitoring, through the discovery there is no cycle of the waiting diagram. 3, time-out detection is also a common detection, but this time interval setting is a big problem, because there are many factors in the middle.
4, different lock mode can increase the degree of concurrency. There are 2 types of lock modes mentioned here: 1, two-version locking,2 version lock, 3 kinds of locks, read Lock,write lock, and commit lock. 2, hierarchic locks, layer-level lock mode.
optimistic concurrency control
The lock described above is a pessimistic concurrency control, and this section is about an optimistic concurrency control approach in a completely different way. The disadvantages of several locking methods are listed first:
1, do not support high concurrent access to data, during which there is generally delay.
2, will cause deadlock, this problem has been very large.
3, if there is a random failure, then the lock will not be released, so that the object waiting for the lock will fall into infinite wait.
And here we say that the principle of optimistic concurrency control is through observation. High concurrency is not afraid of conflict, nothing, we solve just.
1, in optimistic concurrency control, there are 3 stages: 1, working phase work stage, at this stage there will be a number of temporary version of the object. 2, validation phase verification phase, is to solve the first stage of conflict and contradictions. 3. Update phase, along with the commit of the transaction.
2, there are 2 ways to verify the stage: 1, backward validation, backward verification. 2, forward validation, forward verification.
time stamp sequence
The time stamp in the mode of concurrency control is a timestamp-based sequence. Each operation is assigned a separate, unique timestamp value for each transaction. The value of each timestamp represents the version of an Action object. Somewhat similar to the previous thought. The core idea is to compare the order by the time stamp value of the read-write transaction, and then decide whether to perform the operation under certain rules. For example, if you write a transaction, the timestamp value must be greater than the timestamp value of the last read transaction, so that no one is reading this object before you can proceed.
References: <<distributed sysytems Concepts and design>> original version fifth, Author:george Coulouris,jean Dollimore, Tim Kindberg,gordon Blair
Distributed Systems reading notes (16)-----Transactions and concurrency control