Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Timestamp-based concurrency control
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
In [[computer science]], a '''timestamp-based concurrency control''' algorithm is a [[optimistic concurrency control]] method. It is used in some [[database]]s to safely handle transactions using [[timestamps]]. ==Operation== ===Assumptions=== * Every timestamp value is unique and accurately represents an instant in time. * A higher-valued timestamp occurs later in time than a lower-valued timestamp. ===Generating a timestamp=== A number of different approaches can generate timestamps * Using the value of the system's clock at the start of a transaction as the timestamp. * Using a thread-safe shared counter that is incremented at the start of a transaction as the timestamp. * A combination of the above two methods. ===Formal definition=== Each transaction (<math>T_i</math>) is an ordered list of actions (<math>A_{ix}</math>). Before the transaction performs its first action (<math>A_{i1}</math>), it is marked with the current [[timestamp]], or any other [[total order|strictly totally ordered]] sequence: <math>TS(T_i) = NOW()</math>. Every transaction is also given an initially empty set of transactions upon which it depends, <math>DEP(T_i) = []</math>, and an initially empty set of old objects which it updated, <math>OLD(T_i) = []</math>. Each [[Object (computer science)|object]] <math>(O_j)</math> in the database is given two timestamp fields which are not used other than for concurrency control: * <math> RT(O_j)</math> is the timestamp of the last transaction that read the value of the object ( <math>TS(T_r)</math>, where <math>T_r</math> is the last transaction that read the value of the object). * <math>WT(O_j)</math> is the timestamp of the last transaction that updated the value of the object ( <math>TS(T_w)</math>, where <math>T_w</math> is the last transaction that updated the value of the object). For all <math>T_i</math>: :For each action <math>A_{ix}</math>: ::If <math>A_{ix}</math> wishes to read the value of <math>O_j</math>: :::If <math>WT(O_j) > TS(T_i)</math> then '''abort''' (a more recent thread has overwritten the value), :::Otherwise update the set of dependencies <math>DEP(T_i).\mathrm{add}(WT(O_j)) </math> and set <math>RT(O_j) = \max(RT(O_j), TS(T_i))</math>; ::If <math>A_{ix}</math> wishes to update the value of <math>O_j</math>: :::If <math>RT(O_j) > TS(T_i)</math> then '''abort''' (a more recent thread is already relying on the old value), :::If <math>WT(O_j) > TS(T_i)</math> then '''skip''' (the [[Thomas Write Rule]]), :::Otherwise store the previous values, <math>OLD(T_i).\mathrm{add}(O_j, WT(O_j))</math>, set <math>WT(O_j) = TS(T_i)</math>, and update the value of <math>O_j</math>. :While there is a transaction in <math>DEP(T_i)</math> that has not ended: '''wait''' :If there is a transaction in <math>DEP(T_i)</math> that aborted then '''abort''' :Otherwise: '''commit'''. To '''abort''': :For each <math>(\mathrm{old}O_j, \mathrm{old}WT(O_j)) </math> in <math>OLD(T_i)</math> ::If <math>WT(O_j)</math> equals <math>TS(T_i)</math> then restore <math>O_j = \mathrm{old}O_j</math> and <math>WT(O_j) = \mathrm{old}WT(O_j)</math> ===Informal definition=== Whenever a transaction initiated, it receives a timestamp. The '''transaction's timestamp''' indicates when the transaction was initiated. These timestamps ensure that transactions affect each object in the same sequence of their respective timestamps. Thus, given two operations that affect the same object from different transactions, the operation of the transaction with the earlier timestamp must execute before the operation of the transaction with the later timestamp. However, if the operation of the wrong transaction is actually presented first, then it is aborted and the transaction must be restarted. Every object in the database has a '''read timestamp''', which is updated whenever the object's data is read, and a '''write timestamp''', which is updated whenever the object's data is changed. If a transaction wants to read an object, * but the transaction started ''before'' the object's write timestamp it means that something changed the object's data after the transaction started. In this case, the transaction is canceled and must be restarted. * and the transaction started ''after'' the object's write timestamp, it means that it is ''safe'' to read the object. In this case, if the transaction's timestamp is after the object's read timestamp, the read timestamp is set to the transaction's timestamp. If a transaction wants to write to an object, * but the transaction started ''before'' the object's read timestamp it means that something has had a look at the object, and we assume it took a copy of the object's data. So we can't write to the object as that would make any copied data invalid, so the transaction is aborted and must be restarted. * and the transaction started ''before'' the object's write timestamp it means that something has changed the object since we started our transaction. In this case we use the [[Thomas write rule]] and simply skip our write operation and continue as normal; the transaction does not have to be aborted or restarted * otherwise, the transaction writes to the object, and the object's write timestamp is set to the transaction's timestamp. == Physically unrealizable == The behavior is '''physically unrealizable''' if the results of transactions could not have occurred if transactions were instantaneous. The following are the only two situations that result in physically unrealizable behavior: # Transaction T tries to read X but TS(T) < WT(X). Reason: It means that X has been written to by another transaction after T began. # Transaction T tries to write X but TS(T) < RT(X). Reason: It means that a later transaction read X before it was written by T. ==Recoverability== {{for|an explanation of the terms ''recoverable'' (''RC''), ''avoids cascading aborts'' (''ACA'') and ''strict'' (''ST'')|Database transaction schedule}} Note that timestamp ordering in its basic form does not produce recoverable histories. Consider for example the following history with transactions <math>T_1</math> and <math>T_2</math>: :<math>W_1(x)\;R_2(x)\;W_2(y)\;C_2\;R_1(z)\;C_1</math> This could be produced by a TO scheduler, but is not recoverable, as <math>T_2</math> commits even though having read from an uncommitted transaction. To make sure that it produces recoverable histories, a scheduler can keep a list of other transactions each transaction has ''read from'', and not let a transaction commit before this list consisted of only committed transactions. To avoid cascading aborts, the scheduler could tag data written by uncommitted transactions as ''dirty'', and never let a read operation commence on such a data item before it was untagged. To get a strict history, the scheduler should not allow any operations on dirty items. ==Implementation issues== ===Timestamp resolution=== This is the minimum time elapsed between two adjacent timestamps. If the resolution of the timestamp is too large (coarse), the possibility of two or more timestamps being equal is increased and thus enabling some transactions to commit out of correct order. For example, for a system that creates one hundred unique timestamps per second, two events that occur 2 milliseconds apart may be given the same timestamp even though they occurred at different times. ===Timestamp locking=== Even though this technique is a non-locking one, in as much as the object is not locked from concurrent access for the duration of a transaction, the act of recording each timestamp against the Object requires an extremely short duration lock on the Object or its proxy. ==See also== * [[Multiversion concurrency control]] * [[Timestamping (computing)]] [[Category:Concurrency control]] [[Category:Concurrency control algorithms]] [[Category:Transaction processing]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:For
(
edit
)