Streaming Api Design with Akka, Scala and Spray

40
Streaming Api Design with

description

An introduction on how to design APIs, followed by a recipe using scala, and a REST server implementation in Akka and Spray. This presentation looks at API design from two different angles. Firstly, from a API developer/user point of view, we define the API developer journey, we introduce and craft meaningful REST url paths and RESTful resources. Secondly, from an API implementation point of view, we dive into the mechanics of akka, and spray. During the first part we highlight the process of value creation from idea to API design, which translate into the definition of fluent API paths where resources are concatenated, rendering in facts the elements of a streaming analytical pipeline into a set of concatenated resources accessible via the api. We highlight the importance of a meaningful semantic for the API design process and a good match between the value proposition and the resources exposed by the api itself. To render the concept clearer we take the example of a streaming api which collects and gathers readings from a set of jogging wrist bands connected to internet. During the second part of the presentation, we focus on scalability, resiliency and availability of the solution. We introduce the concept of actor as basic processing element. We introduce the concept of location transparencies and hierarchical supervision when mapping actors to a cluster of computing resources. Finally we describe how to translate a url path into a set of routes, and how to dispatch and process the incoming json readings to the right analytical actors. This short overview gives an idea on the mechanics behind a real time, streaming api. And on how to implement api resources as asynchronous communicating actors, in order to provide a rudimental but effective streaming analytical engine.

Transcript of Streaming Api Design with Akka, Scala and Spray

Page 1: Streaming Api Design with Akka, Scala and Spray

Streaming Api Design with

Page 2: Streaming Api Design with Akka, Scala and Spray
Page 3: Streaming Api Design with Akka, Scala and Spray
Page 4: Streaming Api Design with Akka, Scala and Spray

By Gruban / Patrick Gruban from Munich, Germany (originally posted to Flickr as IMG_9038) [CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0)], via Wikimedia Commons

Page 5: Streaming Api Design with Akka, Scala and Spray

Define an API that ...given a collection of wristband data from

a bunch of runners

... ...

{ "heart": 108, "name" : "Judy"}

...

Page 6: Streaming Api Design with Akka, Scala and Spray

Define an API that ...given a collection of wristband data from

a bunch of runners

{ "Fred": 125, "Judy": 110}

... ...

Aggregates the data,

calculate max heart rate each second

grouped by name ...

{ "heart": 108, "name" : "Judy"}

...

Page 7: Streaming Api Design with Akka, Scala and Spray

Let’s design this!Let’s create a stream ...

POST /api/streams

Page 8: Streaming Api Design with Akka, Scala and Spray

Let’s design this!Let’s create a stream ...

GET /api/streams/1

{

"id": 1,

"links": {

"uri": "/api/streams/1",

"input": "/api/streams/1/in",

"filters": "/api/streams/1/in/filters"

}

}

Page 9: Streaming Api Design with Akka, Scala and Spray

Let’s design this!Given a stream id, lets create a filter,

that gives the highest heart rate of the group,

in a window of 1 seconds ...

POST /api/streams/1/in/filters

{ "resolution": 1, "field" : "heart", "transform" : "max" "group_by" : "name"}'

Page 10: Streaming Api Design with Akka, Scala and Spray

Let’s design this!Given a stream id, lets create a filter,

that gives the highest heart rate of the group,

in a window of 1 seconds ...

GET /api/streams/1/in/filters/1

Page 11: Streaming Api Design with Akka, Scala and Spray

Let’s design this!Now I can feed running data in ...

POST /api/streams/1/in

{ "heart": 115, "name" : "Judy"}

Page 12: Streaming Api Design with Akka, Scala and Spray

Let’s design this!And get the filtered data out ...

GET /api/streams/1/in/filtered_by/1/out

{ "Fred": 121, "Judy": 110}

Page 13: Streaming Api Design with Akka, Scala and Spray

This API is fluent … nice.

/api/streams/1/in

/api/streams/1/in/filtered_by/1/out

Page 14: Streaming Api Design with Akka, Scala and Spray

Akka: why?●

Page 15: Streaming Api Design with Akka, Scala and Spray

Akka: asynchronous

Page 17: Streaming Api Design with Akka, Scala and Spray

Akka: asynchronous●

Page 18: Streaming Api Design with Akka, Scala and Spray

Akka: actors

Page 19: Streaming Api Design with Akka, Scala and Spray

Akka: actorsclass HeartRateActor extends Actor {

var heart_rate = 0.0

def receive = {

case value: Double =>

heart_rate = max(heart_rate, value)

case "get" =>

sender ! heart_rate

}

}

Page 20: Streaming Api Design with Akka, Scala and Spray

Actors as Resources

/api/streams

Streams Actor

POST

def receive = { case CreateStream => val stream = system.actorOf(StreamActor(id), s"stream-$id")

Page 21: Streaming Api Design with Akka, Scala and Spray

Actors as Resources

/api/streams/1

Streams Actor Stream-1 Actor

Page 22: Streaming Api Design with Akka, Scala and Spray

Actors as Resources

/api/streams/1/in/filters/1

Streams Actor Stream-1 Actor Filter-1 Actor

Page 23: Streaming Api Design with Akka, Scala and Spray

Actors as Resources

/api/streams/1/in/filters/2

Page 24: Streaming Api Design with Akka, Scala and Spray

Actors as Resources

/api/streams/2/in/filters/1

Page 26: Streaming Api Design with Akka, Scala and Spray

Actor as Scalable Resources

jvm: This is where my actors run

Page 27: Streaming Api Design with Akka, Scala and Spray

Actor as Scalable Resources

jvm: This is where my actors run

Page 28: Streaming Api Design with Akka, Scala and Spray

Actor as Scalable Resources

Many jvm’s, cores, nodes, racks

jvm: This is where my actors run

Page 29: Streaming Api Design with Akka, Scala and Spray

Actor as Scalable Resources

Page 30: Streaming Api Design with Akka, Scala and Spray

Actors as Scalable Resources

Page 31: Streaming Api Design with Akka, Scala and Spray

Where is my http port?

Page 32: Streaming Api Design with Akka, Scala and Spray

Spray: HTTP toolkit based on Akka

Page 33: Streaming Api Design with Akka, Scala and Spray

Spray: HTTP toolkit based on Akka

Page 34: Streaming Api Design with Akka, Scala and Spray

Spray: Reactive and Scalable

Page 35: Streaming Api Design with Akka, Scala and Spray

Spray: Reactive and Scalable

Page 36: Streaming Api Design with Akka, Scala and Spray

Spray: Routing val route = { pathPrefix("api/streams" / IntNumber) { id => { get { ctx => (coreActor ? Get(id) ).mapTo[Int] .onSuccess { resource => complete(resource) } }

} ~ someOtherRoute } }

Page 37: Streaming Api Design with Akka, Scala and Spray

Spray: Web API get the output

Page 38: Streaming Api Design with Akka, Scala and Spray

APIs: back to back connections

Page 40: Streaming Api Design with Akka, Scala and Spray