Akka: A modern need

Reading Time: 3 minutes

There has been some time since the Akka has been in news and there is some reason for being so. But before moving to those key points, let’s revise/understand what Akka actually is?

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 like:

  • High performance – Akka delivers up to 50 million messages 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 Applications, such as cluster, load balancing, partitioning, and sharding.

Why Adopting Akka Matters?

Akka can be used to write an 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, the 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 development 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’s thread won’t block to wait for a return value when it sends a message to another actor. The receiving actor will respond 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 decides to stop or restart the child actors, Don’t worry about synchronization in a multi-thread environment, and all the messages processed sequentially.

Basic 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 you don’t need it anymore to prevent memory leaks.
  • Messages used by actors should always be immutable.

Primary Use Cases of Akka

  • Parallel execution of CPU intensive operations that need to be low latency (fork-join)
  • Thread-safe access to stateful components
  • Configure parallelism and handle timeouts for calls to 3rd parties (“traffic shaping” and throttling)
  • Circuit breaker pattern / high watermark for managing traffic spikes (if our data back-end gets overloaded, we can continue serving without it, although in a degraded mode)
  • None-durable fire-and-forget async operations (very convenient)

 

List of famous tools provided by Akka and their Use cases:

  • Akka Actors: A model for concurrency and distribution without all the pain of threading primitives.
  • Akka Persistence: Eventsourcing for actors allows them to reach the same state after restarts.
  • Akka Streams: An intuitive and safe way to do asynchronous, non-blocking backpressured stream processing.
  • Akka HTTP: Modern, fast, asynchronous, streaming-first HTTP server, and client.
  • Akka Clustering: Achieve resilience and elasticity by distributing your system over multiple nodes.
  • Akka Sharding: Distribute your actors across the cluster based on their identity.

References:


Knoldus-blog-footer-image

Written by 

Ayush is a Software Consultant having more than 11 months of experience. He has knowledge of various programming languages like C, C++, Java, Scala, JavaScript and is currently working on Big Data Technologies like Spark, Kafka, ElasticSearch. He is always eager to learn new and advance concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies includes playing Cricket, Travelling, Watching Movies