Arpan Patro
Arpan Patro

Reputation: 153

Why is default behavior of Akka Actor to process messages one after the other?

I have read that an akka actor processes messages one after the other. Why is this?

What Im unable to wrap my head around is “Why is synchronized execution of messages the default behavior?”. I do understand that for parallel execution of mailbox messages the functions(jobs to be performed) should have 0 side effects.

Is this default akka behavior because that use cases which have absolutely independent execution and 0 side effects are a MINORITY and we usually work with jobs that need common resource.

If the design goal of scala and functional programming is 0 side effect code for which the below scenario holds true then why is this not default behavior in akka actor message processing.

Upvotes: 2

Views: 523

Answers (2)

Cyäegha
Cyäegha

Reputation: 4251

My understanding is that one key point of the actor model is to make parallel programming simpler to understand (and simpler to "get right"), without hard-to-debug issues such as deadlocks.

This works because:

  • you can easily achieve parallelism by creating multiple actors and splitting the workload among them.
  • The code inside each actor is single threaded, and the actor's state is hidden from other actors, so you don't need to protect the actor's internal state from concurrent access issues.

If the actor could process several messages in parallel, it could concurrently access its own internal state, and you would be back to having to worry about locks, synchronization, ConcurrentModificationExceptions... Which would defeat the point.

Upvotes: 1

slouc
slouc

Reputation: 9698

If you take a look at Akka docs you will find that one of the two guarantees is message ordering per sender-receiver pair. If messages were processed in an indeterministic order this guarantee would be compromised.

Upvotes: 1

Related Questions