Berkeley’s Algorithm is a clock synchronization technique used in distributed systems. The algorithm assumes that each machine node in the network either doesn’t have an accurate time source or doesn’t possess an UTC server.
1) An individual node is chosen as the master node from a pool nodes in the network. This node is the main node in the network which acts as a master and rest of the nodes act as slaves. Master node is chosen using a election process/leader election algorithm.
2) Master node periodically pings slaves nodes and fetches clock time at them using Cristian’s algorithm.
Diagram below illustrates how the master sends request to slave nodes.
Diagram below illustrates how slave nodes send back time given by their system clock.
3) Master node calculates average time difference between all the clock times received and the clock time given by master’s system clock itself. This average time difference is added to the current time at master’s system clock and broadcasted over the network.
Psuedocode for above step:
# receiving time from all slave nodes repeat_for_all_slaves: time_at_slave_node = receive_time_at_slave() # calculating time difference time_difference = time_at_master_node - time_at_slave_node # average time difference calculation average_time_difference = sum(all_time_differences) / number_of_slaves synchronized_time = current_master_time + average_time_difference # broadcasting synchronized to whole network broadcast_time_to_all_slaves(synchronized_time)
Diagram below illustrates the last step of Berkeley’s algorithm.
Scope of Improvement
- Improvision in accuracy of cristian’s algorithm.
- Ignoring significant outliers in calculation of average time difference
- In case master node fails/corrupts, a secondary leader must be ready/pre-chosen to take the place of the master node to reduce downtime caused due to master’s unavailability.
- Instead of sending the synchronized time, master broadcasts relative inverse time difference, which leads to decrease in latency induced by traversal time in the network while time of calculation at slave node.
The code below is a python script which can be used to trigger a master clock server.
New synchroniztion cycle started. Number of clients to be synchronized: 3 Client Data updated with: 127.0.0.1:57284 Client Data updated with: 127.0.0.1:57274 Client Data updated with: 127.0.0.1:57272
The code below is a python script which can be used to trigger a slave/client.
Recent time sent successfully Synchronized time at the client is: 2018-11-23 18:49:31.166449
Below is a screenshot of runtime of above python scripts where top left console represents master thread while others represents slave threads.
Note: The scripts above closely depicts working of Berkley’s Algorithm but may differ from the actual implementation of the algorithm in production based distributed networking systems. Availability of port 8080 is machine dependent. In case port 8080 is not free, change the port number accordingly in both master and slave scripts.