Scala Actors - Concurrency

Actors in Abstract:

  • An actors is an object that receives a message and takes action on those messages.
  • The order is which the message arrives is unimportant to an Actor, though some Actor implementations(such as Scala's) queue messages in order.
  • An actor might handle the message internally or it might send a message to another Actor or it might create another Actor to take action based on the message.

Akka

- simple and high-level abstractions of concurrency and parallelism.
- Asynchornous and non-blocking and highly performant event-driven programming.
- Very light-weight event driven processes(1 million actors per GB of head memory).

Fault tolerance:

  • Supervisor hierarchies with let-it-crash semantics.
  • Supervisor hierarchies can span over multiple JVMs to provide truly fauly-tolerant systems.
  • Excellent for writing highly fault-tolerant systems and self-heal and never stop.

Features:

  • Akka provides scalable real-time processing,
    • Scale up(concurrency)
    • Scale out(Remoting)
    • Fault tolerance

Akka modules:

- akka-actors - classic actors, typed actors, IO actors etc.
- akka-agent - Agents, integrated with scala STM
- akka-camel - apache camel integration
- akka-cluster - Cluster membership management, elastic routers.
- akka-kernel - Akka microkernel for running a bare-bones mini application server.
- akka-osgi - base bundel
- akka-osgi-aries
- akka-remote
- akka-slf4j
- akka-testkit 
- akka-zeromq

Do not use -optimize Scala compiler flag

Concepts:

Concurrency vs Parallelism:

Asynchronous vs Synchronous:

Non-blocking vs Blocking:

Deadlock vs Starvation vs Live-lock:

  • Deadlock occurs when several participants are waiting on each other to reach a specific state to be able to progress.As none of them can progress without some other participant to reach a certain state all affected subsystems stall.(catch-22 problem).
  • In contrast to this is the Starvation where one or more participants can progress but there might be one or more that cannot. eg: Say if an algorithm always takes high-priority tasks over the low-priority ones. If the incoming high-priority tasks are higher then no-low priority ones will be finished.
  • Live-lock is similar to deadlock, difference being instead of frozen in a state of waiting for others to progress the participants continuosly change their state. Eg: When 2 participants have 2 identical resources available. They each try to get the resource but they also check if the other needs the resource too. If the resource is requested by others it tries to get the other instance of the resource. Sometimes they bounce between the 2 resources never acquiting it but always yielding to the other.

Race condition:

Race condition occurs when an assumption about the ordering of a set of events might be violated by external non-deterministic effects. Arise often when multiple threads have a shared mutable state though its not the only condition. Eg: If 2 packets are send across a network and if the server receive the p2 before p1. If the message contains no information about the sending order then the server cannot determine the order.

The only gurantee that Akka provides about the messages sent between a given pair of actors is that their order is always preserved.

Non-blocking gurantees:

Wait-freedom:

  • A method is called wait-free if its guranteed to finish in finite no of steps. Never blocking, so deadlock cannot happen.

Lock-freedom:

Obstruction-freedom: