How to reliably send a message from outside the ActorSystem

I am wondering how to send a message to an actor from outside the actor system reliably?

With actor to actor messages I can set a replyTo/sender field in the message, and use the absence of a reply to work out that I need to retry sending the message (etc).

Since I am sending the message from outside the ActorSystem I don’t have an ActorRef to use for the replyTo field though.

Is there something I’m missing?

I think Akka HTTP will probably help here, but I was hoping to keep things simpler while trying Akka out. I basically have a Spring Boot application that is starting the actor system and I’m wanting to send messages from the spring side of things into Akka without losing messages.

You are right that from the outside you would typically use HTTP or gRPC and use error response codes.

Do you run the Spring Boot application and the Akka ActorSystem in the same JVM? Then you are maybe looking for the ask pattern, see Interaction Patterns • Akka Documentation

1 Like

Thanks for the reply!

Yes it looks like the ask pattern will work.

My next question is how do I stop the root of the actor system becoming a bottle neck (let me know if I should do this in a separate thread) .

Basically when a new request comes in I’m not sure the best way to avoid every request having to go through the root actor/system. I would like to directly put the message on the queue of the right actor, but I need to get an ActorRef for it first.

The best I’ve come up with is to get my ActorSystem to save the list of actors from the Receptionist listener to some shared variable that Spring can also see. Spring can then directly call the right ActorRef. This feels a bit hacky though.

If Akka HTTP somehow resolves this concern then I can potentially switch over.

I don’t think that will be a bottleneck. Such actor should be able to handle millions of messages per second if all it does is to lookup the ActorRef from a local Map and forward the message to the actor.

By the way, you can probably use a Router Routers • Akka Documentation
At startup you would create the GroupRouter and from Spring make a request (ask again) to the root actor to retrieve the ActorRef of the router. Then you can send messages from Spring to the router ActorRef.

If you see that the router becomes a bottleneck, I would be impressed by the rest of your application, you can create a few of these routers and use them with some hashing distribution.