Scala IoT Systems With Akka Actors II

Back in 2016, I built an Internet-of-Thing (IoT) prototype system leveraging the “minimalist” design principle of the Actor model to simulate low-cost, low-powered IoT devices. A simplified version of the prototype was published in a previous blog post. The stripped-down application was written in Scala along with the Akka Actors run-time library, which is arguably the predominant Actor model implementation at present. Message Queue Telemetry Transport (MQTT) was used as the publish-subscribe messaging protocol for the simulated IoT devices. For simplicity, a single actor was used to simulate requests from a bunch of IoT devices.

In this blog post, I would like to share a version closer to the design of the full prototype system. With the same tech stack used in the previous application, it’s an expanded version (hence, II) that uses loosely-coupled lightweight actors to simulate individual IoT devices, each of which maintains its own internal state and handles bidirectional communications via non-blocking message passing. Using a distributed workers system adapted from a Lightbend template along with a persistence journal, the end product is an IoT system equipped with a scalable fault-tolerant data processing system.

Main components

Below is a diagram and a summary of the revised Scala application which consists of 3 main components:

IoT with MQTT and Akka Actor Systems v.2

1. IoT

  • An IotManager actor which:
    • instantiates a specified number of devices upon start-up
    • subscribes to a MQTT pub-sub topic for the work requests
    • sends received work requests via ClusterClient to the master cluster
    • notifies Device actors upon receiving failure messages from Master actor
    • forwards work results to the corresponding devices upon receiving them from ResultProcessor
  • Device actors each of which:
    • simulates a thermostat, lamp, or security alarm with random initial state and setting
    • maintains and updates internal state and setting upon receiving work results from IotManager
    • generates work requests and publishes them to the MQTT pub-sub topic
    • re-publishes requests upon receiving failure messages from IotManager
  • A MQTT pub-sub broker and a MQTT client for communicating with the broker
  • A configuration helper object, MqttConfig, consisting of:
    • MQTT pub-sub topic
    • URL for the MQTT broker
    • serialization methods to convert objects to byte arrays, and vice versa

2. Master Cluster

  • A fault-tolerant decentralized cluster which:
    • manages a singleton actor instance among the cluster nodes (with a specified role)
    • delegates ClusterClientReceptionist on every node to answer external connection requests
    • provides fail-over of the singleton actor to the next-oldest node in the cluster
  • A Master singleton actor which:
    • registers Workers and distributes work to available Workers
    • acknowledges work request reception with IotManager
    • publishes work results from Workers to ‘work-results’ topic via Akka distributed pub-sub
    • maintains work states using persistence journal
  • A ResultProcessor actor in the master cluster which:
    • gets instantiated upon starting up the IoT system (more on this below)
    • consumes work results by subscribing to the ‘work-results’ topic
    • sends work results received from Master to IotManager

3. Workers

  • An actor system of Workers each of which:
    • communicates via ClusterClient with the master cluster
    • registers with, pulls work from the Master actor
    • reports work status with the Master actor
    • instantiates a WorkProcessor actor to perform the actual work
  • WorkProcessor actors each of which:
    • processes the work requests from its parent Worker
    • generates work results and send back to Worker

Master-worker system with a ‘pull’ model

While significant changes have been made to the IoT actor system, much of the setup for the Master/Worker actor systems and MQTT pub-sub messaging remains largely unchanged from the previous version:

  • As separate independent actor systems, both the IoT and Worker systems communicate with the Master cluster via ClusterClient.
  • Using a ‘pull’ model which generally performs better at scale, the Worker actors register with the Master cluster and pull work when available.
  • Paho-Akka is used as the MQTT pub-sub messaging client.
  • A helper object, MqttConfig, encapsulates a MQTT pub-sub topic and broker information along with serialization methods to handle MQTT messaging using a test Mosquitto broker.

What’s new?

Now, let’s look at the major changes in the revised application:

First of all, Lightbend’s Activator has been retired and Sbt is being used instead.

On persisting actors state, a Redis data store is used as the persistence journal. In the previous version the shared LevelDB journal is coupled with the first seed node which becomes a single point of failure. With the Redis persistence journal decoupled from a specific cluster node, fault tolerance steps up a notch.

As mentioned earlier in the post, one of the key changes to the previous application is the using of actors representing individual IoT devices each with its own state and capability of communicating with entities designated for interfacing with external actor systems. Actors, lightweight and loosely-coupled by design, serve as an excellent vehicle for modeling individual IoT devices. In addition, non-blocking message passing among actors provides an efficient and economical means for communication and logic control of the device state.

The IotManager actor is responsible for creating and managing a specified number of Device actors. Upon startup, the IoT manager instantiates individual Device actors of random device type (thermostat, lamp or security alarm). These devices are maintained in an internal registry regularly updated by the IoT manager.

Each of the Device actors starts up with a random state and setting. For instance, a thermostat device may start with an ON state and a temperature setting of 68F whereas a lamp device might have an initial state of OFF and brightness setting of 2. Once instantiated, a Device actor will maintain its internal operational state and setting from then on and will report and update the state and setting per request.

Work and WorkResult

In this application, a Work object represents a request sent by a specific Device actor and carries the Device’s Id and its current state and setting data. A WorkResult object, on the other hand, represents a returned request for the Device actor to update its state and setting stored within the object.

Responsible for processing the WorkResult generated by the Worker actors, the ResultProcessor actor simulates the processing of work result – in this case it simply sends via the actorSelection method the work result back to the original Device actor through IotManager. Interacting with only the Master cluster system as a cluster client, the Worker actors have no knowledge of the ResultProcessor actor. ResultProcessor receives the work result through subscribing to the Akka distributed pub-sub topic which the Master is the publisher.

While a participant of the Master cluster actor system, the ResultProcessor actor gets instantiated when the IoT actor system starts up. The decoupling of ResultProcessor instantiation from the Master cluster ensures that no excessive ResultProcessor instances get started when multiple Master cluster nodes start up.

Test running the application

Complete source code of the application is available at GitHub.

To run the application on a single JVM, just git-clone the repo, run the following command at a command line terminal and observe the console output:

The optional NumOfDevices parameter defaults to 20.

To run the application on separate JVMs, git-clone the repo to a local disk, open up separate command line terminals and launch the different components on separate terminals:

Sample console log

Below is filtered console log output from the console tracing the evolving state and setting of a thermostat device:

The following annotated console log showcases fault-tolerance of the master cluster – how it fails over to the 2nd node upon detecting that the 1st node crashes:

Scaling for production

The Actor model is well suited for building scalable distributed systems. While the application has an underlying architecture that emphasizes on scalability, it would require further effort in the following areas to make it production ready:

  • IotManager uses the ‘ask’ method for message receipt confirmation via a Future return by the Master. If business logic allows, using the fire-and-forget ‘tell’ method will be significantly more efficient especially at scale.
  • The MQTT broker used in the application is a test broker provided by Mosquitto. A production version of the broker should be installed preferably local to the the IoT system. MQTT brokers from other vendors like HiveMQ, RabbitMQ are also available.
  • As displayed in the console log when running the application, Akka’s default Java serializer isn’t best known for its efficiency. Other serializers such as Kryo, Protocol Buffers should be considered.
  • The Redis data store for actor state persistence should be configured for production environment

Further code changes to be considered

A couple of changes to the current application might be worth considering:

Device types are currently represented as strings, and code logic for device type-specific states and settings is repeated during instantiation of devices and processing of work requests. Such logic could be encapsulated within classes defined for individual device types. The payload would probably be larger as a consequence, but it might be worth for better code maintainability especially if there are many device types.

Another change to be considered is that Work and WorkResult could be generalized into a single class. Conversely, they could be further differentiated in accordance with specific business needs. A slightly more extensive change would be to retire ResultProcessor altogether and let Worker actors process WorkResult as well.

State mutation in Akka Actors

In this application, a few actors maintain mutable internal states using private variables (private var):

  • Master
  • IotManager
  • Device

As an actor by-design will never be accessed by multiple threads, it’s generally safe enough to use ‘private var’ to store changed states. But if one prefers state transitioning (as opposed to updating), Akka Actors provides a method to hot-swap an actor’s internal state.

Hot-swapping an actor’s state

Below is a sample snippet that illustrates how hot-swapping mimics a state machine without having to use any mutable variable for maintaining the actor state:

Simplified for illustration, the above snippet depicts a Worker actor that pulls work from the Master cluster. The context.become method allows the actor to switch its internal state at run-time like a state machine. As shown in the simplified code, it takes an ‘Actor.Receive’ (which is a partial function) that implements a new message handler. Under the hood, Akka manages the hot-swapping via a stack. As a side note, according to the relevant source code, the stack for hot-swapping actor behavior is, ironically, a mutable ‘private var’ of List[Actor.Receive].

Recursive transformation of immutable parameter

Another functional approach to mutating actor state is via recursive transformation of an immutable parameter. As an example, we can avoid using a mutable ‘private var registry’ as shown in the following ActorManager actor and use ‘context.become’ to recursively transform a registry as an immutable parameter passed to the updateState method:

11 thoughts on “Scala IoT Systems With Akka Actors II

  1. Alan

    On the Worker.scala class you need to substitute:
    val workProcessor = context.watch(context.actorOf(WorkProcessor.props(), “work-processor”))
    for:
    val workProcessor = context.watch(context.actorOf(workProcessorProps, “work-processor”))

    taht way you can reuse the worker class for other types of “work”

    Also, how can I make the Work and WorkResult Generic (Work.scala)? Whenever I add a generic type to those case classes the WorkQueue.scala file complains.

    Reply
    1. Leo Cheung Post author

      Thanks for the feedback, and for pointing out the potential problem with the using of WorkProcessor.Props(), which for some reason crept into the revised version of the Worker class.

      Re: customizing (or generalizing) Work and WorkResult, I left that to the blog readers since it would be largely governed by specific business requirement. As mentioned in the blog post, the master-worker system for work distribution was adapted from a Lightbend template (https://www.lightbend.com/activator/template/akka-distributed-workers). Minimal code change was made in adapting from the template, as the inner-working of the work distribution system isn’t the core focus of the application. Corresponding code change in WorkQueue and probably elsewhere might be inevitable when customizing Work/WorkResult. For instance, WorkQueue consists of code logic that assumes workID of String type being a class member of Work.

      Reply
  2. Silva

    Hi Cheung,
    your job was great, congratulations.

    I have an issue. Have you thought about how to make iotManager be fault tolerant and scalable, just like the Master actor, in a simple way?

    Reply
  3. Leo Cheung Post author

    Thanks for the kind words. On fault tolerance, you’re right that IoTManager/Device can be enhanced to operate like the Master actor or leverage cluster sharding (plus persistence journal) feature. It all comes down to specific business requirement.

    Reply
  4. Konstantinos Chaitas

    Hello Cheung,

    very nice work and super helpful. One question that I have is regarding the mqtt broker. As far as I understand the mqtt broker is still centralized right ? Is there any way we could make it decentralized/distributed ? I know there are distributed brokers out there e.g Vernemq, but is it possible to make a ‘centralized’ mqtt broker e.g mosquito distributed using Akka and maybe clustering/consistent hashing ? Thanks

    Reply
    1. Leo Cheung Post author

      I appreciate the kind words. Yes, the Mosquitto broker used in the proof-of-concept application is not distributed. It’s worth noting that the application is supposed to be largely agnostic to the specific MQTT broker being used, thus it would require little to no code change to replace the existing MQTT broker with something else. If you want a distributed broker, I would recommend going with a by-design distributed product (VerneMQ, Mosca, EMQ, etc), rather than trying to repurpose Mosquitto into something it isn’t principally designed for.

      Reply
      1. Konstantinos Chaitas

        Thanks for the fast and helpful response. I am planning to work for my Master thesis on a project which is using the Moquette MQTT broker (very similar to Mosquitto) and it creates a bottleneck in my whole system. Therefore there are mainly 2 options. 1) To replace it with a distributed MQTT broker(VerneMQ, Mosca, EMQ, etc), or 2) to make it distributed. Since the 2nd option looks more interesting and I could learn more things, I was thinking to give it a try using an Akka cluster and maybe consistent hashing to distribute the traffic to the appropriate node/mqtt broker in the cluster. Actually that’s how I found your blog. Do you think that my idea/design could work ? Can you imagine any brokers/drawbacks ? Thanks in advance

        Reply
        1. Leo Cheung Post author

          As an academic project, building a broker cluster with Mosquitto (or similar MQTT brokers) does sound like an interesting exercise, though it probably wouldn’t be equivalent to a full-feature distributed broker without extensive repurposing effort. Perhaps not exactly what you’re aiming at, this Stack Overflow Q&A might be of interest.

          Reply
  5. Pingback: An Akka Actor-based Blockchain | Genuine Blog

  6. Pingback: Akka GRPC for IoT Streams | Genuine Blog

  7. Pingback: Dynamic IoT Streams With Akka GRPC | Genuine Blog

Leave a Reply to Leo Cheung Cancel reply

Your email address will not be published. Required fields are marked *