Crates.io | hybrid-logical-clock |
lib.rs | hybrid-logical-clock |
version | 0.0.2 |
source | src |
created_at | 2024-08-21 03:26:00.719668 |
updated_at | 2024-08-21 03:31:13.503277 |
description | A Rust implementation of Hybrid Logical Clocks for distributed systems |
homepage | |
repository | https://github.com/zamderax/hybrid-logical-clock |
max_upload_size | |
id | 1346055 |
size | 11,627 |
I built this crate to provide a hybrid logical clock in Rust so that I could learn about distributed systems.
You can create a new hybrid logical clock with the physical clock time set to the given value.
use hybrid_logical_clock::HybridLogicalClock;
let hlc = HybridLogicalClock::new(100);
Sometimes you want to create a hybrid logical clock with both the physical and logical clock time set.
use hybrid_logical_clock::HybridLogicalClock;
let hlc = HybridLogicalClock::new_with_both_physical_and_logical_clock_time(100, 100);
You can compare two hybrid logical clocks to see if they are causally related.
use hybrid_logical_clock::HybridLogicalClock;
let hlc1 = HybridLogicalClock::new(100);
let hlc2 = HybridLogicalClock::new(200);
assert!(hlc1.is_concurrent(&hlc2));
Most distributed systems use lamport or logical clocks to order events. However, these clocks have several drawbacks:
Hybrid Logical Clocks (HLCs) offer several advantages over traditional logical clocks or physical clocks alone:
Causality tracking: HLCs combine the benefits of physical and logical clocks, allowing for accurate causality tracking between events in distributed systems.
Better performance: Unlike vector clocks, HLCs have constant-size timestamps, leading to improved performance in large-scale distributed systems.
Clock drift tolerance: HLCs can handle clock drift between nodes in a distributed system more gracefully than pure physical clocks.
Monotonicity: HLCs ensure that timestamps always move forward, even if there are small backwards jumps in the physical clock.
Fine-grained ordering: When multiple events occur at the same physical time, HLCs can still establish a total order using the logical component.
Compatibility: HLCs can be used as a drop-in replacement for physical timestamps in many systems, providing additional causality information without major architectural changes.
Conflict resolution: In distributed databases or collaborative editing systems, HLCs can help in detecting and resolving conflicts between concurrent updates.
By using a Hybrid Logical Clock, you can achieve a more robust and accurate timekeeping mechanism in distributed systems, leading to better consistency and easier reasoning about event ordering.