Synchronization in Distributed Systems
Synchronization in Distributed Systems
Synchronization in Distributed
Systems
In a centralized system: all processes reside
on the same system utilize the same clock.
Broadcast Based
Request Driven
Broadcast Based – first
approach
The centralized time server’s action:
The physical time service broadcasts periodically the current time to
members of the distributed systems.
The participants’ action:
If a given participant’s clock is ahead of the time server’s clock, the
participant slows down its clock so that it will continually move
closer to the accurate time.
If a participant’s clock is behind the time server’s clock, the
participant moves its clock forward. Alternatives do include gradually
speeding up the clock.
For example
Location A Time server
Current time Current time=740
= 720 Broadcast based
Delay of 10
Location A
Current time=720
Adjusted current time=750
New current time=750
Broadcast Based – second
approach (Berkeley algorithm)
Location A Time Server Location B
1 Current time=740 1
2 Adjusted location A 2 Current time=732
Current time=720 =730 Delay=6
Delay=10
Adjusted location B Slow clock down to
4 =738 5 accommodate 2
Move forward=6 Average and the new
current time=736
Current time=740
Adjusted time=750 Delay=10
720
724
726
718
722
723
Delete the times that are above the
threshold and then average the rest.
Adjusted received times
760 X
724
726
718
702 X
723
703 X
765 X
Logical Clocks
Why Logical Clocks?
It is difficult to utilize physical clocks to
order events uniquely in distributed
systems.
The essence of logical clocks is based on
the happened-before relationship presented
by Lamport.
Happen-Before Relationship
If two events, a and b, occurred at the same
process, they occurred in the order of which they
were observed. That is, a > b.
If a sends a message to b, then a > b. That is, you
cannot receive something before it is sent. This
relationship holds regardless of where events a
and b occur.
C D
Process 2
A B
Process 1
A>B>C>D>F E
Lamport’s Algorithm
Each process increments its clock counter
between every two consecutive events.
If a sends a message to b, then the message must
include T(a). Upon receiving a and T(a), the
receiving process must set its clock to the greater
of [T(a)+d, Current Clock]. That is, if the
recipient’s clock is behind, it must be advanced to
preserve the happen-before relationship. Usually
d=1.
For example
E(1) F(5)
Process 3
C(3) D(4)
Process 2
A(1) B(2)
Process 1
Total Ordering with Logical
Clocks
E(1.3) F(5.3)
Process 3
C(3.2) D(4.2)
Process 2
A(1.1) B(2.1)
Process 1
A>E>B>C>D>F
Mutual Exclusion
In single-processor systems, critical regions
are protected using semaphores, monitors,
and similar constructs.
In distributed systems, since there is no
shared memory, these methods cannot be
used.
A Centralized Algorithm
coordinator
process Request
Grant
Exit
REQ REQ
Token Ring Algorithm
A Comparison of the Three
Algorithms
Algorithm Messages Delay Problems
per entry/exit before entry
1 1
7 2 7 2
Coordinator
6 Ok 3 6 3
5 4 5 4
A Ring Algorithm
234561
1
2 6
2
7
3
23
23456 6
4
5
2345 234
Atomic Transactions
All the synchronization techniques we have
studied so far are essentially low level, like
semaphores.
What we would really like is a much
higher-level abstraction such as atomic
transaction.
For example
Atomic bank transactions:
1. Withdraw(amount, account1)
2. Deposit(amount, account2)
Stable Storage
Stable storage is designed to survive
anything except major calamities such as
floods and earthquakes.
Stable storage can be implemented with a
pair of ordinary disks.
Stable storage is well suited to applications
that require a high degree of fault
tolerance, such as atomic transactions.
Stable storage Stable storage Stable storage
Drive 1 s a s a’ s a
o h o h o h
t f t f t f
b w b w b w
s a s a s a
Drive 2
t f t f t f
b w b w b w
(a) Stable storage (b) Crash after drive 1 is updated © Bad spot
Transaction Primitives
1 BEGIN_TRANSACTION: Mark the start of a transaction.
2 END_TRANSACTION: Terminate the transaction and try to commit.
3 ABORT_TRANSACTION: Kill the transaction; restore the old values.
4 READ: Read data from a file (or other object).
5 WRITE: Write data to a file (or other object).
For example,
BEGIN_TRANSACTION
reserve Austin-Houston;
reserve Houston-Los Angeles;
reserve Los Angeles-Seatle;
END_TRANSCATION
Properties of Transactions
1 Atomic: To the outside world, the
transaction happens indivisibly.
2 Consistent: The transaction does not
violate system invariants.
3 Isolated: Concurrent transactions do
not interfere with each other.
4 Durable: Once a transaction commits,
the changes are permanent.
Isolated or serializable
Isolated or serializable means that if two or
more transactions are running at the same
time, to each of them and to other
processes, the final result looks as though
all transactions ran sequentially in some
(system dependent) order.
An example
BEGIN_TRANACATION
X = 0;
X=X+1;
END_TRANSACTION
(a)
BEGIN_TRANSACTION
X=0;
X= X+2;
END_TRANSACTION
(b)
BEGIN_TRANSACTION
X=0;
X=X+3;
END_TRANSACTION
(c )
Schedule 1 x=0; x=x+1; x=0; x=x+2; x=0; x=x+3; legal
1 2 0 1 2 0 1 2
0’ 3’ 0 3
Writeahead log
x=0;
y=0;
BEGIN_TRANSACTION
x=x+1; log: x=0/;
y=y+2; log: x=0/1; y=0/2;
x=y * y; log: x=0/1; y=0/2; x=1/4;
END_TRANSACTION
Achieving atomic commit in a
distributed system
Two-Phase Commit Protocol
Coordinator Subordinates
Write “Prepare” in the log
Send “Prepare” message
Phase 1 Write “Ready” in the log
Send “Ready” message
Write log record (if all are ready, commit; if not, abort)
Send “Commit” message
Phase 2 Write “Commit” in the log
Commit
Send “Finished” message
Concurrency Control
When multiple transactions are executing
simultaneously in different processes, some
mechanism is needed to keep them out of
each other’s way. That mechanism is called
a concurrency control algorithm.
Concurrency control
algorithms
Locking
In the simplest form, when a process needs to
read or write a file (or other object) as part of a
transaction, it first locks the file.
Distinguishing read locks from write locks.
The unit of locking can be an individual record
or page, a file, or a larger item.
Two-phase locking
The process first acquires all the locks it needs
during the growing phase, then releases them
during the shrinking phase.
In many systems, the shrinking phase does not
take place until the transaction has finished
running and has either committed or aborted. This
policy is called strict two-phase locking.
Two-phase locking
Lock point
Time
Optimistic Concurrency Control
A second approach to handling multiple
transactions at the same time is optimistic
concurrency control. The idea is simple:
just go ahead and do whatever you want to,
without paying attention to what anybody
else is doing. If there is a problem, worry
about it later.
Timestamps
Write
TRD TWR T T TRD
() () () () ()
Do tenative Abort
write
TWR TRD T T TWR
() () () () ()
Abort
Do tentative
write
Read
TWR T T TWR
() () () ()
Ok Abort