What is Akka?
Akka provides a platform to build scalable, resilient and responsive applications in Scala and Java. The Akka framework provides good abstractions for concurrent, asynchronous, and distributed programming, such as Actors, Streams, and Futures. It uses the Actor model to hide all the thread- related code and gives interfaces to implement a scalable and fault-tolerant system easily.
Akka is a truly reactive framework because everything in the sense of sending and receiving a message to Actors, is lock-less, non-blocking IO, and asynchronous.Building application with Akka has several advantages, which are as follows –
- High performance – Akka delivers up to 50 million message per second on commodity hardware having ~2.5 million Actors per GB of RAM.
- Resilient by design – Akka system has self-healing properties for local and remote Actors.
- Distributed and Elastic – Akka has all the mechanisms to scale Application, such as cluster, load balancing, partitioning, and sharding.
How Does Akka Framework Work?
Actor System – Create Actors and initialize actors and configure the desired implementation of each component. The sender initiates the actor creation process, which invokes actorOf() on Actor System and creates a reference of an Actor object, the actor not created /initiated. ActorRef acts as a proxy to the Actor object and provides an interface to communicate with the Actor.
- The Sender Actor uses ! to tell the receiving Actor about the event.
- ActorRef in response dispatches the event on Messages Dispatcher.
- Message Dispatcher enqueue the event to the Message-queue.
- Message Dispatcher also looks for the mailbox. The mailbox holds the message to receive the Actor, finds the MailBox, MessageDispatcher binds a set of Actor to a thread pool and invoke the MailBox.run() method.
Why Adopting Akka Matters?
Akka can be used for write application in the Actor pattern, meaning everything has to be immutable. There are no methods to call on external objects, but an actor used as an object. It’s doesn’t involve locking which gets rid of the deadlock problem. Also, Actor is cheap and not thread bound. Meaning, an Actor can live on whichever Thread it wants and doesn’t waste threads. So if an Actor isn’t handling a message, it doesn’t occupy a thread. On top of that, it is scalable, both over cores, but also multiple machines.
Benefits of Adopting Akka
Concurrent application developing is difficult because of the need to deal with synchronization, locks and shared memory. Akka actors can easily write asynchronous code without the need for lock and synchronization.
Akka actors use message instead of method calls is that the sender thread won’t block to wait for a return value when it sends a message to another actor. The receiving actor will response with the results by sending a reply message to the sender.
Akka actor model handles the error. The actors in a hierarchy, each actor notify its parent of the failure. Then the parent actor decide to stop or restart the child actors, Don’t worry about synchronization in a multi-thread environment and all the messages processed sequentially.
How to Adopt or Use Akka?
Akka actors are lightweight and loosely-coupled by design, thus scale well on a distributed computing environment.
- IoT – The actor model can help to embrace the characteristics of IOT systems and make it easier to represent individual devices and also groups of devices.
- Transaction Processing – The Akka used for Transaction processing where the incoming data is either time series or transactional data. The stream data is either pump in a large amount of data that needs to be processed in parallel and concurrently. Finance, banking, securities, trading, telecoms, social media, and online gaming are some of the domain enterprises that deal with large data coming in from multiple sources.
- Service Provider – Another area is where the application provides services to various other clients via a variety of service means such as SOAP, REST, or Web-sockets. The application generally caters to be processed fast and concurrently.
- Batch Processing – Batch processing used across enterprise domains is another area where Akka actor very well. Dealing with extensive data, applying paradigms such as divide and conquer, mapreduce, master-worker, and grid computing allow large data processed.
- Data Mining/Analytics – Most enterprises generate large amounts of data structured as well as unstructured. An application that mine this data from existing transactional stores or data warehouses can use Akka to process and analyze the massive sets of data.
- Service Gateways/Hubs – Service gateway or hubs connect multiple systems or applications and provide mediation and transformation services. An Akka provides those scale-up and scale-out options along with high availability for applications in this space.
Akka Best Practices
- When performance is a concern use tell() instead of ask().
- Always handle exceptions by sending a Failure message when using ask().
- An actor shouldn’t share any mutable state.
- An actor shouldn’t be declared within another actor.
- Explicitly destroy an actor when don’t need it anymore to prevent memory leaks.
- Messages used by actors should always be immutable.
Akka Performance Monitoring Toolkit
Akka – Persistence
It’s primary purpose is to make Akka Persistence more comfortable to use.
The main components are –
- GeneralAggregate- Which is built on top of PersistenceActor.
- GeneralAggergate- Which is built on top of PersistanceQuery.
It includes feature like –
- Separation of aggregate, view and state(machine).
- View and aggregate both understand the same events and the uses the same state.
- Integrate cluster/sharding-support.
- Simplified AtLeastOnce Delivery-support.
- Automatic starting and stopping aggregates and views.
- Automatic working view that supports getting the current state and the full event history.
How useful was this post?