Introduction to Akka — The concurrent way of handling things

Mehul
5 min readJan 2, 2024

--

In this blog we will learn about:

  1. Why do we require and what is Akka
  2. How does it work(what are the components of akka)

This will only cover the theory side of why Akka

Topics Covered

  1. Conventional programming drawbacks.
  2. Threads and it’s management
  3. Akka introduction
    Actor Model

Requirements

  1. We are going to use Java terminologies for this, so a basic understanding of Java will be good.
  2. Some concepts around multithreading(thread handling).
  3. Reactive systems(just the basics will do, use this for brief intro)

Let’s start

The conventional way
So most of the things we create as a developer(mostly backend) are APIs, which does some specific tasks and will create some entries in some datastore or will get the data from it. And all this happens in a sequential manner mostly which will take in HTTP request and respond with some response. We can also consider the example to writing a main function which will perform some tasks sequentially and print out the results.

The above approach takes care of 90 percent of the use cases, the above way is easy to build and takes not too much time to learn(may be subjective on what amount of things you want to learn).

Example

Sometimes as a developer we need to perform tasks which takes too much time to run and we consider running it on a new thread or running some steps in parallel(new threads) and cumulate their results to have a final output(multithreading).

Let’s say I have to calculate the number of all primes up to 100,000,000.

So the fist thing that comes to mind is let’s run a loop from 2 to the number given and write a function isPrime()(checks if the number is prime or not) and increase the answer by 1 and then print the output. The approach may work for this number, but that takes too much time and what if we increase one more “0”? Still we want to work with this approach. So we try to get a work around by introducing threads.

In multithreading we may want to divide the task into subtask, let’s say we make 10 new threads and each thread will only work with the 1/10th part. Now our solution will be much more efficient and will take much lesser time than before.

Multi threading way of handling things

But the problem with the above solution is that we have to manage to many things, the result from each thread, we can’t use the single variable without locks for incrementing, using locks for variables, one thread’s data is not used by other, starvation, deadlock etc. All thread related problems needs to be taken care of by us to use that program.

If we chose not to directly use java thread class and use executor service then also we have to handle too many cases which might come to haunt us back, but it might depend on the use case.

And believe me this is a very basic problem which can be solved easily, in real world problems the issues faced will be much more complicated and if there is some issue then debugging it will be very hard.

Introducing AKKA

Akka is a framework for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.

If there is any keyword that you are not familiar with in the above statement please read about it(just google it, otherwise the scope of this will not be limited).

The Actor Model

Terminologies

  1. Name and Path: To uniquely identify the Actor.
  2. Message Queue: When an Actor receives a message from another actor they are kept in the message queue to be processed.
  3. Behaviour: It defines what to do with the message, respond, persist, ignore etc. The behaviour will process one message at a time from the queue.

Why

So why does this model work?

  1. Each Actor will have it’s own thread.
  2. Actors will not share the data with other actors. They will have their own state.
  3. Messages are immutable: So we saw that messages can be sent to other actors and if that data becomes unchangeable by either of the actors(sender and receiver) then automatically the actors become thread safe.
  4. Message are processed one at a time: So in each actor the behaviour will take one message to process and nothing will interrupt it while processing. So there will no need of synchronisation.

Example

Now let’s carry on our previous example, where we were creating 10 threads to calculate the number of primes. We are aware that we need to add all the results given by each thread and that needs some kind of synchronisation.

Now by theory of Actor model we can have sender and receiver actors. So let’s name the main actor who gives the instruction and the result to be Manager and that will make 10 Children(Do not worry about how it will make child actors now). So manager will send message to child with the starting number and the total number to be processed to calculate the number of primes in the range.

Once the child behaviour completes it’s processing then it will send another message to the manager, that I have completed my processing and that message will go into manager’s message queue.

So now messages gets processed one at a time by behaviour we don’t have to worry about if two child behaviours sends messages at the same time, because manager behaviour will only process the message once at a time, which will make it thread safe without any synchronisation.

I know there’s no code for us to see for now, in the next blog we will see the performance of conventional method, what issues we face in code for java thread class. How behaviours are created, it’s internals and their performances.

Conclusion

So whenever you are considering building an reactive thread safe application, mainly high performant do consider using Akka framework. It might fit your use case properly.

Explore more at: https://akka.io/ and https://github.com/akka/akka

Hope you like the article, please comment if there are any suggestions on my writing style or some other things.

--

--

Mehul
Mehul

Written by Mehul

Software Development Engineer- Google

No responses yet