---
title: "Clock synchronization need; Lamport"
subject: "Distributed System"
chapter: 5
key: "semester_7/distributed_system:clock-sync-lamport-need"
source: https://www.bctengineer.com/q/semester_7/distributed_system/clock-sync-lamport-need
site: BCT Engineering
---

# Clock synchronization need; Lamport

Distributed System · Chapter 5: Time and State in Distributed Systems

- Asked in: Model question
- Video: [Lamport's Logical Clock | Algorithm | Part-1/2 | Distributed Systems | Lec-56 | Bhanu Priya](https://www.youtube.com/watch?v=mo8OPP5FCTg) — Education 4u
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_7/distributed_system/clock-sync-lamport-need
- Whole chapter: https://www.bctengineer.com/md/semester_7/distributed_system/chapter-5

## Question

Define clock synchronization. What is the need of clock synchronization? Explain the Lamport's clock synchronization algorithm along with an example.

## Short answer

Clock synchronization makes distributed clocks agree on time or event order. It is needed because clocks drift, making timestamps incomparable, affecting event ordering, mutual exclusion, and security. Lamport's algorithm uses a logical counter: increment on each event, timestamp messages, and on receive set counter to max(local, received)+1. Example: send at 2, receive at 3 ensures happened-before ordering.

## Answer

#### Clock synchronization
Clock synchronization is the process of making the clocks of different computers in a distributed system agree, either with each other (internal) or with an external reference such as UTC (external), or, for logical clocks, agree on the order of events.

#### Need
- Each machine has its own clock, and the clocks drift apart, so timestamps on different machines are not comparable.
- Correct ordering of events and transactions (log files, banking, `make` build tools using file times).
- Distributed algorithms: mutual exclusion, deadlock detection, replication, consistent snapshots.
- Security (Kerberos ticket expiry), billing, scheduling and real-time systems.

#### Lamport's clock synchronization algorithm
Lamport defined the happened-before relation (→): events in one process are ordered; a message send happens before its receipt; the relation is transitive. The algorithm gives counters consistent with it.
1. Each process Pi keeps a counter Ci, initialised to 0.
2. Before each event Ci = Ci + 1.
3. When Pi sends a message m, it timestamps it with t = Ci.
4. When Pj receives (m, t), it sets Cj = max(Cj, t) + 1.

#### Example
```text
 P1: a(1)---s1(2)---------------------d(5)
              \ m1(t=2)                  ^
 P2: c(1)------e(3)----s2(4)-------------+
                       m2(t=4)
```
P1's event a has C1 = 1, and the send of m1 has C1 = 2. P2's local event c gives C2 = 1. On receiving m1 (t=2), P2 sets C2 = max(1,2)+1 = 3 (event e). P2 sends m2 at C2 = 4. P1 (C1 = 2) receives m2 and sets C1 = max(2,4)+1 = 5 (event d).
Thus send of m1 (2) < receive (3), send of m2 (4) < receive (5). Equal timestamps on different processes are ordered by process number.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Why is clock synchronisation needed?
   A. Clocks never drift
   B. Only to save power
   C. Clocks drift so timestamps on different machines are not comparable
   D. To avoid networks
   Answer: C. Ordering of events, transactions and security tickets depends on it. (Key point: Need)

2. P2 has C=1 and receives message with t=2. New C2?
   A. 2
   B. 1
   C. 4
   D. 3
   Answer: D. max(1,2)+1 = 3. (Key point: Receive rule)

3. In the example, P1 (C1=2) receives m2 with t=4. What is C1?
   A. 5
   B. 4
   C. 3
   D. 6
   Answer: A. max(2,4)+1 = 5. (Key point: Example result)

4. Which defines the happened-before relation?
   A. Real clock comparison
   B. Order within a process, send before receive, and transitivity
   C. Equal timestamps
   D. Message size
   Answer: B. These three rules define ->. (Key point: Happened-before)

## Related in Distributed System

- [Need of logical clock synchronization](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Alogical-clock-sync-need) — Distributed System, chapter 5
- [Vector clock synchronization algorithm](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Avector-clock-algorithm) — Distributed System, chapter 5, asked 2×
- [Lamport's logical clock algorithm](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Alamport-clock) — Distributed System, chapter 5, asked 5×
- [Clock sync problems; Chandy-Lamport](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aphysical-clock-problems-chandy-lamport) — Distributed System, chapter 5
- [Physical clock synchronization algorithm](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Aphysical-clock-sync-algorithm) — Distributed System, chapter 5
- [Problems of Lamport's clock; vector clock](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Alamport-problems-vector-clock) — Distributed System, chapter 5, asked 2×

## Related in other subjects

- [Clock synchronization in DFS](https://www.bctengineer.com/question.md?key=semester_8%2Fbig_data_technologies%3Aclock-synchronization-dfs) — Big Data Technologies, chapter 1
- [Need for replacement algorithm in cache](https://www.bctengineer.com/question.md?key=semester_5%2Fcomputer_organization_and_architecture%3Areplacement-why) — Computer Organization and Architecture, chapter 6
