How to implement an actor topology in AKKA? - scala

I need to implement various kind of topologies in AKKA actor model system
like Line,mesh etc. Are there in built libraries for various kind of topologies
in AKKA? If not how can I build such topologies? Also, how do we connect two or
more actors in AKKA because that's the basic for creating any topology?
Thanks

I don't think that topologies make any sense in Akka.
As you have noticed, the basic tool for creating topologies is a connection between two entities. But such connection cannot exist in Akka, because any actor can send a message to any actor. This is the foundation of actor model, after all.
Of course, you can try and emulate some topology manually, but I cannot see any use for it.

Related

Akka persistent query reader as competing consumer

I'm dabbling around with Akka and persistence query. Below design shows what I'm doing here.
When I run multiple instances of the "persistence reader actor", all the actors (#6 below) receive the same message. Ideally, I would want one of the actors to receive the message vs all. Is it possible to implement "competing consumers" so that I can avoid the same message being processed by multiple persistence reader actors?
I did go thru akka "smallest-mailbox-pool" to implement competing consumers but wanted to make sure that there is nothing inbuilt in persistence reader plugins before handling competing consumers with my own code.

what the essential difference between akka and ThreadPool+BlockingQueue in ONE Process?

We know Akka is one implementation of actor pattern. Without Akka, I usually implement a simple actor pattern using ThreadPool+BlockingQueue. So the message is offered into the queue, and the works(actors) take the message from the Queue, then do what they should do. Of course, this kind of implementation can be only in just ONE process.
So as to in one process,
What's the essential difference between these two(Akka vs.
ThreadPool+BlockingQueue)
Moreover, what's the difference between actor pattern and producer-consumer model?
Actor model is indeed quite similar to producer-consumer model (P-C).
However, if you use a blocking queue with P-C your application won't be completely non-blocking and asynchronous. The promise of actor model and Akka is that all messages are sent asynchronously and don't block the sender.
Another aspect of it is managing these queues gets quite cumbersome once you have many consumers and producers. With actors you simply send a message and don't have to think about these low level details. Under the hood Akka will keep a message queue aka mailbox per actor with a dispatcher assigning actors to the thread pool to process those messages.
It's much easier to use Akka to achieve highly performant and resilient application than coding it yourself. You get fault tolerance, resource management, location transparency, routing, distributed, async processing, hierarchical supervision out of the box. Not to mention other frameworks and libraries leveraging these features to give you even more (reactive streams, akka http, etc). There are lot's of patterns developed for you already there, so why bother with your own.

Streaming data between akka cluster nodes

I need to stream data between a couple hundred KB and many MBs between akka cluster nodes. Simplest approach would be to split it up as chunked messages, but that appears to be in advisable because it might interfere with housekeeping chatter of the cluster.
Alternatively, I could use messages to communicate one time urls and use http.
However, I'd prefer a persistent connection approach, so I was thinking using zeromq and chunked messages.
But rather than rolling my own approach, I'd like to use an existing way of accomplishing this but I have not found one.
One more requirement: most of the time the consumption of that stream is going straight out via Play, so an approach that created an iteratee that could be used to proxy the steam to http would be preferable.
Akka Streams 2.5.12 has StreamRefs for what I believe to be your use case.
Iteratees can't communicate across machine boundaries, so iteratees alone are probably not the tool you are looking for.
I would pursue one of the following approaches:
Using remote rpc akka Actors to send chunks of your data across the wire. Actors can be used to create iteratees and enumerators on either side (Enumerator.unicast and Iteratee.foreach) of the wire so that the fact that you are using Actors is just an implementation detail and not visible in your interface of these streams.
Use Akka Streams. This library has support for TCP connections, and while this is a different streaming library from iteratees, I have found that it is more robust in the stream operations it supports. It looks like Play is looking to move towards a tighter integration with Akka Streams as they are looking at replacing their netty HTTP backend with Akka Http Streams

Importance of Akka Routers

I have this lingering doubt in my mind about the importance of Akka Routers. I have used Akka Routers in the current project I am working on. However, I am a little confused about the importance of it. Out of the two below methods, which is more beneficial.
having routers and routees.
Creating as many actors as needed.
I understood that router will assign the incoming messages among its routees based on the strategy. Also, we can have supervisor strategy based on the router.
I have also understood that actors are also lightweight and it is not an overhead to create as many actors as possible. So, we can create actors for each of the incoming messages and kill it if necessary after the processing si completed.
So I want to understand which one of the above design is better? Or in other words, in which case (1) has advantage over (2) OR vice versa.
Good question. I had similar doubts before I read Akka documentation. Here are the reasons:
Efficiency. From docs:
On the surface routers look like normal actors, but they are actually
implemented differently. Routers are designed to be extremely
efficient at receiving messages and passing them quickly on to
routees.
A normal actor can be used for routing messages, but an actor's
single-threaded processing can become a bottleneck. Routers can
achieve much higher throughput with an optimization to the usual
message-processing pipeline that allows concurrent routing. This is
achieved by embedding routers' routing logic directly in their
ActorRef rather than in the router actor. Messages sent to a router's
ActorRef can be immediately routed to the routee, bypassing the
single-threaded router actor entirely.
The cost to this is, of course, that the internals of routing code are
more complicated than if routers were implemented with normal actors.
Fortunately all of this complexity is invisible to consumers of the
routing API. However, it is something to be aware of when implementing
your own routers.
Default implementation of multiple routing strategies. You can always write your own, but it might get tricky. You have to take into account supervision, recovery, load balancing, remote deployment, etc.
Akka Router patterns will be familiar to Akka users. If you roll-out your custom routing then everyone will have to spend time understanding all corner cases and implications (+ testing? :)).
TL;DR If you don't care about efficiency too much and if it's easier for you to spawn new actors then go for it. Otherwise use Routers.

Akka events between local and remote actors

Using the event bus mechanism between actors in the same ActorSystem is straight-forward, but I was wondering if there was a sanctioned method for doing so between:
Actors in different ActorSystems in the same JVM
Actors in different JVMs (via remoting)
Assuming that I know the paths to the actors is fine, but if there was a commonly used mechanism to discover those kinds of things as well, I'd love to hear about it.
I think in this case you need to look for distributed publish-subscribe on a cluster, supposing you want to subscribe actors to events, without awareness of the location of the actors. This link may prove useful.
This is a note from the official Akka documentation:
The event stream is a local facility, meaning that it will not
distribute events to other nodes in a clustered environment (unless
you subscribe a Remote Actor to the stream explicitly). If you need to
broadcast events in an Akka cluster, without knowing your recipients
explicitly (i.e. obtaining their ActorRefs), you may want to look
into: Distributed Publish Subscribe in Cluster.