Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Actor Based Concurrency

Actor Based Concurrency

Whistle-stop tour of Akka - slides for the #micromanchester microservices conference.

Amy O' Leary

July 09, 2016
Tweet

Other Decks in Technology

Transcript

  1. Actors vs Microsystems Microsystems • Stand-alone components that serve a

    single purpose • May be in themselves multithreaded Actors • Isolated, single-threaded components that encapsulate both state and behavior • Can be used to implement microservice environments Amy O’ Leary [email protected] 5
  2. In concurrent systems, managing state is hard Managing mutable state

    leads to: • Race conditions • Visibility issues • Lack of atomicity [email protected] Amy O’ Leary 6
  3. In an actor system, all internal state is maintained by

    actors • State held by an actor can be thought to be immutable and atomic • In Akka, only one message within an actor is processed at a time, enforcing atomicity [email protected] Amy O’ Leary 7
  4. Adding Akka to your application public class HelloActor extends UntypedActor

    { public void onReceive(Object message) { if (message instanceof HelloMessage) { System.out.println( "My message is: " + ( (HelloMessage)message ).getMessage() ); } } } [email protected] Amy O’ Leary 9
  5. Starting your application with Akka ActorSystem actorSystem = ActorSystem.create( "MySystem"

    ); ActorRef actorRef = actorSystem.actorOf( new Props( HelloActor.class ), "myActor" ); actorRef.tell( new HelloMessage( "Hello, Akka!" ) ); [email protected] Amy O’ Leary 10
  6. Adding routing to your Akka application akka.actor.deployment { /parent/router1 {

    router = round-robin-pool nr-of-instances = 5 } } [email protected] Amy O’ Leary 11
  7. Adding routing to your Akka application ActorRef router1 = getContext()

    .actorOf(FromConfig.getInstance() .props(Props.create(HelloActor.class)), "router1"); [email protected] Amy O’ Leary 12
  8. Fault tolerance Ever tried. Ever failed. No matter. Try Again.

    Fail again. Fail better. Samuel Beckett [email protected] Amy O’ Leary 13
  9. Building a distributed system • More scalable • Easier to

    release small components separately • Single tenancy – easier to trace a fault to a single component • Easier to change resources in a bottleneck component [email protected] Amy O’ Leary 14
  10. Akka Remoting • Start with a single JVM with components

    designed as actors • Scale up by using Akka Remoting • Any number of actors can occupy their own server • Easy to configure – no code changes needed! • Can combine remoting with routing. [email protected] Amy O’ Leary 17
  11. Akka Remoting Add to your application.conffile: akka { actor {

    provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp{ hostname = "127.0.0.1" port = 2552 } } } [email protected] Amy O’ Leary 19
  12. Akka Remoting Configure your application to be able to talk

    to your remote actors akka { actor { deployment { /sampleActor { remote = "akka.tcp://[email protected]:2553" } } } } [email protected] Amy O’ Leary 20
  13. Akka Remoting Use routing to set up a remote actor

    in several locations akka.actor.deployment { /parent/remotePool { router = round-robin-pool nr-of-instances = 10 target.nodes= ["akka.tcp://[email protected]:2552", "akka://[email protected]:2552"] } } [email protected] Amy O’ Leary 21
  14. A warning note • Increasing concurrency can increase complexity •

    Error reporting can be vague – no stack trace • Debugging from one actor to another is cumbersome • Still need to learn about concurrency in Java! [email protected] Amy O’ Leary 22
  15. What did we learn? • The actor model can be

    used to manage state in a concurrent system • Akka is a great framework for easily implementing an actor based system • Akka can be easily scaled up to a distributed system [email protected] Amy O’ Leary 24