Class LWWRegister<A>

  • All Implemented Interfaces:
    ReplicatedData, ReplicatedDataSerialization, java.io.Serializable

    public final class LWWRegister<A>
    extends java.lang.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:
    Serialized Form
    • Method Detail

      • 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.
      • 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$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.

      • 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.
      • toString

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

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object