Book Image

Akka Cookbook

By : Vivek Mishra, Héctor Veiga Ortiz
Book Image

Akka Cookbook

By: Vivek Mishra, Héctor Veiga Ortiz

Overview of this book

Akka is an open source toolkit that simplifies the construction of distributed and concurrent applications on the JVM. This book will teach you how to develop reactive applications in Scala using the Akka framework. This book will show you how to build concurrent, scalable, and reactive applications in Akka. You will see how to create high performance applications, extend applications, build microservices with Lagom, and more. We will explore Akka's actor model and show you how to incorporate concurrency into your applications. The book puts a special emphasis on performance improvement and how to make an application available for users. We also make a special mention of message routing and construction. By the end of this book, you will be able to create a high-performing Scala application using the Akka framework.
Table of Contents (18 chapters)
Title Page
About the Authors
About the Reviewer
Customer Feedback


Akka is a popular toolkit that helps develop concurrent, scalable, resilient, and reactive applications in the JVM thanks to the actor model. The toolkit has been around for some years, so we can consider it as a mature technology. Moreover, it has been successfully implemented by many companies, such as Cisco,, and Groupon. This book helps you understand how Akka (and all the modules around it) work and provides a set of useful recipes to easily achieve several of the most common tasks. It also includes an introductory chapter on Lagom, the latest microservices framework by Lightbend, the company behind the development of Akka.

What this book covers

Chapter 1, Diving into Akka, covers the basic concepts of Akka: actors, actor systems, mailboxes, and switching actor behaviors.

Chapter 2, Supervision and Monitoring, covers the actor life cycle, actor hierarchy, supervision, and monitoring.

Chapter 3, Routing Messages, covers the different types of group and pool router and how the Akka dispatcher works.

Chapter 4, Using Futures and Agents, covers how futures work and how well they integrate with the Akka toolkit. It also covers Akka agents.

Chapter 5, Scheduling Actors and Other Utilities, explains how the Akka scheduler works and contains various useful recipes about packaging, configuring, and running your Akka application.

Chapter 6, Akka Persistence, explains how to persist the state of an stateful actor to a variety of technologies, such as Apache Cassandra and Redis. It also covers different recovering strategies.

Chapter 7, Remoting and Akka Clustering, covers how to use Akka beyond a single JVM, either by using a well-known remote address or by joining a decentralized peer-to-peer-based cluster membership service.

Chapter 8, Akka Streams, covers the Akka Streams framework and how to integrate it with Akka and with third-party technologies.

Chapter 9, Akka HTTP, explains how to use Akka HTTP both on the client and server side. Also, it shows how to choose the API level depending on the use case.

Chapter 10, Understanding Various Akka Patterns, covers how to use different common programming patterns using Akka.

Chapter 11, Microservices with Lagom, introduces Lagom, the microservices framework from Lightbend. It provides information about conventions and how to create, configure, connect, and run microservices with Lagom.

What you need for this book

In this book, you will find recipes that provide examples that show the expected behavior. All code examples are written in the programming language Scala ( Akka also provides a Java API, which we will not be covering in the examples. Despite that, the rationale behind all the recipes work the same in either Java or Scala. This book can also be used by Java developers to understand the core concepts of the different recipes.

In addition to Scala, we use sbt ( as the build tool for the examples in the book. Sbt not only does dependency management, but also helps us run our examples.

Finally, it is recommended (but not required) to use an IDE to develop the different recipes. IntelliJ IDEA or Scala IDE are two good examples of free IDEs to develop Scala applications.

Who this book is for

This book goes through many aspects of the Akka toolkit. From easy recipes, such as Sending messages to Actors, to advanced topics, such as Master Slave work pulling pattern, its aim is to provide useful examples and explanations of how to achieve different patterns with Akka. Even if you are just starting with Akka or you have already used it in the past, these pages will provide you with a deep understanding of Akka that can be applied to new tasks not covered in this book.


In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).


To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We create a project build definition file called build.sbt by executing a simplesbtcommand."

A block of code is set as follows:

object BehaviourAndState extends App {
  val actorSystem = ActorSystem("HelloAkka")
  // creating an actor inside the actor system
  val actor = actorSystem.actorOf(Props[SummingActor])
  // print actor path

Any command-line input or output is written as follows:

sbt "runMain com.packt.chapter1.BehaviorAndState"

New terms and important words are shown in bold.


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at .

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at We also have other code bundles from our rich catalog of books and videos available at Check them out!


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.