Akka Streams | Three Basic Components

Reading Time: 3 minutes

Akka, a free open source toolkit simplifying the construction of concurrent and distributed system/application. We have already deal with Akka Actors where we learnt about the Akka Actors and their Behaviors. But Actors can be seen dealing with with Sequence of Data where they send or receive series of messages. So, In this blog we discuss Akka Stream in bit detail.

What is Akka Streams ?

It is a library to process and transfer the sequence of data, again the size may not be known or it may be infinite. Akka Streams implementations uses the Reactive Streams interface internally to pass the data between different operators. Akka Reactive is an initiative to provide a standard for asynchronous stream processing with non blocking back pressure.

To Introduce Akka Stream to your project add the following dependency to your ‘build.sbt’ file in the project.

library dependencies

This dependency will allow you to access the rich Akka stream library. Before understanding the components of Akka Stream, let us understand some terminologies related to it.

1. Stream –

An active process that involves moving and transforming data.

2. Element –

Processing unit of streams is known to be element which is used to express the size of Buffer.

3. Back-Pressure –

It means a flow of control i.e. understood as non-blocking and synchronous.

4. Non- Blocking –

It suggests that a process should not hinder the progress of the calling thread, although it might take long time to finish the request.

5. Graph –

Graph is used to define the trajectory through which the elements shall flow while stream is running.

6. Operator –

Operator is name of all building block that builds up a graph.

Components of Akka Stream –

Akka Stream has a number of components like Sources, Sinks, Flows, Runnable graphs, Fault Tolerance, Graph, Fusing. We will discuss major of them as follows –

1. Sources –

Source is a stage with exactly single output. It serves as an input to stream and the amount is not predetermined or can be infinite as well.

It emits data elements whenever the downstream operator (Flow, or Sink) are ready to consume or receive data. A type of source can be files, collections and actors etc.

Some types of sources have been defined in the below snippet.

2. Sinks –

Sink is a stage with exactly one input. It serves as an endpoint of the stream.

Such type of operator requests and accepts the data elements, possibly slowing down the upstream producer of elements. It could write data to the files, databases, REST API, collections etc.

Some types of sinks have been defined in the below snippet.

3. Flows –

Flow is a stage with exactly one input and one output thus can act as a Source and Sink. It is used to connect upstream and downstream, manipulating the data (transforming, filtering etc.) flowing through it.

Whenever a Flow receives a demand from downstream, it propagates it to upstream. It also, propagates back-pressure upstream by reducing or stopping the demand.

Some types of sources have been defined in the below snippet.

Below is an example code for illustrating the Akka Stream.

Akka Stream Code

This is a simple code of Akka Stream which connects source, sink and flow.

Conclusion –

In this blog we have learnt about the Akka Stream, their basic terminologies, components and finally a short code demonstrating this.

Written by 

Kuldeepak Gupta is a passionate software consultant at Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. His current passions include utilizing the power of Scala, Akka, and Play to make Reactive and Big Data systems. He is a self-motivated, enthusiastic person who is recognized as a good team player, dedicated, responsible professional, and a technology enthusiast. His hobbies include playing hockey, participating in Political debates, Reading Tech blogs, and listening to songs.