1
votes

I'm learning Akka. Let's say we have several Student actors who ask questions in English, several Teacher actors who answer in Portuguese and just one Translator actor, like in the diagram below.

enter image description here

How can I correlate the questions with their answers?

  • Should I keep a correlation list as a state in Translator actor?
  • Should I add the full path of the Student actor as a field in the message payload, so Translator actor can find the student who asked the question (using actorSelection method of ActorSystem)?
  • Does Akka has some built-in feature that automatically keeps track of the "call stack"?
  • Other way?

PS. In this example, there are only three types of actors, but let's also consider we can have lots of others actors between them.

2

2 Answers

1
votes

Below are a few bullet points (not necessarily in the same order as your questions) that might address your concern:

  • If you want to look up questions asked by a student, it makes sense to maintain a private Map with actorRef or actorPath (*) of the student actor as a key and the corresponding question IDs (e.g. assigned using random UUIDs) as values. Since Map is highly efficient (O(1)) in key lookup, it's not uncommon to maintain even another Map reversing the key and value if lookup by question is needed as well.

  • For scalability, I would keep the Map minimal by removing questions that, for instance, have already been sent to the teachers. Processed questions can be saved in a database (e.g. Redis, Cassandra) for analytical queries, if necessary.

  • Questions sent to the Translator actor will be pattern-matched based on their language/subject for dispatching to the matching teacher actors. Depending on the business requirement, the questions can be, for example, round-robin-ed to a group of qualifying teacher actors via an Akka router.

  • Answers can be sent directly from teachers to students, or via an intermediary actor (could be actor Translator) if additional answer processing work is needed.

  • Akka keep messages sent to an actor in its mailbox and allows operational customization. To maintain a granular account of the Q&A, I would recommend applying your own programmatic logic and store them in a database if necessary.

(*) It boils down to the specific business requirement in choosing between actorRef and actorPath. Main difference is whether reincarnation of an actor could happen. For instance, if you want to treat a new student actor that uses the same actorPath of a terminated student actor a separate student, use actorRef. See this Akka doc for more details.

1
votes

I recommend you a different approach. Both, student and teachers communicates each other directly. If they don't understand the language they can use the translator service to get the correct language message. This way student and teachers doesn't need to know what language use the other one and keep the communication protocol much more simple.