Akka in Action, Second Edition

Book description

Akka solves the big problems of distributed systems, from multithreading and concurrency to scalability and failure. Learn how to use it effectively.

In Akka in Action, Second Edition you will learn how to:

  • Create basic programs with Akka Typed
  • Work with clusters to build robust, fault-tolerant programs
  • Use Akka with Kubernetes
  • Build microservices with Akka
  • Create and maintain distributed state with strong consistency guarantees
  • Employ actor-based concurrency and parallelism
  • Test Akka software

Akka in Action, Second Edition teaches you to use Akka Typed to solve common problems of distributed systems. You’ll learn how to bring together all of Akka’s moving parts to design and implement highly scalable and maintainable software. Extensively revised by Akka contributor Francisco López-Sancho Abraham, this new edition demonstrates Akka’s complex concepts through engaging hands-on examples. Discover the power of the Actor Model, how Akka works with Kubernetes, and how to utilize Akka modules to create microservices that are reliable and fault tolerant.

About the Technology
For large software systems, the action is in the “-ilities.” Scalability. Reliability. Maintainability. Capability. Akka, toolkit for building distributed message-driven applications, delivers on the “ilities.” And recent innovations, including Akka Typed, ensure that this amazing platform will remain the best way to build and deploy distributed Java and Scala applications for years to come.

About the Book
Akka in Action, Second Edition is your guide to building message-centric distributed applications systems. This new edition covers all features of Akka, including Akka Typed. You’ll learn to create microservices using Akka’s powerful suite of tools, Akka Sharding, Persistence, Streams, Persistence Query, Projections, and gRPC. Practical examples taken directly from industry guide you through clustering, deploying to Kubernetes, and taking full advantage of Akka’s Actors-based approach to concurrency.

What's Inside
  • Work with clusters to build robust, fault-tolerant programs
  • Maintain distributed systems with strong consistency guarantees
  • Utilize concurrency and parallelism
  • Test Akka software


About the Reader
For readers comfortable with Java and Scala.

About the Authors
Francisco Lopez Sancho-Abraham is a senior consultant at Lightbend, and a principal engineer on the Akka Team. Raymond Roestenburg, Rob Bakker, and Rob Williams are the authors of the first edition of Akka in Action.

Quotes
Akka in Action is the best source for a deep yet practical and hands-on introduction to Akka. In it, Francisco discusses everything from the foundational distributed systems stuff to how to integrate it with the more recent high-level modules like Alpakka and Akka Streams. Highly recommended.
- Jonas Bonér, Lightbend

All you need to start with Akka and the Actor Model.
- Rares Tausancea, ING

A gem that few books can match when it comes to Actors, CQRS, and Event Sourcing. For Scala and Java developers, it’s an essential read.
- Sebastian Harko, IBM

Delivers a powerful combination of easy-to-follow lessons on the Akka ecosystem and well-timed discussions on important topics in modern computing.
- John Ackley, Carnegie Learning

Table of contents

  1. inside front cover
  2. Praise for the first edition
  3. Akka in Action
  4. Copyright
  5. dedication
  6. contents
  7. front matter
    1. preface
    2. acknowledgments
    3. about this book
      1. Who should read this book
      2. How this book is organized: A roadmap
      3. About the code
      4. liveBook discussion forum
    4. about the author
      1. Authors of the previous edition
    5. about the cover illustration
  8. 1 Introducing Akka
    1. 1.1 What is Akka?
    2. 1.2 Actors: A quick overview
    3. 1.3 Two approaches to scaling: Setting up the example
    4. 1.4 Traditional scaling
      1. 1.4.1 Traditional scaling and durability: Moving everything to the database
      2. 1.4.2 Traditional scaling and interactive use: Polling
      3. 1.4.3 Traditional scaling: Transactions
    5. 1.5 Scaling with Akka
      1. 1.5.1 Scaling with Akka and durability: Sending and receiving messages
      2. 1.5.2 Scaling with Akka and interactive use: Pushing messages
      3. 1.5.3 Scaling with Akka and failure: Asynchronous decoupling
      4. 1.5.4 The Akka approach: Sending and receiving messages
    6. 1.6 Actors: One programming model to rule up and out
      1. 1.6.1 An asynchronous model
      2. 1.6.2 Actor operations
    7. 1.7 Akka actors
      1. 1.7.1 ActorSystem
      2. 1.7.2 ActorRef, mailboxes, and actors
      3. 1.7.3 Dispatchers
      4. 1.7.4 Actors and the network
    8. Summary
  9. 2 Up and running
    1. 2.1 Printing money
    2. 2.2 Starting to code
      1. 2.2.1 The protocol of the actor
      2. 2.2.2 Creating an application and instantiating the actor
      3. 2.2.3 Sending messages
      4. 2.2.4 Implementing the actor: Receiving messages
      5. 2.2.5 Terminating the system
      6. 2.2.6 The application
      7. 2.2.7 The solution in Git
      8. 2.2.8 Running the app
    3. 2.3 Keeping state with a variable
    4. 2.4 Keeping state with behaviors
    5. 2.5 Scheduling a message
    6. Summary
  10. 3 One actor is no actor
    1. 3.1 Simple parser example
      1. 3.1.1 Coding the app
      2. 3.1.2 Coding the guardian and spawning
      3. 3.1.3 Sending messages back and forth: Adapting responses
      4. 3.1.4 The protocol with the adapter’s message
      5. 3.1.5 The adapter’s function
      6. 3.1.6 Delegating
      7. 3.1.7 A protocol with commands and responses as traits
      8. 3.1.8 Coding the worker
    2. 3.2 Asking and expecting a reply
      1. 3.2.1 A simple question
      2. 3.2.2 Coding the manager
      3. 3.2.3 The protocols
      4. 3.2.4 Asking
      5. 3.2.5 Coding the worker
      6. 3.2.6 Using context
      7. 3.2.7 Ask signature
    3. 3.3 Ask with a payload
    4. Summary
  11. 4 Akka test kit
    1. 4.1 Testing approaches
    2. 4.2 Sync testing
      1. 4.2.1 Effects
      2. 4.2.2 More than one actor
      3. 4.2.3 Testing the logs
    3. 4.3 Async testing
      1. 4.3.1 Using probes
      2. 4.3.2 Fishing for messages
      3. 4.3.3 Logging
      4. 4.3.4 Log capturing
    4. Summary
  12. 5 Fault tolerance
    1. 5.1 What fault tolerance is (and what it isn’t)
      1. 5.1.1 Plain old objects and exceptions
      2. 5.1.2 Wrap it up and let it crash
    2. 5.2 Actor lifecycle events: Signals
    3. 5.3 Supervision strategies and signals
      1. 5.3.1 Uneventful resuming
      2. 5.3.2 Stopping and the PostStop signal
      3. 5.3.3 Restart and the PreRestart signal
      4. 5.3.4 Custom strategy
    4. 5.4 Watching signals from an actor
    5. 5.5 Back to the initial use case
      1. 5.5.1 Supervisor hierarchy initial design
      2. 5.5.2 Supervision hierarchy alternative design
    6. Summary
  13. 6 Discovery and routing
    1. 6.1 Discovery: The receptionist
    2. 6.2 The built-in integration router pattern
    3. 6.3 Balancing load using built-in routers
      1. 6.3.1 Akka pool router
      2. 6.3.2 Changing strategies
      3. 6.3.3 Akka group router
      4. 6.3.4 Consistent hashing strategy
    4. 6.4 Implementing the router pattern using actors
      1. 6.4.1 Content-based routing
      2. 6.4.2 State-based routing
    5. Summary
  14. 7 Configuration
    1. 7.1 Trying out Akka configuration
      1. 7.1.1 Order
      2. 7.1.2 Subtrees
      3. 7.1.3 Substitutions
      4. 7.1.4 Using defaults
    2. 7.2 Akka configuration
    3. 7.3 Multiple systems
      1. 7.3.1 Lifting with Fallback
    4. 7.4 Configuration in tests
      1. 7.4.1 Lifting in tests
    5. Summary
  15. 8 Clustering
    1. 8.1 On top of Akka Cluster
    2. 8.2 Why use clustering?
      1. 8.2.1 Cluster membership: Joining a cluster
      2. 8.2.2 Minimal cluster example
      3. 8.2.3 Starting the cluster
      4. 8.2.4 Leaving the cluster
      5. 8.2.5 Unreachable
      6. 8.2.6 Downing a reachable node
    3. 8.3 Akka Management and the Cluster HTTP extension
      1. 8.3.1 Cluster subscriptions
    4. 8.4 Clustered job processing
      1. 8.4.1 In practice
      2. 8.4.2 The code
      3. 8.4.3 Work distribution in the master
      4. 8.4.4 Starting the cluster
    5. 8.5 Resilient job
      1. 8.5.1 Serialization
      2. 8.5.2 Testing is no different
    6. Summary
  16. 9 Sharding and persistence
    1. 9.1 Akka sharding and stateful systems
      1. 9.1.1 The big picture
      2. 9.1.2 An example: A shipping container
      3. 9.1.3 The simplicity of the sharded entities
      4. 9.1.4 Rebalancing configuration
      5. 9.1.5 Passivation
      6. 9.1.6 Remembering entities
    2. 9.2 Persistence
      1. 9.2.1 The ingredients
      2. 9.2.2 Persistence combined with sharding: A persistent entity
      3. 9.2.3 Available effects
    3. 9.3 Customizing the persistent entity
      1. 9.3.1 Failure
      2. 9.3.2 Recovery
      3. 9.3.3 Snapshotting
      4. 9.3.4 Tagging
      5. 9.3.5 A peek at serialization and schema evolution
    4. 9.4 Running example
    5. Summary
  17. 10 Streams, persistence queries, and projections
    1. 10.1 Akka Streams
      1. 10.1.1 Basic semantics
      2. 10.1.2 Finite streams
      3. 10.1.3 Source
      4. 10.1.4 Flow
      5. 10.1.5 Sink
      6. 10.1.6 Blueprint
      7. 10.1.7 Materialization
      8. 10.1.8 Infinite streams
    2. 10.2 Akka Persistence Query
      1. 10.2.1 Where the rubber meets the road
    3. 10.3 Projections
      1. 10.3.1 Reading
      2. 10.3.2 Writing
      3. 10.3.3 Putting everything together
      4. 10.3.4 The ShardedDaemonProcess
      5. 10.3.5 Back to the SPContainer projection
      6. 10.3.6 All the main parts
    4. 10.4 Projections in action
    5. Summary
  18. 11 Akka ports
    1. 11.1 Akka HTTP
      1. 11.1.1 Akka HTTP servers
      2. 11.1.2 The path
      3. 11.1.3 Directives
      4. 11.1.4 Route directives
      5. 11.1.5 Marshalling and unmarshalling
      6. 11.1.6 Akka HTTP communicating with actors
    2. 11.2 Akka gRPC
      1. 11.2.1 The Protocol Buffers side
      2. 11.2.2 The RPC side
      3. 11.2.3 The plugin and the .proto file
      4. 11.2.4 Akka gRPC in action
      5. 11.2.5 Running the service
      6. 11.2.6 Akka gRPC with an actor
      7. 11.2.7 Running the example
    3. Summary
  19. 12 Real-world example: An Akka betting house
    1. 12.1 The actors
      1. 12.1.1 The wallet
      2. 12.1.2 The market
      3. 12.1.3 The bet
    2. 12.2 The ports
      1. 12.2.1 The market
    3. Summary
  20. 13 Clustering, part 2
    1. 13.1 Akka Cluster Bootstrap
      1. 13.1.1 Clustering in local
      2. 13.1.2 Cluster in action
    2. 13.2 Clustering with the Kubernetes API
      1. 13.2.1 Creating the Docker image
      2. 13.2.2 Kubernetes deployment
      3. 13.2.3 Optional: Setting Java options
      4. 13.2.4 Kubernetes role and role binding
      5. 13.2.5 Service account
    3. 13.3 Split Brain Resolver
      1. 13.3.1 An unreachable problem
      2. 13.3.2 SBR strategies
    4. 13.4 Cluster singletons
    5. Summary
  21. 14 Connecting to systems with Alpakka
    1. 14.1 Alpakka Kafka
      1. 14.1.1 Consuming from Kafka in action
      2. 14.1.2 Detecting consumer failures
      3. 14.1.3 Auto-commit
      4. 14.1.4 Committable sources
    2. 14.2 Pushing to Kafka
      1. 14.2.1 At-most-once delivery guarantee
      2. 14.2.2 At-least-once delivery guarantee
    3. 14.3 Effectively-once delivery
    4. 14.4 Alpakka CSV
      1. 14.4.1 Mapping by column
      2. 14.4.2 Reading and writing with FileIO
    5. Summary
  22. 15 Akka betting house, part 2
    1. 15.1 Projections
      1. 15.1.1 Database projection
      2. 15.1.2 Kafka projection
      3. 15.1.3 The betting-house entry point
    2. 15.2 Configuration
      1. 15.2.1 Persistence
      2. 15.2.2 Cluster local
      3. 15.2.3 Cluster Kubernetes
      4. 15.2.4 Services, sharding, and projections
    3. 15.3 Deployment
      1. 15.3.1 Running local
      2. 15.3.2 Running in Kubernetes
    4. Summary
  23. 16 Akka Streams, part 2
    1. 16.1 Processing elements through services
      1. 16.1.1 CPU-bounded services
      2. 16.1.2 Non-CPU bounded services
    2. 16.2 Connecting to an actor
    3. 16.3 Dealing with exceptions
      1. 16.3.1 Alternative 1: Deciders
      2. 16.3.2 Alternative 2: Modeling exceptions as data
      3. 16.3.3 divertTo
      4. 16.3.4 Restarting the source
    4. 16.4 Adding elements dynamically to a stream
    5. Summary
  24. Appendix A. Setting up
    1. A.1 Installing the JDK
      1. A.1.1 Installing SDKMAN!
      2. A.1.2 Installing Java
    2. A.2 Installing sbt
    3. A.3 Installing Docker
    4. A.4 Installing curl
    5. A.5 Installing grpcurl
    6. A.6 Installing kcat
    7. A.7 Installing the AWS CLI
    8. A.8 Installing Minikube
  25. Appendix B. Microservices and architectural principles
    1. B.1 Do microservices exist?
      1. B.1.1 Let’s bound a context
      2. B.1.2 Characteristics of better services: What were we missing?
      3. B.1.3 Jailed services and other isolations
      4. B.1.4 Some challenges from isolation
    2. B.2 Architectural principles
      1. B.2.1 Event-driven systems
      2. B.2.2 Architectural patterns
      3. B.2.3 Business abstractions
  26. index

Product information

  • Title: Akka in Action, Second Edition
  • Author(s): Robertus Bakker, Rob Williams, Raymond Roestenburg, Francisco Lopez-Sancho
  • Release date: August 2023
  • Publisher(s): Manning Publications
  • ISBN: 9781617299216