Akka Stream
Sonu Mehrotra
Software Consultant
Knoldus Software LLP
Agenda
1) What is Akka Stream ?
2) Components of Akka Stream.
3) Working with Graphs.
4) Error handling.
5) Testing Streams.
Akka Stream
There are two basic problems with data processing -
1) Blocking (Pull Based System)
2) Back Pressure (Push Based System)
Akka Streams is a toolkit for processing of streams. It is an
implementation of Reactive Streams Specification. Its purpose
is to “formulate stream processing setups such that we can
then execute them efficiently and with bounded resource
usage.”
Components of Akka Streams
There are three main components of Akka Streams -
1) Source
2) Flow
3) Sink
Source
Source is a “processing stage with exactly one output,
emitting data elements whenever downstream
processing stages are ready to receive them.”
●
Various Sources -
Sink
Sink is a “processing stage with exactly one input,
requesting and accepting data elements possibly slowing
down the upstream producer of elements.”
Various Sinks -
Flow
Flow is a “processing stage which has exactly one input
and output, which connects its up- and down streams by
transforming the data elements flowing through it.”
Various Flows -
Working with Graphs
Graphs are built from simple Flows which serve as the
linear connections within the graphs as well as junctions
which serve as fan-in and fan-out points for Flows.
Various Junctions provided by Akka Streams -
●
Fan-out Junctions
●
Fan-in Junctions
Fan-out Junctions
Fan-out operations give us the ability to split a stream
into sub-streams.
●
Broadcast[T] – (1 input, N outputs)
Ingests elements from one input and emits duplicated
events across more than one output.
●
Balance[T] – (1 input, N outputs)
Ingests elements from one input and emits to the first
available output port.
Fan-in Junctions
Fan-in operations give us the ability to join multiple streams into a single
output stream.
●
Merge[In] – (N inputs , 1 output)
Picks randomly from inputs pushing them one by one to its output.
●
MergePreferred[In]
Similar to Merge but if elements are available on preferred port, it picks
from it, otherwise randomly from others.
●
Concat[A] – (2 inputs, 1 output)
Concatenates two streams (first consume one, then the second one)
Error Handling
Strategies for how to handle exceptions from processing stream
elements can be defined when materializing the stream.
There are three ways to handle Exceptions -
●
Stop – The stream is completed with failure.
●
Resume – The element is dropped and the stream continues.
●
Restart - The element is dropped and the stream continues after
restarting the stage. Restarting a stage means that any accumulated
state is cleared.
Testing Streams
There are 3 ways to test the stream -
●
Simple sources, sinks and flows.
●
Sources and Sinks in combination with TestProbe from
the akka-testkit module.
●
Sources and Sinks specifically crafted for writing tests
from the akka-stream-testkit module.
References
[1] Akka Streams Documentation
[2] Lightbend Activator
[3] Opencredo Blog
Thank you !

Akka streams

  • 1.
    Akka Stream Sonu Mehrotra SoftwareConsultant Knoldus Software LLP
  • 2.
    Agenda 1) What isAkka Stream ? 2) Components of Akka Stream. 3) Working with Graphs. 4) Error handling. 5) Testing Streams.
  • 3.
    Akka Stream There aretwo basic problems with data processing - 1) Blocking (Pull Based System) 2) Back Pressure (Push Based System) Akka Streams is a toolkit for processing of streams. It is an implementation of Reactive Streams Specification. Its purpose is to “formulate stream processing setups such that we can then execute them efficiently and with bounded resource usage.”
  • 4.
    Components of AkkaStreams There are three main components of Akka Streams - 1) Source 2) Flow 3) Sink
  • 5.
    Source Source is a“processing stage with exactly one output, emitting data elements whenever downstream processing stages are ready to receive them.” ● Various Sources -
  • 6.
    Sink Sink is a“processing stage with exactly one input, requesting and accepting data elements possibly slowing down the upstream producer of elements.” Various Sinks -
  • 7.
    Flow Flow is a“processing stage which has exactly one input and output, which connects its up- and down streams by transforming the data elements flowing through it.” Various Flows -
  • 8.
    Working with Graphs Graphsare built from simple Flows which serve as the linear connections within the graphs as well as junctions which serve as fan-in and fan-out points for Flows. Various Junctions provided by Akka Streams - ● Fan-out Junctions ● Fan-in Junctions
  • 9.
    Fan-out Junctions Fan-out operationsgive us the ability to split a stream into sub-streams. ● Broadcast[T] – (1 input, N outputs) Ingests elements from one input and emits duplicated events across more than one output. ● Balance[T] – (1 input, N outputs) Ingests elements from one input and emits to the first available output port.
  • 10.
    Fan-in Junctions Fan-in operationsgive us the ability to join multiple streams into a single output stream. ● Merge[In] – (N inputs , 1 output) Picks randomly from inputs pushing them one by one to its output. ● MergePreferred[In] Similar to Merge but if elements are available on preferred port, it picks from it, otherwise randomly from others. ● Concat[A] – (2 inputs, 1 output) Concatenates two streams (first consume one, then the second one)
  • 11.
    Error Handling Strategies forhow to handle exceptions from processing stream elements can be defined when materializing the stream. There are three ways to handle Exceptions - ● Stop – The stream is completed with failure. ● Resume – The element is dropped and the stream continues. ● Restart - The element is dropped and the stream continues after restarting the stage. Restarting a stage means that any accumulated state is cleared.
  • 12.
    Testing Streams There are3 ways to test the stream - ● Simple sources, sinks and flows. ● Sources and Sinks in combination with TestProbe from the akka-testkit module. ● Sources and Sinks specifically crafted for writing tests from the akka-stream-testkit module.
  • 13.
    References [1] Akka StreamsDocumentation [2] Lightbend Activator [3] Opencredo Blog
  • 14.