This project is part of the @thi.ng/umbrella monorepo.
Vector clock functions for synchronizing distributed states & processes.
- Initially all clocks are zero.
- Each time a process experiences an internal event, it increments its own
logical clock in the vector by one (
inc()
). - Each time a process sends a message, it increments its own logical clock in the vector by one and then sends a copy of its own vector.
- Each time a process receives a message, it increments its own logical clock in
the vector by one and updates each element in its vector by taking the maximum
of the value in its own vector clock and the value in the vector in the
received message (
merge()
).
The package provides the following immutable vector clock operations, where vector clocks are plain JS objects:
inc(clock, id)
- increment (or create) clock componentremove(clock, id)
- remove clock componentmerge(a, b)
- merge two vector clockssignedSkew(a, b)
- max signed difference between vector clocksabsSkew(a, b)
- max unsigned difference between vector clockscompare(a, b)
- comparator for logically ordering vector clocksisBefore(a, b)
- true if a < bisAfter(a, b)
- true if a > bisConcurrent(a, b)
- if both clocks represent concurrent updatesequiv(a, b)
- equality predicateorderAsc(a, b)
- alias forcompare()
orderDesc(a, b)
- reverse order toorderAsc()
References:
- Wikipedia
- Princeton COS 418: Distributed Systems
- F. Mattern: Virtual Time and Global States of Distributed Systems
- P. Krzyzanowski: Clock synchronization
- L. Lamport
- Akka docs
BETA - possibly breaking changes forthcoming
Search or submit any issues for this package
yarn add @thi.ng/vclock
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/vclock?module" crossorigin></script>
// UMD
<script src="https://unpkg.com/@thi.ng/vclock/lib/index.umd.js" crossorigin></script>
Package sizes (gzipped, pre-treeshake): ESM: 478 bytes / CJS: 558 bytes / UMD: 614 bytes
TODO
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-vclock,
title = "@thi.ng/vclock",
author = "Karsten Schmidt",
note = "https://thi.ng/vclock",
year = 2018
}
© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0