ORSet example, MovieWatchList

This commit is contained in:
Patrik Nordwall 2020-08-11 13:39:25 +02:00 committed by Christopher Batey
parent d078a6b65f
commit 2e0821c2f0
6 changed files with 244 additions and 17 deletions

View file

@ -134,13 +134,41 @@ The factory returns a `Behavior` that can be spawned like any other behavior.
### Conflict free replicated data types
The following CRDTs are included that can be used to build your own data model:
Writing code to resolve conflicts can be complicated to get right.
One well-understood technique to create eventually-consistent systems is to
model your state as a Conflict Free Replicated Data Type, a CRDT. There are two types of CRDTs;
operation-based and state-based. For Replicated Event Sourcing the operation-based is a good fit,
since the events represent the operations. Note that this is distinct from the CRDT's implemented
in @ref:[Akka Distributed Data](distributed-data.md), which are state-based rather than operation-based.
The rule for operation-based CRDT's is that the operations must be commutative — in other words, applying the same events
(which represent the operations) in any order should always produce the same final state. You may assume each event
is applied only once, with @ref:[causal delivery order](#causal-delivery-order).
The following CRDTs are included that can you can use as the state or part of the state in the entity:
* @apidoc[LwwTime]
* @apidoc[Counter]
* @apidoc[akka.persistence.typed.crdt.ORSet]
Akka serializers are included for all these types and can be used to serialize when @ref[embedded in Jackson](../serialization-jackson.md#using-akka-serialization-for-embedded-types).
Akka serializers are included for all these types and can be used to serialize when
@ref[embedded in Jackson](../serialization-jackson.md#using-akka-serialization-for-embedded-types).
An example would be a movies watch list that is represented by the general purpose
@apidoc[akka.persistence.typed.crdt.ORSet] CRDT. `ORSet` is short for Observed Remove Set. Elements can be added and
removed any number of times. Concurrent add wins over remove. It is an operation based CRDT where the delta of an
operation (add/remove) can be represented as an event.
Such movies watch list example:
Scala
: @@snip [movie](/akka-persistence-typed-tests/src/test/scala/docs/akka/persistence/typed/ReplicatedMovieWatchListExampleSpec.scala) { #movie-entity }
Java
: @@snip [movie](/akka-persistence-typed-tests/src/test/java/jdocs/akka/persistence/typed/ReplicatedMovieExample.java) { #movie-entity }
The @ref[Auction example](replicated-eventsourcing-auction.md) is a more comprehensive example that illustrates how application-specific
rules can be used to implement an entity with CRDT semantics.
### Last writer wins