Offset in Cassandra

The CassandraProjectionCassandraProjection has support for storing the offset in Cassandra.

The source of the envelopes can be events from Akka Persistence or any other SourceProvider with supported offset types.

The envelope handler can integrate with anything, such as publishing to a message broker, or updating a read model in Cassandra.

The CassandraProjection offers at-least-once and at-most-once processing semantics, but not exactly-once.

Dependencies

The Akka dependencies are available from Akka’s library repository. To access them there, you need to configure the URL for this repository.

sbt
resolvers += "Akka library repository".at("https://repo.akka.io/maven")
Maven
<project>
  ...
  <repositories>
    <repository>
      <id>akka-repository</id>
      <name>Akka library repository</name>
      <url>https://repo.akka.io/maven</url>
    </repository>
  </repositories>
</project>
Gradle
repositories {
    mavenCentral()
    maven {
        url "https://repo.akka.io/maven"
    }
}

To use the Cassandra module of Akka Projections add the following dependency in your project:

sbt
libraryDependencies += "com.lightbend.akka" %% "akka-projection-cassandra" % "1.6.3"
Maven
<properties>
  <scala.binary.version>2.13</scala.binary.version>
</properties>
<dependencies>
  <dependency>
    <groupId>com.lightbend.akka</groupId>
    <artifactId>akka-projection-cassandra_${scala.binary.version}</artifactId>
    <version>1.6.3</version>
  </dependency>
</dependencies>
Gradle
def versions = [
  ScalaBinary: "2.13"
]
dependencies {
  implementation "com.lightbend.akka:akka-projection-cassandra_${versions.ScalaBinary}:1.6.3"
}

Akka Projections requires Akka 2.10.0 or later, see Akka version.

Project Info: Akka Projections Cassandra
Artifact
com.lightbend.akka
akka-projection-cassandra
1.6.3
JDK versions
Eclipse Temurin JDK 11
Eclipse Temurin JDK 17
Eclipse Temurin JDK 21
Scala versions2.13.15, 3.3.4
JPMS module nameakka.projection.cassandra
License
Readiness level
Supported, support is available from Lightbend
Since 1.0.0, 2020-09-10
Home pagehttps://akka.io
API documentation
Forums
Release notesGitHub releases
IssuesGitHub issues
Sourceshttps://github.com/akka/akka-projection

Transitive dependencies

The table below shows akka-projection-cassandra’s direct dependencies and the second tab shows all libraries it depends on transitively.

Direct dependencies
OrganizationArtifactVersion
com.lightbend.akkaakka-projection-core_2.131.6.3
com.lightbend.akkaakka-stream-alpakka-cassandra_2.139.0.0
com.typesafe.akkaakka-persistence-query_2.132.10.0
org.scala-langscala-library2.13.15
Dependency tree
com.lightbend.akka    akka-projection-core_2.13    1.6.3
    com.typesafe.akka    akka-actor-typed_2.13    2.10.0    BUSL-1.1
        com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
            com.typesafe    config    1.4.3    Apache-2.0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
        com.typesafe.akka    akka-slf4j_2.13    2.10.0    BUSL-1.1
            com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
                com.typesafe    config    1.4.3    Apache-2.0
                org.scala-lang    scala-library    2.13.15    Apache-2.0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
            org.slf4j    slf4j-api    2.0.16
        org.scala-lang    scala-library    2.13.15    Apache-2.0
        org.slf4j    slf4j-api    2.0.16
    com.typesafe.akka    akka-persistence-query_2.13    2.10.0    BUSL-1.1
        com.typesafe.akka    akka-persistence_2.13    2.10.0    BUSL-1.1
            com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
                com.typesafe    config    1.4.3    Apache-2.0
                org.scala-lang    scala-library    2.13.15    Apache-2.0
            com.typesafe.akka    akka-stream_2.13    2.10.0    BUSL-1.1
                com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
                    com.typesafe    config    1.4.3    Apache-2.0
                    org.scala-lang    scala-library    2.13.15    Apache-2.0
                com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
                org.reactivestreams    reactive-streams    1.0.4    MIT-0
                org.scala-lang    scala-library    2.13.15    Apache-2.0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
        com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
        com.typesafe.akka    akka-stream_2.13    2.10.0    BUSL-1.1
            com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
                com.typesafe    config    1.4.3    Apache-2.0
                org.scala-lang    scala-library    2.13.15    Apache-2.0
            com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
            org.reactivestreams    reactive-streams    1.0.4    MIT-0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
        org.scala-lang    scala-library    2.13.15    Apache-2.0
    com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
    com.typesafe.akka    akka-stream_2.13    2.10.0    BUSL-1.1
        com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
            com.typesafe    config    1.4.3    Apache-2.0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
        com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
        org.reactivestreams    reactive-streams    1.0.4    MIT-0
        org.scala-lang    scala-library    2.13.15    Apache-2.0
    org.scala-lang    scala-library    2.13.15    Apache-2.0
com.lightbend.akka    akka-stream-alpakka-cassandra_2.13    9.0.0    BUSL-1.1
    com.datastax.oss    java-driver-core    4.17.0
        com.datastax.oss    java-driver-shaded-guava    25.1-jre-graal-sub-1    Apache 2
        com.datastax.oss    native-protocol    1.5.1    Apache 2
        com.fasterxml.jackson.core    jackson-core    2.17.2    The Apache Software License, Version 2.0
        com.fasterxml.jackson.core    jackson-databind    2.17.2    The Apache Software License, Version 2.0
            com.fasterxml.jackson.core    jackson-annotations    2.17.2    The Apache Software License, Version 2.0
            com.fasterxml.jackson.core    jackson-core    2.17.2    The Apache Software License, Version 2.0
        com.github.jnr    jnr-posix    3.1.15    Eclipse Public License - v 2.0
            com.github.jnr    jnr-constants    0.10.3    The Apache Software License, Version 2.0
            com.github.jnr    jnr-ffi    2.2.11    The Apache Software License, Version 2.0
                com.github.jnr    jffi    1.3.9    The Apache Software License, Version 2.0
                com.github.jnr    jnr-a64asm    1.0.0    The Apache Software License, Version 2.0
                com.github.jnr    jnr-x86asm    1.0.2    MIT License
                org.ow2.asm    asm-analysis    9.2    BSD-3-Clause
                    org.ow2.asm    asm-tree    9.2    BSD-3-Clause
                        org.ow2.asm    asm    9.2    BSD-3-Clause
                org.ow2.asm    asm-commons    9.2    BSD-3-Clause
                    org.ow2.asm    asm-analysis    9.2    BSD-3-Clause
                        org.ow2.asm    asm-tree    9.2    BSD-3-Clause
                            org.ow2.asm    asm    9.2    BSD-3-Clause
                    org.ow2.asm    asm-tree    9.2    BSD-3-Clause
                        org.ow2.asm    asm    9.2    BSD-3-Clause
                    org.ow2.asm    asm    9.2    BSD-3-Clause
                org.ow2.asm    asm-tree    9.2    BSD-3-Clause
                    org.ow2.asm    asm    9.2    BSD-3-Clause
                org.ow2.asm    asm-util    9.2    BSD-3-Clause
                    org.ow2.asm    asm-analysis    9.2    BSD-3-Clause
                        org.ow2.asm    asm-tree    9.2    BSD-3-Clause
                            org.ow2.asm    asm    9.2    BSD-3-Clause
                    org.ow2.asm    asm-tree    9.2    BSD-3-Clause
                        org.ow2.asm    asm    9.2    BSD-3-Clause
                    org.ow2.asm    asm    9.2    BSD-3-Clause
                org.ow2.asm    asm    9.2    BSD-3-Clause
        com.github.stephenc.jcip    jcip-annotations    1.0-1    Apache License, Version 2.0
        com.typesafe    config    1.4.3    Apache-2.0
        io.dropwizard.metrics    metrics-core    4.1.18
            org.slf4j    slf4j-api    2.0.16
        io.netty    netty-handler    4.1.94.Final
            io.netty    netty-buffer    4.1.94.Final
                io.netty    netty-common    4.1.94.Final
            io.netty    netty-codec    4.1.94.Final
                io.netty    netty-buffer    4.1.94.Final
                    io.netty    netty-common    4.1.94.Final
                io.netty    netty-common    4.1.94.Final
                io.netty    netty-transport    4.1.94.Final
                    io.netty    netty-buffer    4.1.94.Final
                        io.netty    netty-common    4.1.94.Final
                    io.netty    netty-common    4.1.94.Final
                    io.netty    netty-resolver    4.1.94.Final
                        io.netty    netty-common    4.1.94.Final
            io.netty    netty-common    4.1.94.Final
            io.netty    netty-resolver    4.1.94.Final
                io.netty    netty-common    4.1.94.Final
            io.netty    netty-transport-native-unix-common    4.1.94.Final
                io.netty    netty-buffer    4.1.94.Final
                    io.netty    netty-common    4.1.94.Final
                io.netty    netty-common    4.1.94.Final
                io.netty    netty-transport    4.1.94.Final
                    io.netty    netty-buffer    4.1.94.Final
                        io.netty    netty-common    4.1.94.Final
                    io.netty    netty-common    4.1.94.Final
                    io.netty    netty-resolver    4.1.94.Final
                        io.netty    netty-common    4.1.94.Final
            io.netty    netty-transport    4.1.94.Final
                io.netty    netty-buffer    4.1.94.Final
                    io.netty    netty-common    4.1.94.Final
                io.netty    netty-common    4.1.94.Final
                io.netty    netty-resolver    4.1.94.Final
                    io.netty    netty-common    4.1.94.Final
        org.hdrhistogram    HdrHistogram    2.1.12    Public Domain, per Creative Commons CC0
        org.reactivestreams    reactive-streams    1.0.4    MIT-0
        org.slf4j    slf4j-api    2.0.16
    com.fasterxml.jackson.core    jackson-core    2.17.2    The Apache Software License, Version 2.0
    com.fasterxml.jackson.core    jackson-databind    2.17.2    The Apache Software License, Version 2.0
        com.fasterxml.jackson.core    jackson-annotations    2.17.2    The Apache Software License, Version 2.0
        com.fasterxml.jackson.core    jackson-core    2.17.2    The Apache Software License, Version 2.0
    com.typesafe.akka    akka-stream_2.13    2.10.0    BUSL-1.1
        com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
            com.typesafe    config    1.4.3    Apache-2.0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
        com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
        org.reactivestreams    reactive-streams    1.0.4    MIT-0
        org.scala-lang    scala-library    2.13.15    Apache-2.0
    org.scala-lang    scala-library    2.13.15    Apache-2.0
com.typesafe.akka    akka-persistence-query_2.13    2.10.0    BUSL-1.1
    com.typesafe.akka    akka-persistence_2.13    2.10.0    BUSL-1.1
        com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
            com.typesafe    config    1.4.3    Apache-2.0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
        com.typesafe.akka    akka-stream_2.13    2.10.0    BUSL-1.1
            com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
                com.typesafe    config    1.4.3    Apache-2.0
                org.scala-lang    scala-library    2.13.15    Apache-2.0
            com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
            org.reactivestreams    reactive-streams    1.0.4    MIT-0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
        org.scala-lang    scala-library    2.13.15    Apache-2.0
    com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
    com.typesafe.akka    akka-stream_2.13    2.10.0    BUSL-1.1
        com.typesafe.akka    akka-actor_2.13    2.10.0    BUSL-1.1
            com.typesafe    config    1.4.3    Apache-2.0
            org.scala-lang    scala-library    2.13.15    Apache-2.0
        com.typesafe.akka    akka-protobuf-v3_2.13    2.10.0    BUSL-1.1
        org.reactivestreams    reactive-streams    1.0.4    MIT-0
        org.scala-lang    scala-library    2.13.15    Apache-2.0
    org.scala-lang    scala-library    2.13.15    Apache-2.0
org.scala-lang    scala-library    2.13.15    Apache-2.0

at-least-once

The offset is stored after the envelope has been processed and giving at-least-once processing semantics. This means that if the projection is restarted from a previously stored offset some elements may be processed more than once. Therefore, the Handler code must be idempotent.

Scala
sourceimport akka.projection.ProjectionId
import akka.projection.cassandra.scaladsl.CassandraProjection

val projection =
  CassandraProjection
    .atLeastOnce(
      projectionId = ProjectionId("shopping-carts", "carts-1"),
      sourceProvider,
      handler = () => new ShoppingCartHandler)
    .withSaveOffset(afterEnvelopes = 100, afterDuration = 500.millis)
Java
sourceimport akka.projection.cassandra.javadsl.CassandraProjection;
import akka.projection.Projection;
import akka.projection.ProjectionId;

int saveOffsetAfterEnvelopes = 100;
Duration saveOffsetAfterDuration = Duration.ofMillis(500);

Projection<EventEnvelope<ShoppingCart.Event>> projection =
    CassandraProjection.atLeastOnce(
            ProjectionId.of("shopping-carts", "carts-1"),
            sourceProvider,
            () -> new ShoppingCartHandler())
        .withSaveOffset(saveOffsetAfterEnvelopes, saveOffsetAfterDuration);

The offset is stored after a time window, or limited by a number of envelopes, whatever happens first. This window can be defined with withSaveOffset of the returned AtLeastOnceProjection. The default settings for the window is defined in configuration section akka.projection.at-least-once. There is a performance benefit of not storing the offset too often, but the drawback is that there can be more duplicates when the projection that will be processed again when the projection is restarted.

The ShoppingCartHandler is shown below.

at-most-once

The offset for each envelope is stored before the envelope has been processed and giving at-most-once processing semantics. This means that if the projection is restarted from previously stored offset one envelope may not have been processed.

Scala
sourceimport akka.projection.ProjectionId
import akka.projection.cassandra.scaladsl.CassandraProjection

val projection =
  CassandraProjection.atMostOnce(
    projectionId = ProjectionId("shopping-carts", "carts-1"),
    sourceProvider,
    handler = () => new ShoppingCartHandler)
Java
sourceProjection<EventEnvelope<ShoppingCart.Event>> projection =
    CassandraProjection.atMostOnce(
        ProjectionId.of("shopping-carts", "carts-1"), sourceProvider, ShoppingCartHandler::new);

Since the offset must be stored for each envelope this is slower than at-least-once, which can batch offsets before storing.

The ShoppingCartHandler is shown below.

groupedWithin

The envelopes can be grouped before processing, which can be useful for batch updates.

Scala
sourceval projection =
  CassandraProjection
    .groupedWithin(
      projectionId = ProjectionId("shopping-carts", "carts-1"),
      sourceProvider,
      handler = () => new GroupedShoppingCartHandler)
    .withGroup(groupAfterEnvelopes = 20, groupAfterDuration = 500.millis)
Java
sourceint groupAfterEnvelopes = 20;
Duration groupAfterDuration = Duration.ofMillis(500);

Projection<EventEnvelope<ShoppingCart.Event>> projection =
    CassandraProjection.groupedWithin(
            ProjectionId.of("shopping-carts", "carts-1"),
            sourceProvider,
            GroupedShoppingCartHandler::new)
        .withGroup(groupAfterEnvelopes, groupAfterDuration);

The envelopes are grouped within a time window, or limited by a number of envelopes, whatever happens first. This window can be defined with withGroup of the returned GroupedProjection. The default settings for the window is defined in configuration section akka.projection.grouped.

When using groupedWithin the handler is a Handler[immutable.Seq[EventEnvelope[ShoppingCart.Event]]]Handler<List<EventEnvelope<ShoppingCart.Event>>>. The GroupedShoppingCartHandler is shown below.

It stores the offset in Cassandra immediately after the handler has processed the envelopes, but that is still with at-least-once processing semantics. This means that if the projection is restarted from previously stored offset the previous group of envelopes may be processed more than once.

Handler

It’s in the HandlerHandler that you implement the processing of each envelope. It’s essentially a function from Envelope to Future[Done]CompletionStage<Done>. This means that the envelope handler can integrate with anything, such as publishing to a message broker, or updating a read model in Cassandra.

A handler that is consuming ShoppingCart.Event from eventsByTag can look like this:

Scala
sourceimport scala.concurrent.duration._
import scala.concurrent.Future

import akka.Done
import akka.projection.scaladsl.Handler
import org.slf4j.LoggerFactory

class ShoppingCartHandler extends Handler[EventEnvelope[ShoppingCart.Event]] {
  private val logger = LoggerFactory.getLogger(getClass)

  override def process(envelope: EventEnvelope[ShoppingCart.Event]): Future[Done] = {
    envelope.event match {
      case ShoppingCart.CheckedOut(cartId, time) =>
        logger.info("Shopping cart {} was checked out at {}", cartId, time)
        Future.successful(Done)

      case otherEvent =>
        logger.debug("Shopping cart {} changed by {}", otherEvent.cartId, otherEvent)
        Future.successful(Done)
    }
  }
}
Java
sourceimport akka.projection.javadsl.Handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

public class ShoppingCartHandler extends Handler<EventEnvelope<ShoppingCart.Event>> {
  private Logger logger = LoggerFactory.getLogger(getClass());

  @Override
  public CompletionStage<Done> process(EventEnvelope<ShoppingCart.Event> envelope) {
    ShoppingCart.Event event = envelope.event();
    if (event instanceof ShoppingCart.CheckedOut) {
      ShoppingCart.CheckedOut checkedOut = (ShoppingCart.CheckedOut) event;
      logger.info(
          "Shopping cart {} was checked out at {}", checkedOut.cartId, checkedOut.eventTime);
      return CompletableFuture.completedFuture(Done.getInstance());
    } else {
      logger.debug("Shopping cart {} changed by {}", event.getCartId(), event);
      return CompletableFuture.completedFuture(Done.getInstance());
    }
  }
}
Hint

Such simple handlers can also be defined as plain functionslambdas via the helper Handler.applyHandler.fromFunction factory method.

Grouped handler

When using CassandraProjection.groupedWithin the handler is processing a SeqList of envelopes.

Scala
sourceimport scala.collection.immutable

class GroupedShoppingCartHandler extends Handler[immutable.Seq[EventEnvelope[ShoppingCart.Event]]] {
  private val logger = LoggerFactory.getLogger(getClass)

  override def process(envelopes: immutable.Seq[EventEnvelope[ShoppingCart.Event]]): Future[Done] = {
    envelopes.map(_.event).foreach {
      case ShoppingCart.CheckedOut(cartId, time) =>
        logger.info("Shopping cart {} was checked out at {}", cartId, time)

      case otherEvent =>
        logger.debug("Shopping cart {} changed by {}", otherEvent.cartId, otherEvent)
    }
    Future.successful(Done)
  }
}
Java
sourcepublic class GroupedShoppingCartHandler extends Handler<List<EventEnvelope<ShoppingCart.Event>>> {
  private Logger logger = LoggerFactory.getLogger(getClass());

  @Override
  public CompletionStage<Done> process(List<EventEnvelope<ShoppingCart.Event>> envelopes) {
    envelopes.forEach(
        env -> {
          ShoppingCart.Event event = env.event();
          if (event instanceof ShoppingCart.CheckedOut) {
            ShoppingCart.CheckedOut checkedOut = (ShoppingCart.CheckedOut) event;
            logger.info(
                "Shopping cart {} was checked out at {}",
                checkedOut.cartId,
                checkedOut.eventTime);
          } else {
            logger.debug("Shopping cart {} changed by {}", event.getCartId(), event);
          }
        });
    return CompletableFuture.completedFuture(Done.getInstance());
  }
}

Stateful handler

The Handler can be stateful, with variables and mutable data structures. It is invoked by the Projection machinery one envelope at a time and visibility guarantees between the invocations are handled automatically, i.e. no volatile or other concurrency primitives are needed for managing the state.

The returned Future[Done]CompletionStage<Done> is to be completed when the processing of the envelope has finished. The handler will not be invoked with the next envelope until after the returned Future[Done]CompletionStage<Done> has been completed.

Scala
sourceclass WordCountHandler extends Handler[WordEnvelope] {
  private val logger = LoggerFactory.getLogger(getClass)
  private var state: Map[Word, Count] = Map.empty

  override def process(envelope: WordEnvelope): Future[Done] = {
    val word = envelope.word
    val newCount = state.getOrElse(word, 0) + 1
    logger.info("Word count for {} is {}", word, newCount)
    state = state.updated(word, newCount)
    Future.successful(Done)
  }
}
Java
sourcepublic class WordCountHandler extends Handler<WordEnvelope> {
  private final Logger logger = LoggerFactory.getLogger(getClass());
  private final Map<String, Integer> state = new HashMap<>();

  @Override
  public CompletionStage<Done> process(WordEnvelope envelope) {
    String word = envelope.word;
    int newCount = state.getOrDefault(word, 0) + 1;
    logger.info("Word count for {} is {}", word, newCount);
    state.put(word, newCount);
    return CompletableFuture.completedFuture(Done.getInstance());
  }
}
Note

It is important that the Handler instance is not shared between several Projection instances, because then it would be invoked concurrently, which is not how it is intended to be used. Each Projection instance should use a new Handler instance. This is the reason why the handler parameter is a factory (() =>)(Supplier) of the handler. A new handler instance is also created when the projection is restarted.

However, the state must typically be loaded and updated by asynchronous operations and then it can be error prone to manage the state in variables of the Handler. For that purpose a StatefulHandlerStatefulHandler is provided.

Let us look at how a StatefulHandler can be implemented in the context of a “word count” domain. The purpose is to process a stream of words and for each word keep track of how many times it has occurred.

Given an envelope and SourceProvider for this example:

Scala
sourcetype Word = String
type Count = Int

final case class WordEnvelope(offset: Long, word: Word)

class WordSource(implicit ec: ExecutionContext) extends SourceProvider[Long, WordEnvelope] {

  private val src = Source(
    List(WordEnvelope(1L, "abc"), WordEnvelope(2L, "def"), WordEnvelope(3L, "ghi"), WordEnvelope(4L, "abc")))

  override def source(offset: () => Future[Option[Long]]): Future[Source[WordEnvelope, NotUsed]] = {
    offset().map {
      case Some(o) => src.dropWhile(_.offset <= o)
      case _       => src
    }
  }

  override def extractOffset(env: WordEnvelope): Long = env.offset

  override def extractCreationTime(env: WordEnvelope): Long = 0L
}
Java
sourcepublic class WordEnvelope {
  public final Long offset;
  public final String word;

  public WordEnvelope(Long offset, String word) {
    this.offset = offset;
    this.word = word;
  }
}

class WordSource extends SourceProvider<Long, WordEnvelope> {

  private final Source<WordEnvelope, NotUsed> src =
      Source.from(
          Arrays.asList(
              new WordEnvelope(1L, "abc"),
              new WordEnvelope(2L, "def"),
              new WordEnvelope(3L, "ghi"),
              new WordEnvelope(4L, "abc")));

  @Override
  public CompletionStage<Source<WordEnvelope, NotUsed>> source(
      Supplier<CompletionStage<Optional<Long>>> offset) {
    return offset
        .get()
        .thenApply(
            o -> {
              if (o.isPresent()) return src.dropWhile(envelope -> envelope.offset <= o.get());
              else return src;
            });
  }

  @Override
  public Long extractOffset(WordEnvelope envelope) {
    return envelope.offset;
  }

  @Override
  public long extractCreationTime(WordEnvelope envelope) {
    return 0L;
  }
}

and a repository for the interaction with the database:

Scala
sourcetrait WordCountRepository {
  def load(id: String, word: Word): Future[Count]
  def loadAll(id: String): Future[Map[Word, Count]]
  def save(id: String, word: Word, count: Count): Future[Done]
}
Java
sourcepublic interface WordCountRepository {
  CompletionStage<Integer> load(String id, String word);

  CompletionStage<Map<String, Integer>> loadAll(String id);

  CompletionStage<Done> save(String id, String word, int count);
}

The Projection can be definined as:

Scala
sourceval projection =
  CassandraProjection
    .atLeastOnce[Long, WordEnvelope](
      projectionId,
      sourceProvider = new WordSource,
      handler = () => new WordCountHandler(projectionId, repository))
Java
sourceProjection<WordEnvelope> projection =
    CassandraProjection.atLeastOnce(
        projectionId, new WordSource(), () -> new WordCountHandler(projectionId, repository));

The handler can be implemented as follows.

A naive approach would be to have one row per word for storing the current count in the database. The handler could be implemented as a completely stateless handler that for each processed envelope loads the current count from the database, increment the count by 1 and saved it again. Typically there will be several instances of the Projection with different ProjectionId.id. Each Projection instance would be responsible for processing a subset of all words. This stateless approach wouldn’t be very efficient and you would have to use optimistic database locking to make sure that one Projection instance is not overwriting the stored value from another instance without reading the right value first.

Better would be that each Projection instance is a single-writer so that it can keep the current word count in memory and only load it on startup or on demand.

A handler that is loading the state from the database when it’s starting up:

Scala
sourceimport akka.projection.scaladsl.StatefulHandler

class WordCountHandler(projectionId: ProjectionId, repository: WordCountRepository)(implicit ec: ExecutionContext)
    extends StatefulHandler[Map[Word, Count], WordEnvelope] {

  override def initialState(): Future[Map[Word, Count]] = repository.loadAll(projectionId.id)

  override def process(state: Map[Word, Count], envelope: WordEnvelope): Future[Map[Word, Count]] = {
    val word = envelope.word
    val newCount = state.getOrElse(word, 0) + 1
    val newState = for {
      _ <- repository.save(projectionId.id, word, newCount)
    } yield state.updated(word, newCount)

    newState
  }
}
Java
sourceimport akka.actor.typed.ActorSystem;
import akka.actor.typed.SupervisorStrategy;
import akka.actor.typed.javadsl.AskPattern;
import akka.actor.typed.javadsl.StashBuffer;
import akka.projection.cassandra.CassandraProjectionTest;
import akka.projection.javadsl.ActorHandler;
import akka.projection.javadsl.StatefulHandler;

public class WordCountHandler extends StatefulHandler<Map<String, Integer>, WordEnvelope> {
  private final ProjectionId projectionId;
  private final WordCountRepository repository;

  public WordCountHandler(ProjectionId projectionId, WordCountRepository repository) {
    this.projectionId = projectionId;
    this.repository = repository;
  }

  @Override
  public CompletionStage<Map<String, Integer>> initialState() {
    return repository.loadAll(projectionId.id());
  }

  @Override
  public CompletionStage<Map<String, Integer>> process(
      Map<String, Integer> state, WordEnvelope envelope) {
    String word = envelope.word;
    int newCount = state.getOrDefault(word, 0) + 1;
    CompletionStage<Map<String, Integer>> newState =
        repository
            .save(projectionId.id(), word, newCount)
            .thenApply(
                done -> {
                  state.put(word, newCount);
                  return state;
                });

    return newState;
  }
}

The StatefulHandler has two methods that needs to be implemented.

  • initialState - Invoked to load the initial state when the projection is started or if previous process failed.
  • process(state, envelope) - Invoked for each Envelope, one at a time. The state parameter is the completed value of the previously returned Future[State]CompletionStage<State> or the initialState.

If the previously returned Future[State]CompletionStage<State> failed it will call initialState again and use that value.

Another implementation would be a handler that is loading the current count for a word on demand, and thereafter caches it in the in-memory state:

Scala
sourceimport akka.projection.scaladsl.StatefulHandler

class WordCountHandler(projectionId: ProjectionId, repository: WordCountRepository)(implicit ec: ExecutionContext)
    extends StatefulHandler[Map[Word, Count], WordEnvelope] {

  override def initialState(): Future[Map[Word, Count]] =
    Future.successful(Map.empty)

  override def process(state: Map[Word, Count], envelope: WordEnvelope): Future[Map[Word, Count]] = {
    val word = envelope.word

    val currentCount =
      state.get(word) match {
        case None =>
          repository.load(projectionId.id, word)
        case Some(count) =>
          Future.successful(count)
      }

    val newState = for {
      c <- currentCount
      newCount = c + 1
      _ <- repository.save(projectionId.id, word, newCount)
    } yield state.updated(word, newCount)

    newState
  }

}
Java
sourceimport akka.actor.typed.ActorSystem;
import akka.actor.typed.SupervisorStrategy;
import akka.actor.typed.javadsl.AskPattern;
import akka.actor.typed.javadsl.StashBuffer;
import akka.projection.cassandra.CassandraProjectionTest;
import akka.projection.javadsl.ActorHandler;
import akka.projection.javadsl.StatefulHandler;

public class WordCountHandler extends StatefulHandler<Map<String, Integer>, WordEnvelope> {
  private final ProjectionId projectionId;
  private final WordCountRepository repository;

  public WordCountHandler(ProjectionId projectionId, WordCountRepository repository) {
    this.projectionId = projectionId;
    this.repository = repository;
  }

  @Override
  public CompletionStage<Map<String, Integer>> initialState() {
    return CompletableFuture.completedFuture(new HashMap<>());
  }

  @Override
  public CompletionStage<Map<String, Integer>> process(
      Map<String, Integer> state, WordEnvelope envelope) {
    String word = envelope.word;

    CompletionStage<Integer> currentCount;
    if (state.containsKey(word))
      currentCount = CompletableFuture.completedFuture(state.get(word));
    else currentCount = repository.load(projectionId.id(), word);

    CompletionStage<Map<String, Integer>> newState =
        currentCount.thenCompose(
            n -> {
              return repository
                  .save(projectionId.id(), word, n + 1)
                  .thenApply(
                      done -> {
                        state.put(word, n + 1);
                        return state;
                      });
            });

    return newState;
  }
}

Actor handler

A good alternative for advanced state management is to implement the handler as an actor, which is described in Processing with Actor.

Flow handler

An Akka Streams FlowWithContext can be used instead of a handler for processing the envelopes, which is described in Processing with Akka Streams.

Handler lifecycle

You can override the start and stop methods of the HandlerHandler to implement initialization before first envelope is processed and resource cleanup when the projection is stopped. Those methods are also called when the Projection is restarted after failure.

See also error handling.

Schema

The database schema for the offset storage table.

Note

The partition field is used to distribute projection rows across cassandra nodes while also allowing us to query all rows for a projection name. For most offset types we return only one row that matches the provided projection key, but the MergeableOffsetMergeableOffset requires all rows.

CREATE TABLE IF NOT EXISTS akka_projection.offset_store (
  projection_name text,
  partition int,
  projection_key text,
  offset text,
  manifest text,
  last_updated timestamp,
  PRIMARY KEY ((projection_name, partition), projection_key));

CREATE TABLE IF NOT EXISTS akka_projection.projection_management (
  projection_name text,
  partition int,
  projection_key text,
  paused boolean,
  last_updated timestamp,
  PRIMARY KEY ((projection_name, partition), projection_key));

Offset types

The supported offset types of the CassandraProjection are:

  • akka.persistence.query.Offset types from events from Akka Persistence
  • String
  • IntInteger
  • Long
  • Any other type that has a configured Akka Serializer is stored with base64 encoding of the serialized bytes.
Note

The MergeableOffsetMergeableOffset that is used for messages from Kafka is not implemented for the CassandraProjection yet, see issue #97.

The schema can be created using the method CassandraProjection.createTablesIfNotExists. This is particularly useful when writting tests. For production enviornments, we recommend creating the schema before deploying the application.

Configuration

Make your edits/overrides in your application.conf.

The reference configuration file with the default values:

sourceakka.projection.cassandra {

  session-config-path = "akka.projection.cassandra.session-config"

  session-config {
    # The implementation of `akka.stream.alpakka.cassandra.CqlSessionProvider`
    # used for creating the `CqlSession`.
    # It may optionally have a constructor with an `ClassicActorSystemProvider` and `Config` parameters.
    session-provider = "akka.stream.alpakka.cassandra.DefaultSessionProvider"

    # Configure Akka Discovery by setting a service name
    service-discovery {
      name = ""
      lookup-timeout = 1 s
    }

    # The ExecutionContext to use for the session tasks and future composition.
    session-dispatcher = "akka.actor.default-dispatcher"

    # Full config path to the Datastax Java driver's configuration section.
    # When connecting to more than one Cassandra cluster different session configuration can be
    # defined with this property.
    # and https://docs.datastax.com/en/developer/java-driver/latest/manual/core/configuration/reference/
    datastax-java-driver-config = "datastax-java-driver"
  }

  offset-store {
    keyspace = "akka_projection"
    # the database table name for the offset store
    table = "offset_store"
    # the database table name for the projection manangement data
    management-table = "projection_management"
  }
}

Cassandra driver configuration

All Cassandra driver settings are via its standard profile mechanism.

One important setting is to configure the database driver to retry the initial connection:

datastax-java-driver.advanced.reconnect-on-init = true

It is not enabled automatically as it is in the driver’s reference.conf and is not overridable in a profile.

It is possible to share the same Cassandra session as Akka Persistence Cassandra by setting the session-config-path:

akka.projection.cassandra {
  session-config-path = "akka.persistence.cassandra"
}

or share the same Cassandra session as Alpakka Cassandra:

akka.projection.cassandra {
  session-config-path = "alpakka.cassandra"
}

Cassandra driver overrides

source# See reference configuration at
# https://docs.datastax.com/en/developer/java-driver/latest/manual/core/configuration/reference/
# (check which exact version Akka Projections uses)
datastax-java-driver {

  # always set this to allow reconnection on startup if cassandra is down
  # not overridiable profile so this plugin can't override it for you
  # advanced.reconnect-on-init = true

  profiles {
    akka-projection-cassandra-profile {
      basic.request {
        consistency = QUORUM
        # the offset store does not use any counters or collections
        default-idempotence = true
      }
    }
  }
}

Contact points configuration

The Cassandra server contact points can be defined with the Cassandra driver configuration

datastax-java-driver {
  basic.contact-points = ["127.0.0.1:9042"]
  basic.load-balancing-policy.local-datacenter = "datacenter1"
}

Alternatively, Akka Discovery can be used for finding the Cassandra server contact points as described in the Alpakka Cassandra documentation.

Without any configuration it will use localhost:9042 as default.

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.