Skip to content

LWWRegister

Defined in: src/crdt/LWWRegister.ts:26

Last-Writer-Wins register. A single value with a timestamp; on merge the higher timestamp wins. Concurrent writes (same ts on two replicas) are resolved deterministically by replicaId lexicographic order — same input on every node, same winner.

Use this for single-value state that’s eventually consistent — a user’s display name, a feature-flag value, the latest-known health status of a service. When concurrent writes are common, pick a CRDT that captures both branches (e.g. an OR-Set of values) instead.

Wall-clock pitfall. Default timestamps come from Date.now(), which can drift between machines and even go backwards on the same machine (NTP correction). In practice this means a write from a faster-clocked node always wins; if that’s a problem, pass a clock option backed by a hybrid logical clock (HLC) or a Lamport counter.

const a = LWWRegister.empty().assign(‘a’, ‘red’); const b = LWWRegister.empty().assign(‘b’, ‘blue’); a.merge(b).value() // → whichever assign() was called later

V

  • Crdt<LWWRegister<V>>

assign(replica, value, timestamp?): LWWRegister<V>

Defined in: src/crdt/LWWRegister.ts:44

Set the register to value, stamped with timestamp (default Date.now()) on behalf of replica. The timestamp is what merge uses to decide who wins — pass an explicit one if you want HLC/Lamport semantics.

string

V

number = ...

LWWRegister<V>


equals(other): boolean

Defined in: src/crdt/LWWRegister.ts:80

LWWRegister<V>

boolean


merge(other): LWWRegister<V>

Defined in: src/crdt/LWWRegister.ts:54

Join two replicas. Must be a join-semilattice operation: total, idempotent, commutative, associative.

LWWRegister<V>

LWWRegister<V>

Crdt.merge


timestamp(): number

Defined in: src/crdt/LWWRegister.ts:52

Timestamp of the last write — 0 for an empty register.

number


toJSON(): LWWRegisterJson<V>

Defined in: src/crdt/LWWRegister.ts:66

Wire-friendly representation — every CRDT must be JSON-encodable so it can travel through the cluster transport without bespoke codecs. toJSON() is the inverse of the static fromJSON factory each impl exposes.

LWWRegisterJson<V>

Crdt.toJSON


value(): V | null

Defined in: src/crdt/LWWRegister.ts:49

Current value, or null if no assign has been called.

V | null


static empty<V>(): LWWRegister<V>

Defined in: src/crdt/LWWRegister.ts:34

Empty register — no value yet. value() returns null.

V

LWWRegister<V>


static fromJSON<V>(json): LWWRegister<V>

Defined in: src/crdt/LWWRegister.ts:75

V

LWWRegisterJson<V>

LWWRegister<V>