Does sharing of immutable dataset among actors create duplicate dataset underneath?

36 views Asked by At

I have a case where each Akka cluster node (JVM) has one single actor responsible for creating and maintaining (READ/WRITE) a map of some huge objects. Other actors on the same node need to share (READ) the map (the map will be passed into some third-party library, so no way to change the map access by Akka messages).

case class ShareMap(m: Map[String, HugeObject])

class MapOwner extends Actor {
  var m: immutable.Map[String, HugeObject] = ???
  override def receive: Receive = {
    case UpdateMap(delta) =>
      val m2 = m ++ delta
      this.m = m2
    case GetMap =>
      sender ! ShareMap(m)
  }
}

I want to share the immutable map by sending the map reference to actors within the same JVM.

My question is: for my case (within the same JVM), will it be ensured that no duplicate map object will be created? I have some concerns about the memory usage and performance.

1

There are 1 answers

0
bottaio On BEST ANSWER

According to the docs

The messages that Akka actors send to each other are JVM objects (e.g. instances of Scala case classes). Message passing between actors that live on the same JVM is straightforward. It is done via reference passing. However, messages that have to escape the JVM to reach an actor running on a different host have to undergo some form of serialization (i.e. the objects have to be converted to and from byte arrays).

This should be just a reference.