Introduction To Akka Streams – Part 1

Akka is powerful open source library that helps you to develop distributed application which are fault tolerant and highly concurrent. Akka Streams is implementation of Reactive Streams specification which helps you to develop application that requires stream consumption.

Why Application need Stream Processing

In todays modern application development , stream consumption is one of the most important way to consume data , whether its stream of video coming from OTT platform to your device or in some cases your mobile scroll events going from end-user to server. All these are examples of stream of data flowing from one end to another. In such cases Stream processing becomes highly important. Moreover in todays micro-service based cloud application development micro-services not just rely on REST based communication but also events streams and data streams are common communication medium.

Akka Streams

Akka Streams which are built on top of popular Akka Actors library is one the most popular Stream processing library. A Stream can be visualised as a process that includes transforming and operating on continuous flow of data . Now the Source of this data can be anything a simple array , or File System or in most cases the Stream is coming from the streaming service like Kinesis or Kafka.

Akka Stream Concepts

In this post we will look into only linear processing in akka streams . Later in this series we will explore graph based stream topology.To Start with basic elements of the Akka Streams are Source, Sink and Flow . You can imagine Source as name suggest from where the stream initialise. Akka Stream API provides number of way to initialise source something like this.

Source<Integer, NotUsed> source = Source.range(1, 10);

This represents a Source of Integer stream. This stream can be run using method runForEach which will call a Producer lambda for each value of the stream data.

source.runForeach(i -> System.out.println(i),  actorSystem)

In above we have to also provide the actorSystem object in order to run this stream. Putting it all to gather we have

public void runBasicAkkaStream(){
                .thenRun(() -> System.out.print("Stream Consumed"));

public CompletionStage<Void> runningForEachElement(Source<Integer,NotUsed> source){
  return source.runForeach(System.out::println,  actorSystem)                .thenRun(() -> actorSystem.terminate());}

public Source<Integer, NotUsed> getIntegerRangeSource(){
    return Source.range(1, 10);

Executing this code will will print the values in the stream from 1 to 10. There are number of operations that you can do as well. The important thing to notice here is the Source can be reusable , you can have another operations on this source and consume it .

These was just an introduction to Akka Streams we will explore more in upcoming post . You can check the code on github

One Reply to “Introduction To Akka Streams – Part 1”

Comments are closed.