concept Akka in category akka

appears as: Akka, Akka, n Akka, Akka
Reactive Applications with Akka .NET

This is an excerpt from Manning's book Reactive Applications with Akka .NET.

1.3.2. Where not to use Akka.NET

Although Akka.NET makes it easier to build large reactive systems, it brings with it some difficulties. You’ve seen how complex systems force you to consider their complexities. For example, you must think about partial failures of system components that might impact other components, consider data consistency and how that should be handled in the case of partial failures, and deal with plenty of other issues. Akka.NET brings these difficulties and complexities to the surface as first-class principles, which means that you have to address them. When dealing with them, you’ll also unearth a number of other complexities: notably, debugging is more difficult, and you have to think about concurrency. Therefore, for fairly simple web applications that have basic requirements, Akka.NET is unlikely to provide any significant benefits. These include relatively basic CRUD (create, read, update, delete) applications that are backed by a basic database model.

At its core, Akka.NET provides a concurrency model designed to allow multiple components to operate simultaneously. This means that when developing systems with Akka.NET, you need to think carefully about the data in your system. Although Akka.NET removes the possibility of concurrent access to shared data, there’s still the opportunity for data races to occur, as well as the potential for deadlocks. For a system that doesn’t need to operate concurrently, Akka.NET is likely to complicate matters rather than simplify them.

Figure 2.2. The use of state machines with actors in Akka.NET allows you to simplify the process of developing actors with complex internal logic.
using Akka.Persistence;

public class ShoppingCartActor : PersistentActor
{
    public override string PersistenceId { get { throw new
     NotImplementedException(); } }

    protected override bool ReceiveCommand(object message)
    {
            throw new NotImplementedException();
    }
    protected override bool ReceiveRecover(object message)
    {
        throw new NotImplementedException();
    }
}

You know that a reactive application depends on systems that are responsive, regardless of system failures, failures in external dependencies, or increased load; and you’ve seen numerous tools and design methodologies to help you achieve that end. You’ve seen how to get around many of the problems relating to fault tolerance and scalability when running on a single machine through the use of Akka.Remote.

When you create applications that use Akka.Remote, you architect with the explicit intention of having to perform manual configuration changes. For example, when you had a subset of the actor deployment tree hosted on another machine, you needed to specify the exact network address for the machine that was hosting it. You had to do the same thing when you assigned routees on separate machines to develop applications that could scale out. But there was no process in place for what should happen if the machine hosting the actor system failed, leaving you with no actor system running. You’d need to constantly monitor your Akka.Remote application and be ready to update the configuration at any point during the day or night. Even if you managed to script the process of updating the actor system to point to new instances as they became available, you’d still be faced with inevitable downtime, because you’d need to reprocess configuration by restarting the actor system.

Although using Akka.Remote certainly presents some advantages over using a single machine, it doesn’t yet match all the traits of reactive applications, which can automatically react to their hosting environment and remain fault tolerant in the face of failure, or scale when faced with increased or decreased load. The key part here is the automatic reaction to their environment. Akka.Remote’s role wasn’t automatic and required some degree of human intervention to manage. In an ideal situation, you’d add more machines that would become part of a single actor system distributed across multiple machines.

Akka.NET provides this functionality through the Akka.Cluster module, which builds on top of the low-level functionality provided by Akka.Remote to create a high-level overview of the networked actor system. It creates a cluster of the individual actor systems and allows you to address all the machines as though they were a single actor system. In this chapter, you’ll see how to create a cluster from independent actor systems. You’ll see how to deploy actors into the cluster and let Akka.Cluster transparently handle the deployment on any machine in the cluster. You’ll also see some of the more advanced features available in Akka.Cluster that help you easily build applications that make the most of the resources available in these vast clusters of machines.

12.1. Introducing Akka.Cluster

Akka.Cluster builds on top of the networking abstraction provided by Akka.Remote to create a scalable, fault-tolerant cluster of machines that can represent a cluster of actor systems as a single actor system (see figure 12.1). Akka .Cluster is therefore incredibly useful for scenarios in which you need high availability guarantees that you can’t achieve by using a single machine to host an actor system.

Figure 12.1. With Akka.NET ‘s clustering capabilities, you can treat a cluster of individual machines as a single actor system.

Some of Akka.Cluster’s concepts can be confusing, especially considering it presents a new area of computing—distributed computing. It’s useful to define some of the terms used throughout this chapter:

  • Node—An individual actor system instance that runs as part of a larger cluster. A node runs an Akka.NET application that connects to and communicates with other nodes in the cluster.
  • Reactive Design Patterns

    This is an excerpt from Manning's book Reactive Design Patterns.

    Erlang and Akka provide proxies through which all Actor interactions must take place: a PID in Erlang and an ActorRef in Akka. So, an individual Actor does not need to know the physical location of the Actor to which it is sending a message—a feature called location transparency, treated at length in chapter 5. This makes message-sending code more declarative, because all the physical “how-to” details of how the message is actually sent are dealt with behind the scenes. Location transparency enables you to add even such sophisticated features as starting a new Actor and rerouting all messages to it if a receiving Actor goes down mid-conversation, without anyone needing to alter the message-sending code.

    Listing 14.2. Lifting the EC2 node into a Future to simplify failure recovery
    import scala.PartialFunction; import scala.concurrent.ExecutionContext; import scala.concurrent.Future; #1 import akka.dispatch.Futures; import akka.japi.pf.PFBuilder; import akka.pattern.CircuitBreaker; #1 private ExecutionContext exeCtx; private CircuitBreaker circuitBreaker; public Future<Instance> startInstanceAsync(AWSCredentials credentials) {   Future<Instance> f = circuitBreaker.callWithCircuitBreaker(() ->     Futures.future(() -> startInstance(credentials), exeCtx));   PartialFunction<Throwable, Future<Instance>> recovery = #2     new PFBuilder<Throwable, Future<Instance>>()       .match(AmazonClientException.class,              ex -> ex.isRetryable(), #3              ex -> startInstanceAsync(credentials))       .build(); #4   return f.recoverWith(recovery, exeCtx); #5 } #1 - Using Scala’s Future and Akka’s CircuitBreaker #2 - Defines the recovery strategy #3 - Some AWS calls can safely be retried. #4 - The circuit breaker will take care of recurring failures, and any unmatched exceptions will not be recovered. #5 - Decorates the Future with recovery
    Akka in Action

    This is an excerpt from Manning's book Akka in Action.

    1.1. What is Akka?

    In this book we’ll show how the Akka toolkit, an open source project built by Lightbend, provides a simpler, single programming model—one way of coding for concurrent and distributed applications—the actor programming model. Actors are (fitting for our industry) nothing new at all, in and of themselves. It’s the way that actors are provided in Akka to scale applications both up and out on the JVM that’s unique. As you’ll see, Akka uses resources efficiently and makes it possible to keep the complexity relatively low while an application scales.

    Akka’s primary goal is to make it simpler to build applications that are deployed in the cloud or run on devices with many cores and that efficiently leverage the full capacity of the computing power available. It’s a toolkit that provides an actor programming model, runtime, and required supporting tools for building scalable applications.

    Listing 2.3. The sbt build file
    enablePlugins(JavaServerAppPackaging) #1 name := "goticks" version := "1.0" organization := "com.goticks" #2 libraryDependencies ++= { #3 val akkaVersion = "2.4.9" #4 Seq( "com.typesafe.akka" %% "akka-actor" % akkaVersion, #5 "com.typesafe.akka" %% "akka-http-core" % akkaVersion, "com.typesafe.akka" %% "akka-http-experimental" % akkaVersion, "com.typesafe.akka" %% "akka-http-spray-json-experimental" % akkaVersion, "io.spray" %% "spray-json" % "1.3.1", "com.typesafe.akka" %% "akka-slf4j" % akkaVersion, "ch.qos.logback" % "logback-classic" % "1.1.3", "com.typesafe.akka" %% "akka-testkit" % akkaVersion % "test", "org.scalatest" %% "scalatest" % "2.2.0" % "test" ) } #1 - Needed for deploying to Heroku (coming up) #2 - Info about our app #3 - Tells sbt about remote repositories to get dependencies from #4 - The version of Akka we’re using #5 - The Akka actor module dependency (Lightbend was formerly Typesafe; hence the package names) !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!
    Reactive Web Applications: With Play, Akka, and Reactive Streams

    This is an excerpt from Manning's book Reactive Web Applications: With Play, Akka, and Reactive Streams.

    One of the main design goals of the Scala programming language is to enable developers to tackle the complexity of programming multicore and distributed systems. It does so by favoring immutable values and data structures over mutable ones, providing functions and higher-order functions as first-class citizens of the language, as well as easing the use of an expression-oriented programming style. For this reason, this book’s examples are written in Scala rather than Java. (It should, however, be noted that Play, Akka, and Reactive Streams all have Java APIs.) We’ll review the core concepts of functional programming with Scala in chapter 3.

    The actor-based concurrency model[1] was popularized by the Erlang programming language and is implemented on the JVM by the Akka concurrency toolkit (http://akka.io). This chapter provides an introduction to the wonderful world of actors. As you’ll see, actors are a very effective tool for building scalable and resilient applications. As Akka is available out of the box in Play, we can use it to implement more-advanced asynchronous logic.

    You’ll also need to declare the latest version of Akka as a dependency in build.sbt because we’ll use a few of its features, along with the library for logging:

    Finally, for logging to work correctly, it requires a bit of configuration. First you need to tell Akka where to log by adding the following configuration in conf/application.conf.

    Listing 6.2. Configuring logging bindings
    akka { loggers = ["akka.event.slf4j.Slf4jLogger"] loglevel = "DEBUG" logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" }

    We’re using the SLF4J logger (http://slf4j.org) provided by Akka. Because Play already includes logback (http://logback.qos.ch), we’ll use it as the SLF4J back end, which means we need to configure it appropriately. Adjust the conf/logback.xml file that’s generated when using the Activator, as follows.

    Listing 7.21. Publishing an event on Akka’s built-in event stream