SlideShare a Scribd company logo
A dive into Akka Streams
From the basics to a real-world scenario
Gioia Ballin, Simone Fabiano - SW developers
Who are these guys?
Gioia Ballin
Twitter / @GioiaBallin
Simone Fabiano
Twitter / @mone_tpatpc
R&D Data Analysis Microservices Infrastructure WebBlog
Agenda
The Problem
Akka Streams Basics
The Collector Service
Recap and Learnings
Data processing pipeline
COLLECTOR SVC AMAZON KINESIS
WI-FI/3GSENSORS
The Collector Service: two months ago...
AMAZON
KINESIS
INBOX
ACTOR
TCP
Kinesis Client
INBOX
ACTOR Kinesis Client
INBOX
ACTOR Kinesis Client
TCP
TCP
chunks
chunks
chunks
events
events
events
put
put
put
A dive into akka streams: from the basics to a real-world scenario
Data may be lost
COLLECTOR SVC AMAZON KINESIS
WI-FI/3GSENSORS
CONNECTION
ISSUES
MESSAGE
OVERLOAD
Not REALLY reactive: why?
Lack of...
RECOVERY MECHANISMS
BACKPRESSURE
Recovery mechanisms
Upload failure? Retry
Kinesis unavailable? Buffer
Ingestion speed under control
Take advantage of sensors buffer
Sensors switch from busier to less busy services
Welcome Akka Streams!
Backpressure on the network
+
Easy fit for stream paradigm
Typed flow & Increased Readability
Akka Streams Basics - 3 2 1 Go!
SOURCE FLOW SINK
val source = Source(1 to 42)
val flow = Flow[Int].map(_ + 1)
val sink = Sink.foreach(println)
val graph = source.via(flow).to(sink)
graph.run()
Flows are collections (almost)
OK map, filter, fold, …
No flatMap! (there’s mapConcat though)
Extras mapAsync, mapAsyncUnordered, buffer..
How backpressure works
Source Flow Sink
pull
pull
push
push
Collector with Akka Streams
path("data") {
post {
extractRequest { request =>
val source = request.entity.dataBytes
val flow = processing()
val sink = Sink.ignore
source.via(flow).runWith(sink)
… } }
Flow[ByteString]
.via(chunkBuffer)
Transformation tools: Framing.delimiter
val chunkBuffer = Framing.delimiter(
ByteString("n"),
maxPresenceEventBytes,
false
)
.map(_.dropRight(1))
.map(_.utf8String)
Flow[ByteString]
.via(chunkBuffer)
.via(presenceEventFactory.asStream)
Flow[ByteString]
.via(chunkBuffer)
.via(presenceEventFactory.asStream)
.via(persistentBuffer)
class PersistentBuffer[A] (...)
extends GraphStage[FlowShape[A, A]] {
val in =
Inlet[A]("PersistentBuffer.in")
val out =
Outlet[A]("PersistentBuffer.out")
override val shape = FlowShape.of(in,out)
When the available stages are not enough, write your own
Custom stages: State
override def createLogic(
attr: Attributes
): GraphStageLogic =
new GraphStageLogic(shape) {
var state: StageState[A] = initialState[A]
val cb = getAsyncCallback[Try[A]] {
case Success(elements:A) =>
state = state.copy(...)
	 ...
}
queue.getCurrentQueue
.onComplete(cb.invoke)
Custom Stages: ports
setHandler(in, new InHandler {
override def onPush() = {
val element = grab(in)
pull(in)
...
}
...
})
setHandler(out, new OutHandler {
override def onPull(): Unit = {
push(out, something)
}
...
})
Custom Stages: Start and stop
override def postStop(): Unit = {
...
}
override def preStart(): Unit = {
...
}
Flow[ByteString]
.via(chunkBuffer)
.via(presenceEventFactory.asStream)
.via(persistentBuffer)
.via(kinesisPublisher)
Delegating work to actors
Flow[KinesisEvent]
.mapAsync(1) { event =>
(publisher ? Publish(event))
.mapTo[PublishCompleted]
}
Flow[KinesisEvent]
.mapAsync(1) { event =>
(publisher ? Publish(event))
.mapTo[Future[PublishCompleted]]
.flatten
}
Flow[ByteString]
.via(chunkBuffer)
.via(presenceEventFactory.asStream)
.via(persistentBuffer)
.via(kinesisPublisher)
.alsoTo(countEvents)
Extra side-effects
val countEvents = Sink.foreach[Event] { _ =>
metricEventsPerTimeUnitOfSensor.mark()
metricEventsPerTimeUnit.mark()
}
Automatic Fusion and Async Stages
A stream is handled by 1 thread
NO CROSS-THREAD COMMUNICATIONS
NO PARALLELISM
FASTER CODE!
SLOWER CODE!
A boundary will split your stream on different threads
source
.via(doSomething).async
.via(doSomething).async
.runWith(Sink.foreach(println))
Materialized Values
val sink: Sink[Any, Future[Done]] = Sink.ignore
.runWith(sink) is a shortcut for .toMat(sink)(Keep.Right).run
STREAM RUN
Obtain a materialized
value per each stage
Materialized Values Example: TestKit
val myFlow = Flow[String].map { v => v.take(1) }
val (pub, sub) = TestSource.probe[String]
.via(myFlow)
.toMat(TestSink.probe[Any])(Keep.both)
.run()
sub.request(1)
pub.sendNext("Gathering")
sub.expectNext("G")
Stream Ending: Supervisioning
val flow = otherFlow
.withAttributes
ActorAttributes.supervisionStrategy {
case _ => Supervision.resume
}
)
Stream Ending: Supervisioning
RESUME
RESET
STOP
> drop failing elem
> keep going
> drop failing elem
> reset stage state
> keep going
fail all the things!
Other shapes
FAN OUT FAN IN WHATEVER
Finally Got Reactive!
In two months to production...
Memory Leaks
Data Losses
Backpressure on the network
Recovery mechanisms
Key learnings
Actors aren’t sufficient to be reactive
Backpressure as a building block
Akka Streams API is rich
THANK YOU!
Sleep more worry less

More Related Content

PPTX
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka...
PDF
Journey into Reactive Streams and Akka Streams
PDF
Streaming all the things with akka streams
PDF
Reactive Streams / Akka Streams - GeeCON Prague 2014
PDF
Resilient Applications with Akka Persistence - Scaladays 2014
PDF
Reactive Stream Processing with Akka Streams
PDF
Fresh from the Oven (04.2015): Experimental Akka Typed and Akka Streams
PDF
VJUG24 - Reactive Integrations with Akka Streams
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka...
Journey into Reactive Streams and Akka Streams
Streaming all the things with akka streams
Reactive Streams / Akka Streams - GeeCON Prague 2014
Resilient Applications with Akka Persistence - Scaladays 2014
Reactive Stream Processing with Akka Streams
Fresh from the Oven (04.2015): Experimental Akka Typed and Akka Streams
VJUG24 - Reactive Integrations with Akka Streams

What's hot (20)

PDF
Scala usergroup stockholm - reactive integrations with akka streams
PDF
Distributed Real-Time Stream Processing: Why and How 2.0
PDF
2014 akka-streams-tokyo-japanese
PDF
Build Real-Time Streaming ETL Pipelines With Akka Streams, Alpakka And Apache...
PDF
Reactive programming on Android
PPTX
Asynchronous Orchestration DSL on squbs
PDF
Akka Streams and HTTP
PDF
Akka streams
PDF
Akka streams - Umeå java usergroup
PDF
Async - react, don't wait - PingConf
PDF
Asynchronous stream processing with Akka Streams
PDF
[Tokyo Scala User Group] Akka Streams & Reactive Streams (0.7)
PDF
HBase RowKey design for Akka Persistence
PDF
Akka persistence == event sourcing in 30 minutes
PDF
Building Scalable Stateless Applications with RxJava
PDF
Reactive programming with RxJava
PDF
Reactive stream processing using Akka streams
PDF
Reactive streams processing using Akka Streams
PPT
Spark stream - Kafka
PPTX
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Scala usergroup stockholm - reactive integrations with akka streams
Distributed Real-Time Stream Processing: Why and How 2.0
2014 akka-streams-tokyo-japanese
Build Real-Time Streaming ETL Pipelines With Akka Streams, Alpakka And Apache...
Reactive programming on Android
Asynchronous Orchestration DSL on squbs
Akka Streams and HTTP
Akka streams
Akka streams - Umeå java usergroup
Async - react, don't wait - PingConf
Asynchronous stream processing with Akka Streams
[Tokyo Scala User Group] Akka Streams & Reactive Streams (0.7)
HBase RowKey design for Akka Persistence
Akka persistence == event sourcing in 30 minutes
Building Scalable Stateless Applications with RxJava
Reactive programming with RxJava
Reactive stream processing using Akka streams
Reactive streams processing using Akka Streams
Spark stream - Kafka
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Ad

Similar to A dive into akka streams: from the basics to a real-world scenario (20)

PPT
Spark streaming with kafka
PDF
Job Queue in Golang
PDF
GTS Episode 1: Reactive programming in the wild
PDF
MongoDB World 2019: Life In Stitch-es
PDF
The Art of The Event Streaming Application: Streams, Stream Processors and Sc...
PPTX
Kakfa summit london 2019 - the art of the event-streaming app
PDF
Building a High-Performance Database with Scala, Akka, and Spark
PDF
Building Stateful Microservices With Akka
PDF
Practical RxJava for Android
PDF
Reactive Streams 1.0 and Akka Streams
PDF
Apache Flink @ Tel Aviv / Herzliya Meetup
PDF
Actors or Not: Async Event Architectures
PDF
Azure Durable Functions (2019-04-27)
PDF
Maxim Salnikov - Service Worker: taking the best from the past experience for...
PDF
Anton Moldovan "Load testing which you always wanted"
PPT
Gearmanpresentation 110308165409-phpapp01
PDF
Online Meetup: Why should container system / platform builders care about con...
PDF
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
PDF
Oracle Drivers configuration for High Availability
ODP
Concurrency on the JVM
Spark streaming with kafka
Job Queue in Golang
GTS Episode 1: Reactive programming in the wild
MongoDB World 2019: Life In Stitch-es
The Art of The Event Streaming Application: Streams, Stream Processors and Sc...
Kakfa summit london 2019 - the art of the event-streaming app
Building a High-Performance Database with Scala, Akka, and Spark
Building Stateful Microservices With Akka
Practical RxJava for Android
Reactive Streams 1.0 and Akka Streams
Apache Flink @ Tel Aviv / Herzliya Meetup
Actors or Not: Async Event Architectures
Azure Durable Functions (2019-04-27)
Maxim Salnikov - Service Worker: taking the best from the past experience for...
Anton Moldovan "Load testing which you always wanted"
Gearmanpresentation 110308165409-phpapp01
Online Meetup: Why should container system / platform builders care about con...
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
Oracle Drivers configuration for High Availability
Concurrency on the JVM
Ad

Recently uploaded (20)

PDF
Upgrade and Innovation Strategies for SAP ERP Customers
DOCX
The Five Best AI Cover Tools in 2025.docx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
ai tools demonstartion for schools and inter college
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
medical staffing services at VALiNTRY
PDF
top salesforce developer skills in 2025.pdf
PPT
JAVA ppt tutorial basics to learn java programming
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Transform Your Business with a Software ERP System
PDF
System and Network Administration Chapter 2
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Upgrade and Innovation Strategies for SAP ERP Customers
The Five Best AI Cover Tools in 2025.docx
Design an Analysis of Algorithms II-SECS-1021-03
ai tools demonstartion for schools and inter college
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Migrate SBCGlobal Email to Yahoo Easily
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
medical staffing services at VALiNTRY
top salesforce developer skills in 2025.pdf
JAVA ppt tutorial basics to learn java programming
Operating system designcfffgfgggggggvggggggggg
Transform Your Business with a Software ERP System
System and Network Administration Chapter 2
2025 Textile ERP Trends: SAP, Odoo & Oracle
ISO 45001 Occupational Health and Safety Management System
Design an Analysis of Algorithms I-SECS-1021-03
history of c programming in notes for students .pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

A dive into akka streams: from the basics to a real-world scenario