Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM

Uploaded on


  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Helpful to me, thanks
    Are you sure you want to
    Your message goes here
  • Very funny and informative presentation, it shows basics concepts of Akka in Java, but they can be applied to scala programming too.
    Are you sure you want to
    Your message goes here
  • I have a video, but it's in Italian. Sorry.
    Are you sure you want to
    Your message goes here
  • Great presentation, Do you have a video of you presenting this?
    Are you sure you want to
    Your message goes here
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Concurrency, Scalability
    & Fault-tolerance 2.0 with Actors & STM
    by Mario Fusco
    twitter: @mariofusco
  • 2. Which are the biggestchallengesin contemporary software development?
  • 3. Deal with overloadedsystems
  • 4. Do concurrencyeffectively
  • 5. Design, develop and deployapplicationsthat scale well
  • 6. The native Java concurrency model
    Based on:
    They are sometimes plain evil …
    … and sometimes a necessary pain …
    … but always the wrong default
  • 7. Actors: a different model of concurrency
    • Implements Message-Passing Concurrency
    • 8. Share NOTHING
    • 9. Isolated lightweight processes
    • 10. Communicates through messages
    • 11. Asynchronous and non-blocking
    • 12. Each actor has a mailbox (message queue)
    • 13. A single thread can managemany (thousands of) actors
  • People are bad at parallel thinking
    With actors is easier to reason about and easier to avoid:
    Race conditions, Deadlocks, Starvation and Live locks
  • 14. What is Akka?
    • An actor based concurrency framework
    • 15. Written in Scala, butalsoworking in Java
    • 16. Nowrealeasing 1.0 after 2 years of development (currentlyRC4 available)
    • 17. Better performances and throughput and smallermemoryfootprint (650 bytes) than native scala actors
    • 18. Open source (Apache2)
  • Untyped Actors
    public class MyUntypedActorextends UntypedActor {
    public void onReceive(Object message) throws Exception {
    if (message instanceofString) {
    String msg = (String)message;
    System.out.println("Received message: " + msg);
    UntypedActorRef actor = UntypedActor.actorOf(MyUntypedActor.class);
    // --- Do some work here ---
  • 19. Sending message to UntypedActors
    There are 3 different ways to send a message to an actor:
    Fire-and-forget ( ! In Scala )
    Uses a Future under the hood, blocking the sender Actor until eithera reply is received or the Future times out ( !! In Scala )
    Object res1 = actor.sendRequestReply(msg);
    Object res2 = actor.sendRequestReply(msg, 1000); // timeout
    Explicitly returns a Future ( !!! In Scala )
    Future future= actor.sendRequestReplyFuture(msg);
    IMPORTANT: Messages can be any kind of object but have to be immutable.
    Akkacan’t enforce immutability (yet) so this has to be by convention
  • 20. Typed Actors
    public class CounterImplextends TypedActorimplements Counter {
    private int counter = 0;
    public void count() {
    public intgetCountNr() {
    Counter counter= (Counter)TypedActor
    .newInstance(Counter.class, CounterImpl.class, 1000);
    counter.count(); // Fire-and-forget
    intcount = counter.getCountNr(); // uses Future under the hood
  • 21. “Let it crash”
    supervisor hierarchies
  • 22. You can’t avoid failures: shit happens
  • 23. What’swrong in trying to preventerrors?
    Java and other languages and frameworks (not designed with concurrency in mind) signal faults/errors with exceptions
    Throwing an exception in concurrent code will just simply blow up the thread that currently executes the code that threw it:
    There is no way to find out that things went wrong, apart from inspecting the stack trace
    There is nothing you can do to recover from the problem and bring back your system to a normal functioning
  • 24. Fail fast & build self-repairing systems
  • 25. Supervisedactors
    • Actors provide a clean way of getting error notification and do something about it
    • 26. Supervised actors also allow you to create sets (hierarchies) of actors where you can be sure that either all are dead or none are dead
    • 27. That encourages non-defensive programming: don’t try to prevent things from go wrong, because they will. Failures are a natural state in the life-cycle of your app: crash early and let someone else (that sees the whole picture), deal with it
    • 28. A supervisor is responsible for starting, stopping and monitoring its child processes. It can act with 2 fault handling strategies:
    or All-For-One
  • 29. Declarative supervised actors configuration
    Supervisor supervisor = new Supervisor(
    new SupervisorConfig(
    new AllForOneStrategy( // or OneForOneStrategy
    new Class[]{Exception.class}, // Exceptions to handle
    3, // maximum number of restart retries
    5000), // within time in millis
    new Supervise[] {
    new Supervise(
    permanent()), // the actor will always be restarted
    new Supervise(
    temporary()) // the actor won’t be shut down normally
    // You can also link and unlink actors from a supervisor;
  • 30. Remote actors
    • Akka provides remote actors in a completely transparent way both in regards to sending messages, error handling and supervision
    • 31. Uses a very efficient and scalable I/O implementation built upon JBossNetty and Google Protocol Buffers
    • 32. Can be secured with a cookie authentication mechanism
    • 33. Can be bothTyped and Untyped
    • 34. Twotypes of remote actors:
    • 35. server-managed: run on the machine thatcreateditwhile client can lookup and obtain a proxy
    • 36. client managed: makethemrunning on a different machine while the localone just hold a proxy to sendthemmessages
  • Server-managed
    Bean actor =
    Bean.class, BeanImpl.class,
    .start("localhost", 9999)
    “my-service", actor
    Bean remoteActor =
    Bean.class, BeanImpl.class,
    timeout, "", 9999
    class MyActor
    extends UntypedActor {}
    "", 9999
    class MyActor
    extends UntypedActor {}
    .start("localhost", 9999)
  • 37. Software Transactional Memory
    An STM turns the Java heap into a transactional data set with begin/commit/rollback semantics. Very much like a regular database.
    It implements the first three letters in ACID; ACI:
    • Atomic: all or none of the changes made during a transaction get applied
    • 38. Consistent: a transaction gets a consistent of reality
    • 39. Isolated: changes made by concurrent execution transactions are not visible to each other
    Akka’s STM implements the concept already present in Clojure’s STM
    The STM is based on Transactional References (referred to as Refs). Refs:
    • are memory cells, holding an (arbitrary) immutable value
    • 40. implement CAS (Compare-And-Swap) semantics
    • 41. are managed and enforced by the STM for coordinated changes across many Refs
    • 42. are implemented using the excellent Multiverse STM (
  • Why we need an STM?
    public class Container {
    private int value;
    private final ReadWriteLock lock = new ReentrantReadWriteLock();
    public intgetValue() {
    try {
    return value;
    } finally {
    public void setValue(int value) {
    try {
    this.value= value;
    } finally {
    What’swrong with it?
    • Ad-hoc and hardlyreusablelogic
    • 43. Unnecessarycomplexity
    • 44. Lockslimitconcurrency and thenthroughput
  • How does Akka STM work?
    // Create a Ref with an initial value (also possible without)
    final Ref<Integer> ref = new Ref<Integer>(0);
    public int counter() {
    // A transaction is delimited using an Atomic anonymous inner class
    return new Atomic<Integer>() {
    public Integer atomically() {
    intinc = ref.get() + 1; // Accessing the value of the Ref
    ref.set(inc); // Changing the value of the Ref
    return inc;
    • A transaction is automatically retried when it runs into some read or write conflict
    • 45. In this case an (exponential) delay isused to preventfurthercontentions
    • 46. Thereshouldn’t be side-effects inside the transaction to avoid to repeatthem
  • Transactional Datastructures
    final TransactionalMap<String, User> users =
    new TransactionalMap<String, User>();  
    // fill users map (in a transaction)
    new Atomic() {
    public Object atomically() {
    users.put("bill", new User("bill"));
    users.put("mary", new User("mary"));
    users.put("john", new User("john"));
    return null;
    // access users map (in a transaction)
    User user = new Atomic<User>() {
    public User atomically() {
    return users.get("bill").get();
  • 47. Persistent Datastructures
    Copyright Rich Hickey 2009
  • 48. Coordinating Actors with STM
    public class Counter extends UntypedActor {
    private Ref<Integer> count = new Ref(0);
    private void increment() { count.set(count.get() + 1); }
    public void onReceive(Object incoming) throws Exception {
    if (incoming instanceof Coordinated) {
    Coordinated coordinated = (Coordinated) incoming;
    Object message = coordinated.getMessage();
    if (message instanceof Increment) {
    Increment increment = (Increment) message;
    if (increment.hasFriend()) {
    .sendOneWay(coordinated.coordinate(new Increment()));
    coordinated.atomic(new Atomically() {
    public void atomically() {
  • 49. Actors + STM = Transactors
    public class Counter extends UntypedTransactor {
    Ref<Integer> count = new Ref<Integer>(0);
    @Override public Set<SendTo> coordinate(Object message) {
    if (message instanceof Increment) {
    Increment increment = (Increment) message;
    if (increment.hasFriend())
    return include(increment.getFriend(), new Increment());
    return nobody();
    public void atomically(Object message) {
    if (message instanceof Increment) {
    count.set(count.get() + 1);
  • 50. Other Akka goodies
    • Implements Advanced Message Queuing Protocol (AMPQ) based on the RabbitMQ
    • 51. Integration with Apache Camel
    • 52. PersistencemodulesupportingmanyNoSQL DB like: Cassandra, MongoDB, Riak, Redis, Terrastore, CouchDB, Voldemort, Hbase
    • 53. Integration betweenAkkaSTM and JTA (Java Transaction API)
    • 54. OSGisupport
    • 55. Integration with Spring allowing to defineTyped Actors as Spring managed bean
    • 56. Integration with Lift Web Framework and Play! Framework
  • Thanks a lot!
    Happy Java (or Scala) hing
    by Mario Fusco
    twitter: @mariofusco