You are previewing Learning Akka.
O'Reilly logo
Learning Akka

Book Description

Build fault tolerant concurrent and distributed applications with Akka

About This Book

  • Build networked applications that self-heal

  • Scale out your applications to handle more traffic faster

  • An easy-to-follow guide with a number of examples to ensure you get the best start with Akka

  • Who This Book Is For

    This book is intended for beginner to intermediate Java or Scala developers who want to build applications to serve the high-scale user demands in computing today. If you need your applications to handle the ever-growing user bases and datasets with high performance demands, then this book is for you. Learning Akka will let you do more for your users with less code and less complexity, by building and scaling your networked applications with ease.

    What You Will Learn

  • Use Akka to overcome the challenges of concurrent programming

  • Resolve the issues faced in distributed computing with the help of Akka

  • Scale applications to serve a high number of concurrent users

  • Make your system fault-tolerant with self-healing applications

  • Provide a timely response to users with easy concurrency

  • Reduce hardware costs by building more efficient multi-user applications

  • Maximise network efficiency by scaling it

  • In Detail

    Software today has to work with more data, more users, more cores, and more servers than ever. Akka is a distributed computing toolkit that enables developers to build correct concurrent and distributed applications using Java and Scala with ease, applications that scale across servers and respond to failure by self-healing. As well as simplifying development, Akka enables multiple concurrency development patterns with particular support and architecture derived from Erlang’s concept of actors (lightweight concurrent entities). Akka is written in Scala, which has become the programming language of choice for development on the Akka platform.

    Learning Akka aims to be a comprehensive walkthrough of Akka. This book will take you on a journey through all the concepts of Akka that you need in order to get started with concurrent and distributed applications and even build your own.

    Beginning with the concept of Actors, the book will take you through concurrency in Akka. Moving on to networked applications, this book will explain the common pitfalls in these difficult problem areas while teaching you how to use Akka to overcome these problems with ease.

    The book is an easy to follow example-based guide that will strengthen your basic knowledge of Akka and aid you in applying the same to real-world scenarios.

    Style and approach

    An easy-to-follow, example-based guide that will take you through building several networked-applications that work together while you are learning concurrent and distributed computing concepts. Each topic is explained while showing you how to design with Akka and how it is used to overcome common problems in applications. By showing Akka in context to the problems, it will help you understand what the common problems are in distributed applications and how to overcome them.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

    Table of Contents

    1. Learning Akka
      1. Table of Contents
      2. Learning Akka
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewer
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. eBooks, discount offers, and more
          6. Questions
      9. 1. Starting Life as an Actor
        1. What's in this book?
        2. Chapter overview
        3. What is Akka
          1. Actor Model origins
          2. What's an Actor anyway?
          3. Actors and Message passing
            1. The Evolution of supervision and fault tolerance in Erlang
            2. The Evolution of distribution and location transparency
        4. What we will build
          1. Example 1 – handling distributed state
          2. Example 2 – getting lots of work done
        5. Setting up your environment
          1. Choosing a language
          2. Installing Java – Oracle JDK8
            1. Installing on Windows
            2. Installing on OS X
            3. Installing on Linux or Unix (Universal instructions)
          3. Ensuring Java is configured in your environment
          4. Installing Scala
          5. Installing Typesafe Activator
            1. Windows
            2. Linux/Unix/OS X
            3. OS X
          6. Creating a new project
          7. Installing an IDE
            1. Install IntelliJ CE
            2. Eclipse
              1. Installing Eclipse (Scala-Ide)
              2. Preparing the project for Eclipse
              3. Importing the project into Eclipse
        6. Creating your first Akka application – setting up the SBT project
          1. Adding Akka to build.sbt
            1. A note on getting the right Scala version with %%
            2. Adding other Dependencies from Maven Central
          2. Creating your first Actor
            1. Making the Message first
              1. Java
              2. Scala
            2. Defining Actor response to the Message
              1. Java – AkkademyDb.java
              2. Scala – AkkademyDb.scala
          3. Validating the code with unit tests
            1. Akka Testkit
              1. Java
              2. Scala
          4. Running the test
        7. Homework
        8. Summary
      10. 2. Actors and Concurrency
        1. Reactive system design
        2. The 4 reactive tenets
          1. Responsive
          2. Elastic
          3. Resilient
          4. Event-driven/message-driven
          5. Reactive Tenet Correlation
        3. Anatomy of an Actor
          1. Java Actor API
          2. Scala Actor API
        4. Creating an actor
          1. Props
        5. Promises, futures, and event-driven programming models
          1. Blocking versus event-driven APIs
            1. Skills check-point
          2. Having an Actor respond via a future
            1. Java example
              1. Working with Scala futures
              2. Test case
              3. Actor creation
            2. Scala example
              1. Test case
              2. Actor creation
            3. Blocking threads in tests
          3. Understanding futures and promises
            1. Future – expressing failure and latency in types
              1. Preparing for the Java example
              2. Preparing for Scala examples
              3. Note on sleeping
              4. Anatomy of a future
              5. Handling success cases
                1. Executing code with the result
                2. Transforming the result
                3. Transforming the result asynchronously
              6. Handling failure cases
                1. Executing code in the failure case
                2. Recovering from failure
                3. Recovering from failure asynchronously
              7. Composing futures
                1. Chaining operations together
                2. Combining futures
                3. Dealing with lists of futures
                4. Future cheat-sheet
              8. Composing a Distributed System – AkkademyDb and client
          4. Preparing the DB and messages
            1. The messages
            2. Implementing the DB functionality
            3. Enabling remoting
            4. Main
            5. Publishing the messages
            6. Starting the DB
          5. Producing the client
            1. Scaffolding the project
            2. Modifying build.sbt
            3. Building the client
            4. Testing
        6. Homework
          1. General learning
          2. Project homework
        7. Summary
      11. 3. Getting the Message Across
        1. Setting the stage with an example problem
          1. Sketching the project
            1. Core functionality
        2. Messaging delivery
          1. Messages should be immutable
          2. Ask message pattern
            1. Designing with Ask
            2. Callbacks execute in another execution context
            3. Timeouts are required
            4. Timeout stacktraces aren't useful
            5. Ask has overhead
            6. Complexity of Actors and Ask
          3. Tell
            1. Designing with Tell
              1. Handling replies with Tell
              2. Scheduling a Tell Timeout
              3. Avoiding Ask with an Anonymous Actor
            2. Forward
            3. Pipe
        3. Homework
          1. General learning
          2. Project homework
        4. Summary
      12. 4. Actor Lifecycle – Handling State and Failure
        1. The 8 Fallacies of Distributed Computing
          1. The network is reliable
          2. Latency is zero
          3. Bandwidth is infinite
          4. The network is secure
          5. Network topology doesn't change
          6. There is one administrator
          7. Transport cost is zero
          8. The network is homogeneous
        2. Failure
          1. Isolating failure
            1. Redundancy
          2. Supervision
            1. Supervision hierarchies
            2. Supervision strategies and the drunken sushi chef
            3. Defining supervisor strategies
            4. Actor lifecycle
            5. Messages in restart, stop
            6. Terminating or killing an Actor
            7. Lifecycle monitoring and DeathWatch
            8. Safely restarting
        3. State
          1. Online/Offline state
            1. Transitioning state
            2. Stashing messages between states
          2. Conditional statements
          3. Hotswap: Become/Unbecome
            1. Stash leaks
            2. Finite State Machines (FSM)
            3. Defining states
            4. Defining the state container
            5. Defining behavior in FSMs
          4. Using restarts to transition through states
        4. Homework
        5. Summary
      13. 5. Scaling Up
        1. Moore's law
        2. Multicore architecture as a distribution problem
        3. Choosing Futures or Actors for concurrency
        4. Doing work in parallel
          1. Doing work In parallel with futures
          2. Doing work in parallel with Actors
            1. Introducing Routers
            2. Routing logic
            3. Sending Messages to All Actors in a Router Group/Pool
            4. Supervising the Routees in a Router Pool
        5. Working with Dispatchers
          1. Dispatchers explained
          2. Executors
          3. Creating Dispatchers
          4. Deciding Which Dispatcher to use where
          5. Default Dispatcher
          6. Blocking IO dispatcher use with futures
          7. Article parsing dispatcher
            1. Using a configured dispatcher with Actors
            2. Using BalancingPool/BalancingDispatcher
          8. Optimal parallelism
        6. Homework
        7. Summary
      14. 6. Successfully Scaling Out – Clustering
        1. Introducing Akka Cluster
        2. One Giant Monolith or Many Micro Services?
        3. Definition of a Cluster
          1. Failure Detection
          2. Gossiping an Eventually Consistent View
        4. CAP Theorem
          1. C – Consistency
          2. A – Availability
          3. P – Partition Tolerance
          4. Compromises in CAP Theorem
            1. CP System – Preferring Consistency
            2. AP System – Preferring Availability
            3. Consistency as a Sliding Scale
        5. Building Systems with Akka Cluster
          1. Creating the Cluster
            1. Configuring the Project
            2. Seed Nodes
            3. Subscribing to Cluster Events
            4. Starting the Cluster
            5. Leaving the Cluster Gracefully
          2. Cluster Member States
            1. Failure Detection
          3. Routing Messages to the Cluster
          4. Producing a Distributed Article Parse Service
          5. Cluster Client for Clustered Services
            1. Setting up the Server Project
            2. Setting up the Client Project
            3. Sharing the Message Class between Client and Server
            4. Sending Messages to the Cluster
            5. Building a Distributed Key Value Store
            6. Disclaimer – Distributed Systems are Hard
          6. Designing the Cluster
            1. Basic Key-Value Store Design
            2. Coordinating Node
              1. Sharding for Linear Scalability
            3. Redundant Nodes
        6. Combining Sharding and Replication
          1. Pre-Sharding And Redistributing Keys to New Nodes
        7. Addressing Remote Actors
          1. Using akka.actor.Identify to Find a Remote Actor
        8. Homework
        9. Summary
      15. 7. Handling Mailbox Problems
        1. Overwhelming your weakest link
          1. Ballooning response times
          2. Crashing
        2. Resiliency
          1. Mailboxes
            1. Configuring mailboxes
              1. Selecting a mailbox in deployment configuration
              2. Selecting a mailbox in code
            2. Deciding which mailbox to use
              1. Prioritizing messages in mailboxes
        3. Staying responsive under load
          1. Circuit breakers
            1. Circuit breaker listeners
            2. Circuit breaker examples
        4. Homework
        5. Summary
      16. 8. Testing and Design
        1. Example problem
        2. Approaching application design
          1. High-Level design
        3. Designing, building, and testing the Domain model
          1. Specifications
          2. Designing the Domain model
          3. Testing and building the Domain model
          4. Building by specification
        4. Testing actors
          1. Testing Actor behavior and state
          2. Testing Message flow
            1. Using the test Itself as an Actor
            2. Using TestProbes as mock Actors
        5. Testing Advice
        6. Homework
        7. Summary
      17. 9. A Journey's End
        1. Other Akka Features and Modules
          1. Logging in Akka
          2. Message Channels and EventBus
          3. Agents
          4. Akka Persistence
          5. Akka I/O
          6. Akka streams and HTTP
        2. Deployment Tools
        3. Monitoring Logs and Events
          1. Next Steps
          2. Writing some Actor Code
          3. Coursera Courses
        4. Summary
      18. Index