Class LWWRegister<A>

java.lang.Object
org.apache.pekko.cluster.ddata.LWWRegister<A>
All Implemented Interfaces:
Serializable, ReplicatedData, ReplicatedDataSerialization

public final class LWWRegister<A> extends Object implements ReplicatedData, ReplicatedDataSerialization
Implements a 'Last Writer Wins Register' CRDT, also called a 'LWW-Register'.

It is described in the paper A comprehensive study of Convergent and Commutative Replicated Data Types.

Merge takes the register with highest timestamp. Note that this relies on synchronized clocks. LWWRegister should only be used when the choice of value is not important for concurrent updates occurring within the clock skew.

Merge takes the register updated by the node with lowest address (UniqueAddress is ordered) if the timestamps are exactly the same.

Instead of using timestamps based on System.currentTimeMillis() time it is possible to use a timestamp value based on something else, for example an increasing version number from a database record that is used for optimistic concurrency control.

The defaultClock is using max value of System.currentTimeMillis() and currentTimestamp + 1. This means that the timestamp is increased for changes on the same node that occurs within the same millisecond. It also means that it is safe to use the LWWRegister without synchronized clocks when there is only one active writer, e.g. a Cluster Singleton. Such a single writer should then first read current value with ReadMajority (or more) before changing and writing the value with WriteMajority (or more).

For first-write-wins semantics you can use the reverseClock() instead of the defaultClock()

This class is immutable, i.e. "modifying" methods return a new instance.

See Also:
  • Method Details

    • defaultClock

      public static <A> LWWRegister.Clock<A> defaultClock()
      The default LWWRegister.Clock is using max value of System.currentTimeMillis() and currentTimestamp + 1.
    • reverseClock

      public static <A> LWWRegister.Clock<A> reverseClock()
      This LWWRegister.Clock can be used for first-write-wins semantics. It is using min value of -System.currentTimeMillis() and currentTimestamp + 1, i.e. it is counting backwards.
    • apply

      public static <A> LWWRegister<A> apply(SelfUniqueAddress node, A initialValue)
    • apply

      public static <A> LWWRegister<A> apply(SelfUniqueAddress node, A initialValue, LWWRegister.Clock<A> clock)
    • create

      public static <A> LWWRegister<A> create(A initialValue, SelfUniqueAddress node, LWWRegister.Clock<A> clock)
      Scala API Creates a LWWRegister with implicits, given deprecated apply functions using Cluster constrain overloading.
    • create

      public static <A> LWWRegister<A> create(SelfUniqueAddress node, A initialValue, LWWRegister.Clock<A> clock)
      Java API
    • create

      public static <A> LWWRegister<A> create(SelfUniqueAddress node, A initialValue)
      Java API
    • create$default$3

      public static <A> LWWRegister.Clock<A> create$default$3(A initialValue)
    • unapply

      public static <A> scala.Option<A> unapply(LWWRegister<A> c)
      Extract the value().
    • value

      public A value()
    • timestamp

      public long timestamp()
    • getValue

      public A getValue()
      Java API
    • withValue

      public LWWRegister<A> withValue(SelfUniqueAddress node, A value, LWWRegister.Clock<A> clock)
      Change the value of the register.

      You can provide your clock implementation instead of using timestamps based on System.currentTimeMillis() time. The timestamp can for example be an increasing version number from a database record that is used for optimistic concurrency control.

    • withValue

      public LWWRegister<A> withValue(SelfUniqueAddress node, A value)
      Change the value of the register.
    • withValueOf

      public LWWRegister<A> withValueOf(A value, SelfUniqueAddress node, LWWRegister.Clock<A> clock)
      Change the value of the register.

      You can provide your clock implementation instead of using timestamps based on System.currentTimeMillis() time. The timestamp can for example be an increasing version number from a database record that is used for optimistic concurrency control.

    • updatedBy

      public UniqueAddress updatedBy()
      The current value was set by this node.
    • withValueOf$default$3

      public LWWRegister.Clock<A> withValueOf$default$3(A value)
    • merge

      public LWWRegister<A> merge(LWWRegister<A> that)
      INTERNAL API
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object