Concurrency and Parallelism In Akka

Reading Time: 3 minutes

After going through the previous blogs, we are now familiar with the basics of Akka. Moving forward let us have a look at concurrency and parallelism in Akka actors.

Concurrency and parallelism are related concepts, but there are small differences.

Message Processing in Akka Actors

First, let us have a look at message processing in Akka actors.

akka

Akka follows single-threaded illusion. That means each actor processes (at most) one message at a time. Then we don’t have any concurrency or parallelism. Let’s take an example-

Suppose if we want to have multiple users all operating to our system in parallel, then a single actor can handle that, as it processes one message at a time. This means that if we want to scale up or we want multiple users to access the system at the same time, then, we need to have multiple actors in parallel.

This is how Akka is structured-

  • Each actor handles single message at a time.
  • If we want multiple users to use the system in parallel then we need multiple actors.

What do we mean by Concurrency and Parallelism?

Now let us understand basically what do we mean by concurrency and parallelism in Akka.

Two or more tasks are considered concurrent if the order in which they get executed in time is not predetermined.

In other words, concurrency introduces non-determinism. This means that when we have multiple tasks, we don’t know the order of their execution. We don’t know which task will execute first or which will be the last task to execute.

Concurrent tasks may or may not get executed in parallel. Hence, concurrency is a more general concept than parallelism.

Concurrent programming is primarily concerned with the complexity that arises due to non-deterministic control flow. So, when we have multiple tasks and we don’t know the order of their execution, then, it gives rise to complexity. And concurrent programming is all about handling this complexity.

Parallel programming, on the other hand, aims at improving the throughput of the system. It also helps in making the control flow deterministic.

So, parallel programming is about saying that when we have concurrent tasks how can we execute those tasks at the same time rather than executing them serially, and, how we can make them deterministic.

Understanding Concurrency and Parallelism via Example

Let us have a look at the below image-

As we can see for concurrent, we have two queues of people for a single coke machine. So, each person is lining up to access this coke machine.

In a concurrent system, the people are not polite. They are not going to wait for each other. Whoever reaches first will get to access the machine and move on. Therefore, this becomes non-deterministic. We cannot tell who will reach the coke machine first.

So, the reality is that there is only one machine. So, only one person at a time gets to access the machine.No multiple people get to access it at same time. Thus, there is no parallelism.

Parallelism, on the other hand, has two queues of people along with two coke machines. In this case, we have two lines each allocated to a separate machine. So, we have determinism and achieve parallelism.

First-line can go to the first machine-independent of the second line and vice versa. We also know the order in which the machine would be accessed by the people in the line. So, we achieve determinism.

Conclusion

  • Concurrency is a property of the program.
  • Parallel execution is a property of the machine.To achieve parallel execution,we need hardware to support it.

That’s all about concurrency and parallelism. Stay tuned for upcoming blogs.