GithubHelp home page GithubHelp logo

carrot-garden / akka_akka-data-replication Goto Github PK

View Code? Open in Web Editor NEW

This project forked from patriknw/akka-data-replication

0.0 2.0 0.0 1.16 MB

Replication of CRDTs in Akka Cluster

License: Other

Scala 99.73% Java 0.27%

akka_akka-data-replication's Introduction

Akka Data Replication

This is an EARLY PREVIEW of a library for replication of data in an Akka cluster. It is a replicated in-memory data store supporting low latency and high availability requirements. The data must be so called Conflict Free Replicated Data Types (CRDTs), i.e. they provide a monotonic merge function and the state changes always converge.

For good introduction to CRDTs you should watch the Eventually Consistent Data Structures talk by Sean Cribbs.

CRDTs can't be used for all types of problems, but when they can they have very nice properties:

  • low latency of both read and writes
  • high availability (partition tolerance)
  • scalable (no central coordinator)
  • strong eventual consistency (eventual consistency without conflicts)

Built in data types:

  • Counters: GCounter, PNCounter
  • Registers: LWWRegister, Flag
  • Sets: GSet, ORSet
  • Maps: ORMap, LWWMap, PNCounterMap

You can use your own custom data types by implementing the merge function of the ReplicatedData trait. Note that CRDTs typically compose nicely, i.e. you can use the provided data types to build richer data structures.

The Replicator actor implements the infrastructure for replication of the data. It uses direct replication and gossip based dissemination. The Replicator actor is started on each node in the cluster, or group of nodes tagged with a specific role. It communicates with other Replicator instances with the same path (without address) that are running on other nodes. For convenience it is typically used with the DataReplication Akka extension.

A short example of how to use it:

class DataBot extends Actor with ActorLogging {
  import DataBot._
  import Replicator._

  val replicator = DataReplication(context.system).replicator
  implicit val cluster = Cluster(context.system)

  import context.dispatcher
  val tickTask = context.system.scheduler.schedule(5.seconds, 5.seconds, self, Tick)

  replicator ! Subscribe("key", self)

  def receive = {
    case Tick =>
      val s = ThreadLocalRandom.current().nextInt(97, 123).toChar.toString
      if (ThreadLocalRandom.current().nextBoolean()) {
        // add
        log.info("Adding: {}", s)
        replicator ! Update("key", ORSet(), WriteLocal)(_ + s)
      } else {
        // remove
        log.info("Removing: {}", s)
        replicator ! Update("key", ORSet(), WriteLocal)(_ - s)
      }

    case _: UpdateResponse => // ignore

    case Changed("key", data: ORSet) =>
      log.info("Current elements: {}", data.value)
  }

  override def postStop(): Unit = tickTask.cancel()

}

The full source code for this sample is in DataBot.scala.

More detailed documentation can be found in the ScalaDoc of Replicator and linked classes.

Two other examples:

Dependency

Latest version of akka-data-replication is 0.8. This version depends on Akka 2.3.6 and is cross-built against Scala 2.10.4 and 2.11.4.

Add the following lines to your build.sbt file:

resolvers += "patriknw at bintray" at "http://dl.bintray.com/patriknw/maven"

libraryDependencies += "com.github.patriknw" %% "akka-data-replication" % "0.8"

More Resources

akka_akka-data-replication's People

Contributors

hseeberger avatar patriknw avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.