DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Writing a Chat With Akka
  • How to Write RFCs for Open Source Projects
  • Event-Driven Order Processing Program
  • Lessons Learned While Building Near Real-Time Network Traffic Analyzer

Trending

  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • AI-Based Threat Detection in Cloud Security
  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  1. DZone
  2. Coding
  3. Java
  4. Message Routing in Akka

Message Routing in Akka

Here's an introductory article on message routing in Akka with Scala.

By 
Efthymios Vafeiadis user avatar
Efthymios Vafeiadis
·
Updated Mar. 19, 19 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
14.5K Views

Join the DZone community and get the full member experience.

Join For Free

In many modern software products, there is a need to accept heavy traffic load, perform multitasking, and run procedures in parallel. Systems have multiple execution cores for executing multiple tasks at the same time rather than sequentially with multithreading engineering.

This makes the system faster but it also causes issues, like shared memory problems. Solutions like locking can protect shared memory values and are fully supported by JVM languages. The drawback is that there is a more complicated code implementation. On the other hand, Akka Actor model is an alternative. The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems.

It also alleviates the developer from having to deal with explicit locking and thread management, making it easier to write concurrent and parallel systems. Actors are objects that encapsulate state and behavior and communicate exclusively by exchanging messages, which are placed into the recipient’s mailbox. Many companies and open-source projects use Akka to develop concurrent systems; Paypal's transaction handling is a very interesting case study.

This is an article about routing in Akka — it would be nice to have the minimum familiarity with Actor model and Scala to get the most out of it.

How to Implement Routing in Akka

In some cases, we may need a specific type of message routing mechanism for specific requirements like:

  • Send messages to the less busy actor among other actors of the same type, that is, to the actor with the lowest number of messages.
  • Send messages to actors with a round-robin algorithm, that is, send messages one by one to all actors in a loop.
  • Send a single message to all the actors in a specific group to redistribute the work among actors automatically with the help of some mechanism.
  • Routing allows messages to be routed to one or more actors known as routees, by sending the messages to a router that will know how to route the messages to the routees.

Types of Routing Strategy

In Akka, there are many routing strategies, such as :

  •  RoundRobin: Routes in a round-robin fashion to its routees.
  •  Random: This router type selects one of its routees randomly for each message.
  •  SmallestMailBox: A Router that tries to send to the non-suspended child routee with fewest messages in mailbox.
  •  Broadcast: A broadcast router forwards the message it receives to all its routees.
  •  ScatterGatherFirstCompleted: The ScatterGatherFirstCompletedRouter will send the message on to all its routees. It then waits for the first reply it gets back. This result will be sent back to the original sender. Other replies are discarded.
  •  TailChopping: The TailChoppingRouter will first send the message to one, randomly picked, routee and then after a small delay to a second routee and so on.
     It waits for the first reply it gets back and forwards it back to the original sender. Other replies are discarded.

How Routing Is Designed Within Akka

Routers look like normal actors, but in reality, they are implemented in a different way. Routers are designed to be extremely efficient at receiving messages and passing them quickly on to routees. A normal actor can be used for routing messages, but an actor's single-threaded processing can become a bottleneck. Routers can achieve much higher throughput with optimization to the usual message-processing pipeline that allows concurrent routing.

This is achieved by embedding routers' routing logic directly in their ActorRef rather than in the router actor. Messages sent to a router's ActorRef can be immediately routed to the routee, bypassing the single-threaded router actor entirely. The cost to this is, of course, that the internals of routing code are more complicated than if routers were implemented with normal actors.

Below I present a simple routing implementation with RoundRobin and Broadcast.

Setup

At first, we need to create an sbt Scala project in our IDE — I prefer IntelliJ — with Akka dependencies.

name := "akka-routing"

version := "0.1"

scalaVersion := "2.12.7"

val akkaVersion = "2.5.13"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  "com.typesafe.akka" %% "akka-testkit" % akkaVersion,
  "org.scalatest" %% "scalatest" % "3.0.5"
)


Round Robin Router

Let's begin by creating an example of a Round Robin Router. As we said before, in this case, the router will send messages one by one to all actors in a loop, as described in the following schema.

Round Robin Router

At first, we will create an Actor let's call it RoundRobinActor.

package actors

import akka.actor.{Actor, ActorLogging}

class RoundRobinActor extends Actor with ActorLogging {

  override def receive = {
    case msg: String => log.info(s" I am ${self.path.name}")
    case _ => log.info("Unknown message ")
  }

}


Then, we will test it by creating a RounRobinPool of two actors and route four "hello" messages.

import actors.RoundRobinActor
import akka.actor.{ActorSystem, Props}
import akka.routing.RoundRobinPool

object RoundRobinPoolApp extends App {
  val actorSystem = ActorSystem("Akka-RoundRobin-Example")
  val router = actorSystem.actorOf(RoundRobinPool(2).props
    (Props[RoundRobinActor]))
  for (i <- 1 to 4) {
    router ! s"Hello $i"
  }

}


As we can see in the console, the message routed all routees with the Round Robin algorithm by creating a loop.

[akka://Akka-RoundRobin-Example/user/$a/$a]  I am $a
[akka://Akka-RoundRobin-Example/user/$a/$b]  I am $b
[akka://Akka-RoundRobin-Example/user/$a/$a]  I am $a
[akka://Akka-RoundRobin-Example/user/$a/$b]  I am $b


Broadcast Router

Now, let's create an example for Broadcast Router. As we said, the Broadcast Router will forward the message to all its routees, as we can see in the following schema.

Broadcast Router


Again, we will create an Actor; let's call it BroadcastActor.

package actors

import akka.actor.{Actor, ActorLogging}

class BroadcastActor extends Actor with ActorLogging {
  override def receive = {
    case msg: String => log.info(s" $msg, I am ${self.path.name}")
    case _ => log.info("Unknown message ")
  }

}


Then, we will test it by creating a BroadcastPool of three actors and route the message "hello."

import actors.BroadcastActor
import akka.actor.{ActorSystem, Props}
import akka.routing.BroadcastPool

object Broadcastpool extends App {
  val actorSystem = ActorSystem("Akka-Example-Broadcast")
  val router =
    actorSystem.actorOf(BroadcastPool(3).props
    (Props[BroadcastActor]))
  router ! "hello"

}



As we can see in the console, the message broadcasted successfully to all routees.

 [akka://Akka-Example-Broadcast/user/$a/$c]  hello, I am $c
 [akka://Akka-Example-Broadcast/user/$a/$a]  hello, I am $a
 [akka://Akka-Example-Broadcast/user/$a/$b]  hello, I am $b


This article was an introduction about message routing in Akka with Scala.

Happy routing!

Akka (toolkit) Round-robin (document) Shared memory Open source Scala (programming language) Schema Memory (storage engine) Implementation

Opinions expressed by DZone contributors are their own.

Related

  • Writing a Chat With Akka
  • How to Write RFCs for Open Source Projects
  • Event-Driven Order Processing Program
  • Lessons Learned While Building Near Real-Time Network Traffic Analyzer

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!