Ask vs Tell or forward for Actors using Akka Streems


(Aditya Sharma) #1

Hi I am working with akka streams along with akka-stream-kafka. I am setting up a Stream with the below setup:

Source (Kafka) --> | Akka Actor Flow | --> Sink (MongoDB)

Actor Flow basically by Actors that will process data, below is the hierarchy:

                                     Master Actor  
                                      /       \
                          URLTypeHandler     SerializedTypeHandler
                             /       \                   |
                     Type1Handler   Type2Handler     SomeOtherHandler

So Kafka has the message, I write up the consumer and run it in atMostOnceSource configuration and use

Consumer.Control control =
            Consumer.atMostOnceSource(consumerSettings, Subscriptions.topics(TOPIC))
                    .mapAsyncUnordered(10, record -> processAccessLog(rootHandler, record.value()))
                    .to(Sink.foreach(it -> System.out.println("FinalReturnedString--> " + it)))

I’ve used a print as a sink initially, just to get the flow running.

and the processAccessLog is defined as:

private static CompletionStage<String> processAccessLog(ActorRef handler, byte[] value) {

    handler.tell(value, ActorRef.noSender());

    return CompletableFuture.completedFuture("");

Now, from the definition ask must be used when an actor is expecting a response, makes sense in this case since I want to return values to be written in the sink.

But everyone (including docs), mention to avoid ask and rather use tell and forward, an amazing blog is written on it Don’t Ask, Tell.

In the blog he mentions, in case of nested actors, use tell for the first message and then use forward for the message to reach the destination and then after processing directly send the message back to the root actor.

Now here is the problem, how do I send the message from D back to A, such that I can still use the sink.
I also have a follow-up question here, is it good practice to have open ended streams? e.g. Streams where Sink doesn’t matter because the actors have already done the job. (I don’t think it is recommend to do so, seems flawed).