How to manage large amounts of data with akka streams

24
How to manage large amounts of data with Akka Streams by Ihor Mielientiev

Transcript of How to manage large amounts of data with akka streams

Page 1: How to manage large amounts of data with akka streams

How to manage large amounts of data with

Akka Streamsby Ihor

Mielientiev

Page 2: How to manage large amounts of data with akka streams

Agenda1. Reactive Streams2. Backpressure3. Handling big amount of data (Demos)

Page 3: How to manage large amounts of data with akka streams

Reactive Streamshttp://www.reactive-streams.org/

Page 4: How to manage large amounts of data with akka streams

Reactive Streams: Goals

1. Backpressured asynchronous stream processing2. Standard interfaces

Page 5: How to manage large amounts of data with akka streams

Reactive StreamsReactive Streams is a standard and specification for Stream-oriented libraries for the JVM thatprocess a potentially unbounded number of elementsasynchronously passing elements between componentswith mandatory non-blocking backpressure.

Page 6: How to manage large amounts of data with akka streams

Reactive Streams: ImplementationsAkka Streams @ Lightbend (Typesafe)

Reactor @ Pivotal (Spring 5 will adopt “reactive” programming)

RxJava @ Netflix

Vert.x @ RedHat

Ratpack

Slick 3.0

MongoDB

Reactive Rabbit (driver)

Open JDK 9 - Doug Lea @ Oracle

etc.

Page 7: How to manage large amounts of data with akka streams

Reactive Streams (interfaces)

Page 8: How to manage large amounts of data with akka streams

Backpressure

Page 9: How to manage large amounts of data with akka streams

What is backpressure?

Page 10: How to manage large amounts of data with akka streams

What is backpressure?

Page 11: How to manage large amounts of data with akka streams

What is backpressure?

Page 12: How to manage large amounts of data with akka streams

Solutions1.Just Increase Buffer Size :)

2.Bounded buffer (drop messages + require resending)

3.Negative ACK (tell Publisher to slow down)4.speed(publisher) < speed(subscriber) (Dynamic Push/Pull)

Page 13: How to manage large amounts of data with akka streams
Page 14: How to manage large amounts of data with akka streams

Handling big files (HTTP)In many cases the request stored in memory

(gather/parse http request and then analyzing)It’s a bad, bad Idea to put the body in memory. (Try

to upload 10 GB file and store it)

Page 15: How to manage large amounts of data with akka streams

Classic Java Stream Management

Page 16: How to manage large amounts of data with akka streams
Page 17: How to manage large amounts of data with akka streams

ProblemsLow Performance (if no buffer)

Buffer Issues (buffer management/overflow/...)

Error Handling

Mixing IO management with business logic

Not Modular

Synchronous (Blocking)

Ugly Code

Page 18: How to manage large amounts of data with akka streams

Better Way: Akka Streams

Data Creator (Producer)

Data ConsumerPipe and transfor data (Connector)

Page 19: How to manage large amounts of data with akka streams
Page 20: How to manage large amounts of data with akka streams

Akka StreamsPure FunctionalComposableType Safe

Page 21: How to manage large amounts of data with akka streams

Let’s manage HTTP body stream with Akka Stream/HTTP

Page 22: How to manage large amounts of data with akka streams

Process (Upload)1. File is post by client (browser etc.)2. Normalize stream (chunks)3. Async save each chunk in K/V storage (Amazon S3)4. Store metadata

Page 23: How to manage large amounts of data with akka streams

Process (Download)1. Get a fileId from client2. Stream each chunk to client :)

Page 24: How to manage large amounts of data with akka streams

Questions?