16 Akka Streams, part 2

 

This chapter covers

  • Processing elements efficiently
  • Connecting from a stream to a database, a gRPC service, or an actor
  • Dealing with exceptions
  • Adding elements to a running stream after it starts

A book dealing only with Akka Streams would be no smaller than this entire book. It’s an important topic, but it’s too extensive to cover fully here. In this chapter, you learn the basics and the cases you will encounter most often in practice.

Akka Streams has three layers:

  1. The Flow API—Creates simple linear flows that are taken from a source, transformed, and passed to a sink. A typical example is a source that reads Kafka, enriches the event by calling an external service, and publishes it back to Kafka with a sink.
  2. The GraphDSL—At this level, you can combine multiple streams to create something more complex than a linear stream, namely a graph. An example would be a flow that is fanned out into multiple streams so that in each of these streams, a different service enriches the event; later, all these streams are combined into one flow.
  3. The GraphStage API—This is the lowest-level API, which the Akka Streams library uses to create GraphDSL and the Flow API. It is very unlikely that you will ever need such a low-level composition.

In this chapter, you learn about the Flow API, which you used in chapter 10. It has three types of stages: the Source is the producer, a Sink is the consumer, and, in the middle, the Flow is where the transformations take place.

16.1 Processing elements through services

16.1.1 CPU-bounded services

16.1.2 Non-CPU bounded services

16.2 Connecting to an actor

16.3 Dealing with exceptions

16.3.1 Alternative 1: Deciders

16.3.2 Alternative 2: Modeling exceptions as data

16.3.3 divertTo

16.3.4 Restarting the source

16.4 Adding elements dynamically to a stream

Summary