O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Scala Reactive Programming

Book Description

Build fault-tolerant, robust, and distributed applications in Scala

About This Book

  • Understand and use the concepts of reactive programming to build distributed systems running on multiple nodes.
  • Learn how reactive architecture reduces complexity throughout the development process.
  • Get to grips with functional reactive programming and Reactive Microservices.

Who This Book Is For

This book is for Scala developers who would like to build fault-tolerant, scalable distributed systems. No knowledge of Reactive programming is required.

What You Will Learn

  • Understand the fundamental principles of Reactive and Functional programming
  • Develop applications utilizing features of the Akka framework
  • Explore techniques to integrate Scala, Akka, and Play together
  • Learn about Reactive Streams with real-time use cases
  • Develop Reactive Web Applications with Play, Scala, Akka, and Akka Streams
  • Develop and deploy Reactive microservices using the Lagom framework and ConductR

In Detail

Reactive programming is a scalable, fast way to build applications, and one that helps us write code that is concise, clear, and readable. It can be used for many purposes such as GUIs, robotics, music, and others, and is central to many concurrent systems. This book will be your guide to getting started with Reactive programming in Scala.

You will begin with the fundamental concepts of Reactive programming and gradually move on to working with asynchronous data streams. You will then start building an application using Akka Actors and extend it using the Play framework. You will also learn about reactive stream specifications, event sourcing techniques, and different methods to integrate Akka Streams into the Play Framework. This book will also take you one step forward by showing you the advantages of the Lagom framework while working with reactive microservices. You will also learn to scale applications using multi-node clusters and test, secure, and deploy your microservices to the cloud.

By the end of the book, you will have gained the knowledge to build robust and distributed systems with Scala and Akka.

Style and approach

The book takes a pragmatic approach, showing you how to build a scalable distributed system using Scala and Akka.

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 files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Scala Reactive Programming
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started with Reactive and Functional Programming
    1. Introduction to Reactive
      1. What is Reactive?
      2. What is Reactive programming?
      3. What is a data stream or stream?
      4. RP versus Reactive systems versus Reactive architecture
      5. Event-Driven versus Message-Driven
      6. Benefits of Reactive systems with RP
    2. Functional programming
      1. What is functional programming?
      2. Principles of functional programming
      3. Benefits of functional programming
      4. Functional Reactive programming
      5. Types of RP
      6. Why FP is the best fit for RP
    3. Reactive Manifesto
      1. Need of Reactive Manifesto
      2. Principles of Reactive systems
        1. Message-Driven
        2. Elasticity
        3. Resilience
        4. Responsiveness
      3. Why Reactive Streams specification?
      4. Why is Play Framework the best for Reactive systems?
      5. Reactive systems versus traditional systems
    4. The Java 9 Flow API
      1. Flow API – Publisher
      2. Flow API – Subscriber
      3. Flow API – Subscription
      4. Flow API – Processor
      5. Flow API – Flow
    5. Implementations of Reactive Streams
      1. Lightbend's Reactive Platform
      2. Pivotal's Reactor project
      3. Microsoft's Reactive Extensions (RX)
      4. Netflix's RxJava
      5. Eclipse's Vert.x
      6. Ratpack
    6. How are Reactive Streams born?
    7. Marble diagrams
      1. What is a Marble diagram?
      2. Data transformation
      3. Benefits of Marble diagrams
      4. Rules of Marble diagrams
      5. Important FRP operators
        1. FRP – the map() function Marble diagram
        2. FRP – the flatMap() function Marble diagram
        3. FRP – the merge() function Marble diagram
        4. FRP – the filter() function Marble diagram
        5. FRP – the reduce() function Marble diagram
        6. FRP – the concat() and sorted() functions Marble diagram
      6. Observer pattern versus Reactive pattern
    8. Summary
  8. Functional Scala
    1. Introduction to Scala
      1. The Scala ecosystem
      2. Understanding the Scala Application
      3. Scala REPL
    2. Principles of Scala FP
      1. FP Design Patterns
      2. Scala FP features in action
        1. Immutability
        2. Scala functions
        3. Scala pure functions
      3. Pattern matching
      4. Scala combinators
      5. For-comprehensions
      6. Scala implicits
        1. Implicit parameters
          1. Implicit conversions
    3. Scala anonymous functions
    4. Everything is an expression
    5. Referential transparency
    6. Functions are first-class citizens
      1. Partial functions
      2. Function currying
      3. Higher-Order Functions
    7. Scala tail-recursion
      1. Types of recursions
      2. Benefits of linear recursion
        1. A linear-recursion example
      3. Benefits of tail-recursion
        1. A tail-recursion example
    8. Scala Type class
      1. Benefits of Type classes
    9. Scala Collections in action
      1. Scala List
        1. Scala List Cons operator
        2. Right associative rule
      2. Scala Map
      3. Scala Range
    10. Scala Functional Design Patterns
      1. Scala map() function
      2. Scala flatMap() function
        1. Advantages of flatMap
      3. Scala Monads in action
        1. Scala Option
        2. Scala Either
        3. Scala Case class and object
        4. Benefits of Scala Case class
    11. Scala Traits in action
      1. Trait as an interface
      2. Traits linearization 
      3. Linearization rules
    12. Summary
  9. Asynchronous Programming with Scala
    1. Introduction to Scala AP
      1. What is asynchronous?
      2. Differences between asynchronous and synchronous
      3. Benefits of asynchronous programming
      4. Differences between Concurrency and Parallelism
      5. How Scala supports AP
    2. The Scala Future API
      1. Building blocks of the Scala Future API
      2. Benefits of the Scala Future API
    3. The Scala Future
      1. What is a Future in Scala?
      2. The Scala Future API
        1. What is a computation unit?
        2. Future Trait definition
        3. Future Companion object
        4. The complete Scala Future API
      3. Scala Future examples
    4. The Scala Promise
      1. What is a Scala Promise?
      2. The Scala Promise API
      3. Scala Promise examples
    5. Scala ExecutionContext
      1. What is ExecutionContext?
    6. The relationship between Scala Future components
      1. Differences between a Future and a Promise
    7. Scala Future API callbacks
    8. Scala Future API combinators
    9. Scala Future.sequence()
    10. The Scala Async Library
      1. Scala Async API
    11. The Scala Future API in Play Framework
    12. The Scala Future API in the Akka Toolkit
    13. A Scala Future versus a Java Future
    14. Summary
  10. Building Reactive Applications with Akka
    1. Introduction to Akka
      1. What is Akka?
      2. Applications on the JVM (Java Virtual Machine)
      3. Features of Akka
      4. Benefits of Akka (or why do we need Akka?)
      5. Building blocks of the Akka Toolkit
      6. Akka Extensions (or modules)
      7. Akka Clients
    2. Actor Model in-depth
      1. What is the Actor Model?
      2. Principles of the Actor Model (or properties of Actor in the Actor Model)
      3. Issues with the Shared-State Concurrency model
      4. Benefits of the Actor Model
      5. Components of the Actor Model (or building blocks of the Actor Model)
      6. What are the benefits of immutable messages in the Actor Model?
      7. How Akka implements the Actor Model
      8. Other Actor Model implementations
    3. Akka ActorSystem
      1. What is the ActorSystem?
      2. Roles and responsibilities of ActorSystem
        1. How to create an Akka ActorSystem?
        2. How to shut down an Akka ActorSystem?
      3. Components of Akka's ActorSystem
      4. What is materialization and Akka's implementation?
    4. Akka Actors
      1. What is an Actor?
      2. Components of Akka Actor
        1. Akka Actor – ActorRef
        2. Akka Actor – Dispatcher
          1. Benefits of the dispatcher pattern
        3. Akka Actor – Mailbox(MessageQueue)
        4. Akka Actor – Actor
        5. Akka Actor – ActorPath
      3. Actor versus thread
      4. The lifecycle of an Akka Actor
        1. Actor's preStart() lifecycle method
        2. Actor's postStop() lifecycle method
        3. Actor's preRestart() lifecycle method
        4. Actor's postRestart() lifecycle method
      5. Why Actors are lightweight?
    5. Actor basic operations
      1. Defining an Actor
      2. Creating an Actor
      3. Sending messages to an Actor
        1. The tell (!) function
        2. The ask (?) function
      4. Replaying messages from an Actor
        1. Actor to Actor communication
        2. Actor to non-Actor communication
      5. Stopping an Actor
      6. Killing an Actor
      7. Become/unbecoming an Actor
        1. Case object Switch
      8. Supervise an Actor
    6. Akka Supervision
      1. What is supervision?
      2. What is the main goal of Akka's supervision?
      3. Benefits of supervision
        1. Why don't we write exceptions or failures handling in Actor itself?
      4. Rules of Akka Supervision
      5. Akka Supervision strategies
      6. Akka's supervision hierarchy
        1. Actor's Path
    7. What is Akka's Let It Crash model?
      1. Akka HelloWorld Actor example
    8. Akka Actors communicate example
    9. Akka Actor's lifecycle example
    10. Akka parent-child Actors example
    11. Actor's Path versus Reference
      1. Differences between ActorRef and ActorPath
    12. When we execute actorRef.tell (message), what happens internally?
      1. MessageDispatcher
    13. How to configure a dispatcher?
    14. Akka Configuration
    15. Akka SBT Templates
    16. Akka Logging
    17. Business problem
      1. Problem discussion
    18. Akka Actor's implementation
    19. Summary
  11. Adding Reactiveness with RxScala
    1. Introduction to RxScala
      1. Reactive Extensions
        1. Understanding Rx implementations
        2. RxScala
        3. Benefits of Reactive Extensions
        4. Limitations of Reactive Extensions
    2. Building blocks of RxScala
      1. Understanding the Observable
        1. Developing RxScala HelloWorld with an Observable
      2. Observer
        1. Extending RxScala HelloWorld with Observer
      3. Subscriber
        1. Extending RxScala HelloWorld with Subscriber
      4. Subscription
      5. Scheduler
    3. RxScala Marble diagrams
      1. RxScala's map() function
    4. Summary
  12. Extending Applications with Play
    1. Introduction to Play Framework
      1. What is Play Framework?
      2. Features of Play Framework
      3. Benefits of Play Framework
      4. Why Play Framework is so fast?
      5. Clients of Play Framework
      6. Building blocks of Play Framework
      7. Play Routings
        1. Client Request
        2. Controller Action
        3. Routes
      8. Play Controllers
    2. View Templates
      1. Benefits of Twirl Templates
    3. Model and Forms
      1. Other components – services and repositories
    4. Configuration
    5. Play Framework View Template constructs
      1. Twirl View Templates
      2. Twirl Template Constructs
    6. Architecture of Play Framework
      1. The new features of Play Framework
        1. Play Framework 2.5.x features
        2. Play Framework 2.6.x features
      2. Play Application project structure
      3. Play/Scala HelloWorld example
    7. What is Play Dependency Injection (DI)
      1. Benefits of DI
    8. Extend HelloWorld Example With DI
    9. Extending HelloWorld example with Scala Futures
    10. Play Form – Data Model
      1. Play Framework Form API
      2. Play Form – Binding Data
      3. Play Form functions
      4. Play Framework Form-based web application
    11. Extending Play/Scala HelloWorld example with Akka Toolkit
    12. Play Fileupload web application
    13. Summary
  13. Working with Reactive Streams
    1. Introduction to Akka Streams
      1. What is a stream?
        1. Goals of data streaming
      2. What is Akka Streams?
        1. Goals of the Akka Streams API
      3. Features of the Akka Streams API
      4. Benefits of the Akka Streams API
      5. Why do we need Akka Streams and why not just Akka?
    2. Other Reactive Streams implementations
    3. Components of Akka Streams
    4. The Akka Streams API
      1. What is streaming data?
        1. The Akka Streams Source component
        2. The Akka Streams Sink component
        3. The Akka Streams Flow component
      2. RunnableGraph or Graph
    5. Modules of Akka Streams
    6. Akka Materialization
      1. What is materialization?
      2. Akka's Materializer
      3. Akka's ActorMaterializer
        1. Roles and responsibilities of Akka's ActorMaterializer
      4. Akka's Actor versus Akka Stream's ActorMaterializer
    7. Backpressure
      1. The traditional backpressure approach
        1. Push – Fast Producer / Slow Consumer
        2. Pull – Slow Producer/Fast Consumer
        3. The dynamic push/pull model
    8. Akka Streams programming basics
      1. Creating a Source
      2. Creating a Sink
      3. Connect a Source to a Sink
      4. Creating a Flow
      5. Connecting a Source to a Flow to a Sink
    9. Why do we need ActorSystem and ActorMaterializer?
    10. Akka Streams HelloWorld example
    11. Extending the Akka Streams HelloWorld example with the Flow component
      1. HelloWorld example description
    12. Akka Streams GraphDSL
      1. Goal of Akka Streams Graph DSL
      2. Building blocks of Akka Streams Graph DSL
        1. Fan-In functions
        2. Fan-Out functions
        3. Edge operator
    13. An Akka Streams GraphDSL example
    14. Akka Persistence
      1. Akka Persistence features
      2. The ES (Event Sourcing) model
        1. What is an Event?
        2. An Event Stream
        3. Principles of an Event Sourcing (ES) model
          1. Event sourcing
    15. The CQRS pattern
      1. Benefits of CQRS
      2. How Akka Persistence implements CQRS/ES
    16. How to develop Akka Persistence Actors
      1. Step1 – Use the PersistenceActor trait
      2. Step2 – Implement PersistenceActor's receiveRecover
      3. Step3 – Implement PersistenceActor's receiveCommand
      4. Step4 – Implement PersistenceActor's persistenceId
      5. Step5 – Configure our journal details in application.conf
    17. Akka Persistence MongoDB Weather example
    18. Summary
  14. Integrating Akka Streams to Play Application
    1. Akka Streams
      1. Akka Streams revisited
      2. Types of Akka Stream components
      3. Akka Dynamic Streams
        1. MergeHub
        2. BroadcastHub
        3. PartitionHub
    2. Developing Akka Dynamic Streams
    3. Integrating Akka Streams into Play
      1. Designing a Reactive Chat System
      2. Developing the Reactive Chat System
      3. Test Reactive Chat Application
    4. Akka Persistence Query
      1. Building blocks of Akka Persistence App
      2. Developing WF Reactive Akka Persistence App
      3. Testing WF Reactive Akka Persistence App
    5. Summary
  15. Reactive Microservices with Lagom
    1. Introduction to Lagom Framework
      1. What is Lagom Framework?
      2. Features of Lagom Framework
      3. Benefits of Lagom Framework
    2. Drawbacks of monolith architecture
    3. Benefits of microservice architecture
    4. Principles of microservices
    5. Lagom Reactive Platform architecture
    6. Modules of Lagom Framework
      1. Lagom Core modules
      2. Lagom Scala Core modules
      3. Lagom Persistence modules
      4. Lagom Clustering modules
      5. Lagom Kafka modules
      6. Lagom logging module
      7. Building blocks of Lagom Framework
      8. Lagom Service Locator
      9. Lagom Service Gateway
      10. Lagom Service Descriptor
      11. Lagom Framework's identifiers
      12. Lagom ServiceCall in detail
        1. Description
      13. Internal components of Lagom Framework
    7. Kafka with ZooKeeper
      1. Cassandra
    8. Lagom project structure
    9. Lagom System API
    10. Lagom System – implementation
    11. Lagom System – frontend
    12. Lagom Hello Reactive System
      1. Getting Hello Service code
      2. Testing the Hello Reactive System
    13. Lagom Hello Reactive System microservices
    14. Understanding the Hello Service API code
    15. Understanding HelloService implementation code
    16. Developing Lagom WF Reactive System
      1. WF Reactive System architecture
      2. WF Reactive System – Producer API
      3. WF Reactive System – Producer implementation
      4. WF Reactive System – Consumer API
      5. WF Reactive System – Consumer implementation
      6. WF Reactive System – frontend
      7. Testing the WF Reactive System
    17. Summary
  16. Testing Reactive Microservices
    1. Introduction to TDD
      1. What is TDD?
      2. Code Coverage
      3. Benefits of TDD
    2. Unit testing frameworks
      1. ScalaTest
      2. ScalaTest Plus
      3. Benefits of ScalaTest
      4. Unit testing Scala applications
    3. Unit testing Play Framework components
      1. Test HelloWorld without using DI Controller
      2. Test HelloWorld with DI Controller
      3. Unit testing Akka Actors
        1. Akka Toolkit's testing modules
      4. Testing HelloWorld Actor
      5. Unit testing HelloWorldActor (Version 2)
      6. Executing HelloWorld Actor unit tests
      7. Unit testing Akka Streams
        1. Uniting test Akka Streams, with Actor's Testkit
        2. Unit testing Akka Streams with Akka Stream's Testkit
    4. Unit testing Lagom services
      1. The Code Coverage tool
      2. The SCoverage tool
        1. SBT SCoverage plugin
    5. Summary
  17. Managing Microservices in ConductR
    1. Introduction to Lightbend's ConductR
      1. What is ConductR?
      2. Responsibilities of ConductR
      3. Advantages of ConductR
      4. Components of ConductR
      5. ConductR APIs
        1. The Bundle API
        2. The Control API
    2. Installing Lightbend ConductR
      1. Prerequisites
      2. Installing the ConductR CLI
      3. ConductR Sandbox Visualizer
      4. Understanding ConductR Sandbox information
    3. Preparing WF Reactive System for ConductR
      1. ConductRSBT plugin
        1. Mix ConductRApplicationComponents trait
    4. Deploying the WF Reactive System on ConductR
      1. Starting ConductR Sandbox with multinodes
    5. Starting ConductR Sandbox with a single node
      1. Bundle WF Reactive System components
    6. Observing the WF Reactive System bundles
      1. ConductR Bundle descriptor
      2. Loading WF Reactive System bundles
        1. Load wf-producer-impl
        2. Loading wf-consumer-impl
        3. Loading wf-frontend
    7. Running and testing WF Reactive System bundles
    8. Useful ConductR commands
    9. Summary
  18. Reactive Design Patterns and Best Practices
    1. Understanding Design Patterns
      1. Understanding Let-it-Crash
    2. The Circuit Breaker pattern
      1. Understanding the Circuit Breaker pattern
      2. With or without Circuit Breaker
      3. How a Circuit Breaker works
    3. The Sharding Pattern
    4. The Event Sourcing pattern
    5. The Event Streaming Pattern
    6. The Active-Passive Replication Pattern
    7. The Resource Management Design Patterns
      1. The Reactive Resource Loan pattern
      2. The Reactive Resource Pool pattern
    8. Message Flow Patterns
    9. The Request-Response Design Pattern
    10. Understanding the Reactive Design Pattern
    11. Ask Reactive Design Pattern
    12. The Throttling Pattern
    13. The Pull pattern
    14. Reactive System's best practices and tools
      1. Tools
      2. Useful best practices
        1. Prefer tell over ask with Akka Actors
        2. Don't sequentialize Futures
    15. Summary
  19. Scala Plugin for IntelliJ IDEA
    1. Understanding Scala Plugin
    2. How to set up Scala Plugin for IntelliJ IDE
    3. Summary
  20. Installing Robomongo
    1. What is Robomongo?
      1. Setting up Robomongo
      2. How to use Robomongo?
    2. Summary
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think