Practical Akka HTTP - introduction

Łukasz Sowa
Łukasz SowaCo-founder, managing partner at Iterators
ITERATORSI T E R AT O R S @luksow
Practical Akka HTTP
introduction
Łukasz Sowa, XI Tricity Scala User Group
I T E R AT O RITERATORSI T E R AT O R S @luksow
Hi, I'm Łukasz
● Co-founder, dev @ Iterators (https://iterato.rs)
● Highly concurrent & distributed systems (MSA)
● ScalaWAW organizer (please attend!)
● Pizza, beer & football lover
● http://luksow.com
● contact@luksow.com
● @luksow
ITERATORSI T E R AT O R S @luksow
What's in it for you?
● Learn
– What Akka HTTP is
– Key architectural concepts
– When & how to use it
– How does it fit in the Scala ecosystem
● Takeaways
– Beginners: ability to start using Akka HTTP
– Advanced: solid foundations for more exploring
I T E R AT O RITERATORSI T E R AT O R S @luksow
Akka HTTP basics
boring facts
ITERATORSI T E R AT O R S @luksow
Akka HTTP – in definition
● Part of Akka project
● HTTP toolkit (server- and client-side)
● On top of akka-actor & akka-stream
● Streams, reactive, backpressure, blah!
● Not a framework – focus: integration layers
● Multiple levels of abstraction (open API)
● Scalable; max throughput, acceptable latency
● Both for Java & Scala
ITERATORSI T E R AT O R S @luksow
Akka HTTP – in implementation
● Fully asynchronous & non-blocking
● Actor-friendly but focused on higher-level
API
● Lightweight
● Modular, testable
● Based on mature spray.io
ITERATORSI T E R AT O R S @luksow
Akka HTTP – in dates
● Spray.io (2011-03 – 2015-06 ✝?)
– Bad chunked-requests support
– Missing features (ex. websockets)
– Hard to develop & debug
● Spray.io acquired by Typesafe (2013-10)
● First preview - 0.4 (2014-06)
– Very experimental, practically unusable
● 'Good enough for evaluation and production' – 1.0 (2015-07)
– No, not really, still very experimental and lacking
● Five months later - 2.0 (2015-12)
– Oh sorry, we changed some interfaces, have a nice migration!
● Akka 2.4.2-RC3 (2016-02)
– Akka HTTP included in main Akka project, only 'half experimental'
ITERATORSI T E R AT O R S @luksow
Akka HTTP – in parts
● akka-http-core
– Low-level server & client-side HTTP implementation
● akka-http
– High-level API: (un)marshalling, (de)compression, DSL
● akka-http-testkit
– Utilities for testing server-side implementation
● akka-http-spray-json
– Glue-code for (de)serialization from/to JSON with spray-json
● akka-http-xml
– Glue-code for (de)serialization from/to XML with scala-xml
I T E R AT O RITERATORSI T E R AT O R S @luksow
Inside Akka HTTP
HTTP model, server, DSL,
client, (un)marshalling, testkit
I T E R AT O RITERATORSI T E R AT O R S @luksow
HTTP model
● Fully immutable, case-class-based
● Abstraction for most HTTP things (types!)
● Little logic inside
● Lots of predefined types – common media type,
status codes, encodings etc.
● Open for extensions
● Still very efficient parsing & rendering
● Drawback? Your clients and connected services have
to obey the standard (and bugs happen too!)
I T E R AT O RITERATORSI T E R AT O R S @luksow
HTTP model – examples (1)
I T E R AT O RITERATORSI T E R AT O R S @luksow
HTTP model - examples (2)
I T E R AT O RITERATORSI T E R AT O R S @luksow
HTTP model - examples (3)
I T E R AT O RITERATORSI T E R AT O R S @luksow
Serving – the streams way
I T E R AT O RITERATORSI T E R AT O R S @luksow
Serving – the functional way
I T E R AT O RITERATORSI T E R AT O R S @luksow
Serving – the hard way
Flow[ByteString, ByteString, _]
I T E R AT O RITERATORSI T E R AT O R S @luksow
Serving – example
Full example: https://github.com/theiterators/cors-buster
I T E R AT O RITERATORSI T E R AT O R S @luksow
Serving – the nice way
I T E R AT O RITERATORSI T E R AT O R S @luksow
Routing DSL
● Internal domain specific language for
routing
● How most services are actually written
● Layer to the application (business logic)
● Type safe but flexible
● Not just routing – behaviour definition
● Very composable
● Fun, powerful and looks sexy!
I T E R AT O RITERATORSI T E R AT O R S @luksow
Routing DSL - example
I T E R AT O RITERATORSI T E R AT O R S @luksow
Routing DSL – directives (1)
● type Route = RequestContext ⇒
Future[RouteResult]
● abstract class Directive[L]
● def complete(m: ⇒
ToResponseMarshallable): StandardRoute
● def reject(rejections: Rejection*):
StandardRoute
● def fail(error: Throwable):
Future[RouteResult]
I T E R AT O RITERATORSI T E R AT O R S @luksow
Routing DSL – directives (2)
● ~136 predefined directives
● Roles: extracting, filtering, transforming
request or response, side-effecting
● Create your own by composing (| and &
operators) or writing from scratch
● Beaware: it takes time to fully understand
how directives work
I T E R AT O RITERATORSI T E R AT O R S @luksow
Routing DSL – custom directive
I T E R AT O RITERATORSI T E R AT O R S @luksow
Routing DSL – rejections
● Rejections
– Are produced by directives
– Travel down the routing structure
– Can be cancelled
– Must be handled if no route completes request
– Can be extended
– trait RejectionHandler extends (immutable.Seq[Rejection] ⇒
Option[Route])
– Handling rejection = transforming it into response (probably
with some error code etc.)
– Ex. MethodRejection, AuthorizationFailedRejection,
MissingCookieRejection, MissingQueryParamRejection
I T E R AT O RITERATORSI T E R AT O R S @luksow
Routing DSL – failures
● Failures (or exceptions)
– Are triggered by exceptions or fail(...)
– Travel up the routing structre
– Can be handled by handleExceptions
directive or top-level ExceptionHandler
– trait ExceptionHandler extends
PartialFunction[Throwable, Route]
– Can be used to simplify flow (not very nice) –
ex. for validation
I T E R AT O RITERATORSI T E R AT O R S @luksow
Client
● Connection-level client
– Full control over connection and
request/response scheduling
● Host-level client
– Akka manages connection pool to one
specific host
● Request-level
– Akka performs all connection management
I T E R AT O RITERATORSI T E R AT O R S @luksow
Connection-level client
● Stream-based
● Enables
– HTTP persistent connection
– HTTP pipelining
I T E R AT O RITERATORSI T E R AT O R S @luksow
Host-level client
● Stream-based
● Some complexities around pool
configuration and materialization
I T E R AT O RITERATORSI T E R AT O R S @luksow
Request-level client
● Super easy to use
● What you usually need
● Notice request-building DSL!
I T E R AT O RITERATORSI T E R AT O R S @luksow
(Un)marshalling (1)
● Marshalling – high level → low (wire) level
– ToEntityMarshaller[T]
– ToHeadersAndEntityMarshaller[T]
– ToResponseMarshaller[T]
– ToRequestMarshaller[T]
● Unmarshalling – low (wire) level → high level
– FromEntityUnmarshaller[T]
– FromMessageUnmarshaller[T]
– FromResponseUnmarshaller[T]
– FromRequestUnmarshaller[T]
– FromStringUnmarshaller[T]
– FromStrictFormFieldUnmarshaller[T]
I T E R AT O RITERATORSI T E R AT O R S @luksow
(Un)marshalling (2)
● Many predefined (un)marshallers (also for
generic types)
● Extensible
● JSON and XML support in additional
packages
● Type-class approach – implicit resolution
● Cryptic error messages
I T E R AT O RITERATORSI T E R AT O R S @luksow
Testkit
● Very simple and straightforward
● Allows to assert responses returned for
given requests
● Integrates well with specs2, ScalaTest
I T E R AT O RITERATORSI T E R AT O R S @luksow
Everyday Akka HTTP
some additional remarks
I T E R AT O RITERATORSI T E R AT O R S @luksow
Akka HTTP in Scala world (1)
● Again: it's not a framework
● Not a competition for Play
– Akka HTTP is going to be Play's (2.5.X?)
backend
● Competition: Spray.io, Finagle, Scalatra,
Unfiltered?
● Backed by Lightbend (Typesafe)
I T E R AT O RITERATORSI T E R AT O R S @luksow
Akka HTTP in Scala world (2)
● Where it excels
– Integration layers
– Microservices
– Pure REST APIs
● Where it falls short
– Fully-fledged web applications (with server-
side template generation)
– IMO still lacks maturity
I T E R AT O RITERATORSI T E R AT O R S @luksow
Notable community projects
● https://github.com/softwaremill/akka-http-session
● https://github.com/hseeberger/akka-sse
● https://github.com/hseeberger/akka-http-json
● Not too many
● So roll up your sleeves! :)
● But integrates seamlessly with almost anything
● Lots of educational projects exist
I T E R AT O RITERATORSI T E R AT O R S @luksow
Performance
● Spray.io's performance was impressive
– 750K req/sec via Twitter
– More benchmarks by TechEmpower
● Akka HTTP 1.0 was roughly 10x slower
● Akka HTTP 2.4.2-RC2 75% of Spray's perf
– “this is not the end of the performance work,
we have only just begun”
● But those benchmarks, you know…
I T E R AT O RITERATORSI T E R AT O R S @luksow
Conclusions
tldr;
I T E R AT O RITERATORSI T E R AT O R S @luksow
What was skipped?
● Streams stuff (persistent connections,
pipelining, backpressure etc.)
● Low-level internals
● SSL/TLS support
● Very nice Websocket support
● Probably bunch of other important things
I T E R AT O RITERATORSI T E R AT O R S @luksow
What to remember?
● Akka HTTP is (probably) the hottest Scala HTTP toolkit
● Built on very solid foundations
● Features all the building blocks needed for well-
designed HTTP services
● Provides both low- and high-level (DSLs!) interfaces
for almost everything
● Reactive streams included
● Still lacks maturity
● Easy to start with but hard to master
● Fun to use!
I T E R AT O RITERATORSI T E R AT O R S @luksow
How to start?
● Start coding right away
● Have fun
● Discover best practices along the way
● http://doc.akka.io/docs/akka-stream-and-http-experimental/current/scala/http/index.html
● http://doc.akka.io/api/akka-stream-and-http-experimental/2.0.3/
● https://github.com/theiterators/akka-http-microservice + tutorial
● https://github.com/theiterators/reactive-microservices + tutorial
● Lightbend Activator – search akka-http
● https://groups.google.com/forum/#!forum/akka-user
● https://gitter.im/akka/akka
● Sign up for a newsletter on http://luksow.com to get notified about big tutorial I'm preparing
I T E R AT O RITERATORSI T E R AT O R S @luksow
Thanks!
● Łukasz Sowa
● https://iterato.rs
● http://luksow.com
● contact@luksow.com
● @luksow
Questions?
1 of 42

Recommended

scalaphx-akka-http by
scalaphx-akka-httpscalaphx-akka-http
scalaphx-akka-httpTerry Drozdowski
330 views18 slides
Building scalable rest service using Akka HTTP by
Building scalable rest service using Akka HTTPBuilding scalable rest service using Akka HTTP
Building scalable rest service using Akka HTTPdatamantra
3.7K views46 slides
An Introduction to Akka http by
An Introduction to Akka httpAn Introduction to Akka http
An Introduction to Akka httpKnoldus Inc.
2.9K views23 slides
Akka Http , Routes, Streams with Scala by
Akka Http , Routes, Streams with ScalaAkka Http , Routes, Streams with Scala
Akka Http , Routes, Streams with ScalaJerry Kuru
1.9K views12 slides
Akka http by
Akka httpAkka http
Akka httpKnoldus Inc.
1.9K views20 slides
Akka Streams and HTTP by
Akka Streams and HTTPAkka Streams and HTTP
Akka Streams and HTTPRoland Kuhn
41.6K views58 slides

More Related Content

What's hot

Display earthquakes with Akka-http by
Display earthquakes with Akka-httpDisplay earthquakes with Akka-http
Display earthquakes with Akka-httpPierangelo Cecchetto
511 views26 slides
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka... by
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka...Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka...
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka...Reactivesummit
2.1K views26 slides
Understanding Akka Streams, Back Pressure, and Asynchronous Architectures by
Understanding Akka Streams, Back Pressure, and Asynchronous ArchitecturesUnderstanding Akka Streams, Back Pressure, and Asynchronous Architectures
Understanding Akka Streams, Back Pressure, and Asynchronous ArchitecturesLightbend
23.6K views65 slides
How to manage large amounts of data with akka streams by
How to manage large amounts of data with akka streamsHow to manage large amounts of data with akka streams
How to manage large amounts of data with akka streamsIgor Mielientiev
727 views24 slides
Asynchronous Orchestration DSL on squbs by
Asynchronous Orchestration DSL on squbsAsynchronous Orchestration DSL on squbs
Asynchronous Orchestration DSL on squbsAnil Gursel
525 views28 slides
Akka streams scala italy2015 by
Akka streams scala italy2015Akka streams scala italy2015
Akka streams scala italy2015mircodotta
2K views58 slides

What's hot(20)

Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka... by Reactivesummit
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka...Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka...
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Ka...
Reactivesummit2.1K views
Understanding Akka Streams, Back Pressure, and Asynchronous Architectures by Lightbend
Understanding Akka Streams, Back Pressure, and Asynchronous ArchitecturesUnderstanding Akka Streams, Back Pressure, and Asynchronous Architectures
Understanding Akka Streams, Back Pressure, and Asynchronous Architectures
Lightbend23.6K views
How to manage large amounts of data with akka streams by Igor Mielientiev
How to manage large amounts of data with akka streamsHow to manage large amounts of data with akka streams
How to manage large amounts of data with akka streams
Igor Mielientiev727 views
Asynchronous Orchestration DSL on squbs by Anil Gursel
Asynchronous Orchestration DSL on squbsAsynchronous Orchestration DSL on squbs
Asynchronous Orchestration DSL on squbs
Anil Gursel525 views
Akka streams scala italy2015 by mircodotta
Akka streams scala italy2015Akka streams scala italy2015
Akka streams scala italy2015
mircodotta2K views
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Kafka by Akara Sucharitakul
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & KafkaBack-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Kafka
Back-Pressure in Action: Handling High-Burst Workloads with Akka Streams & Kafka
Akara Sucharitakul931 views
Specs2 whirlwind tour at Scaladays 2014 by Eric Torreborre
Specs2 whirlwind tour at Scaladays 2014Specs2 whirlwind tour at Scaladays 2014
Specs2 whirlwind tour at Scaladays 2014
Eric Torreborre2.5K views
Reactive Stream Processing with Akka Streams by Konrad Malawski
Reactive Stream Processing with Akka StreamsReactive Stream Processing with Akka Streams
Reactive Stream Processing with Akka Streams
Konrad Malawski14.2K views
Serverless Event Streaming with Pulsar Functions by StreamNative
Serverless Event Streaming with Pulsar FunctionsServerless Event Streaming with Pulsar Functions
Serverless Event Streaming with Pulsar Functions
StreamNative382 views
Build Real-Time Streaming ETL Pipelines With Akka Streams, Alpakka And Apache... by Lightbend
Build Real-Time Streaming ETL Pipelines With Akka Streams, Alpakka And Apache...Build Real-Time Streaming ETL Pipelines With Akka Streams, Alpakka And Apache...
Build Real-Time Streaming ETL Pipelines With Akka Streams, Alpakka And Apache...
Lightbend7.5K views
Asynchronous stream processing with Akka Streams by Johan Andrén
Asynchronous stream processing with Akka StreamsAsynchronous stream processing with Akka Streams
Asynchronous stream processing with Akka Streams
Johan Andrén3K views
Developing Secure Scala Applications With Fortify For Scala by Lightbend
Developing Secure Scala Applications With Fortify For ScalaDeveloping Secure Scala Applications With Fortify For Scala
Developing Secure Scala Applications With Fortify For Scala
Lightbend11.1K views
Developing distributed applications with Akka and Akka Cluster by Konstantin Tsykulenko
Developing distributed applications with Akka and Akka ClusterDeveloping distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka Cluster
2014 akka-streams-tokyo-japanese by Konrad Malawski
2014 akka-streams-tokyo-japanese2014 akka-streams-tokyo-japanese
2014 akka-streams-tokyo-japanese
Konrad Malawski25.5K views
VJUG24 - Reactive Integrations with Akka Streams by Johan Andrén
VJUG24  - Reactive Integrations with Akka StreamsVJUG24  - Reactive Integrations with Akka Streams
VJUG24 - Reactive Integrations with Akka Streams
Johan Andrén897 views
Building Distributed Systems in Scala by Alex Payne
Building Distributed Systems in ScalaBuilding Distributed Systems in Scala
Building Distributed Systems in Scala
Alex Payne35.5K views
Akka-chan's Survival Guide for the Streaming World by Konrad Malawski
Akka-chan's Survival Guide for the Streaming WorldAkka-chan's Survival Guide for the Streaming World
Akka-chan's Survival Guide for the Streaming World
Konrad Malawski5.4K views
Reactive integrations with Akka Streams by Konrad Malawski
Reactive integrations with Akka StreamsReactive integrations with Akka Streams
Reactive integrations with Akka Streams
Konrad Malawski1.9K views
Zoo keeper in the wild by datamantra
Zoo keeper in the wildZoo keeper in the wild
Zoo keeper in the wild
datamantra1.4K views

Viewers also liked

Securing Microservices using Play and Akka HTTP by
Securing Microservices using Play and Akka HTTPSecuring Microservices using Play and Akka HTTP
Securing Microservices using Play and Akka HTTPRafal Gancarz
8K views26 slides
Building a Reactive RESTful API with Akka Http & Slick by
Building a Reactive RESTful API with Akka Http & SlickBuilding a Reactive RESTful API with Akka Http & Slick
Building a Reactive RESTful API with Akka Http & SlickZalando Technology
13.6K views59 slides
Spring Boot Microservices vs Akka Actor Cluster by
Spring Boot Microservices vs Akka Actor Cluster Spring Boot Microservices vs Akka Actor Cluster
Spring Boot Microservices vs Akka Actor Cluster OpenCredo
21.9K views25 slides
Akka-http by
Akka-httpAkka-http
Akka-httpIosif Itkin
261 views32 slides
Akka HTTP by
Akka HTTPAkka HTTP
Akka HTTPTanUkkii
2.8K views34 slides
Akka in Practice: Designing Actor-based Applications by
Akka in Practice: Designing Actor-based ApplicationsAkka in Practice: Designing Actor-based Applications
Akka in Practice: Designing Actor-based ApplicationsNLJUG
28.7K views33 slides

Viewers also liked(20)

Securing Microservices using Play and Akka HTTP by Rafal Gancarz
Securing Microservices using Play and Akka HTTPSecuring Microservices using Play and Akka HTTP
Securing Microservices using Play and Akka HTTP
Rafal Gancarz8K views
Building a Reactive RESTful API with Akka Http & Slick by Zalando Technology
Building a Reactive RESTful API with Akka Http & SlickBuilding a Reactive RESTful API with Akka Http & Slick
Building a Reactive RESTful API with Akka Http & Slick
Zalando Technology13.6K views
Spring Boot Microservices vs Akka Actor Cluster by OpenCredo
Spring Boot Microservices vs Akka Actor Cluster Spring Boot Microservices vs Akka Actor Cluster
Spring Boot Microservices vs Akka Actor Cluster
OpenCredo21.9K views
Akka HTTP by TanUkkii
Akka HTTPAkka HTTP
Akka HTTP
TanUkkii2.8K views
Akka in Practice: Designing Actor-based Applications by NLJUG
Akka in Practice: Designing Actor-based ApplicationsAkka in Practice: Designing Actor-based Applications
Akka in Practice: Designing Actor-based Applications
NLJUG28.7K views
Play Framework: async I/O with Java and Scala by Yevgeniy Brikman
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
Yevgeniy Brikman108.6K views
Akka in Production - ScalaDays 2015 by Evan Chan
Akka in Production - ScalaDays 2015Akka in Production - ScalaDays 2015
Akka in Production - ScalaDays 2015
Evan Chan53.3K views
Scala, Akka, and Play: An Introduction on Heroku by Havoc Pennington
Scala, Akka, and Play: An Introduction on HerokuScala, Akka, and Play: An Introduction on Heroku
Scala, Akka, and Play: An Introduction on Heroku
Havoc Pennington16.8K views
Microservices in Scala - theory & practice by Łukasz Sowa
Microservices in Scala - theory & practiceMicroservices in Scala - theory & practice
Microservices in Scala - theory & practice
Łukasz Sowa1.6K views
Microservices 101: opportunities, dilemmas and problems by Łukasz Sowa
Microservices 101: opportunities, dilemmas and problemsMicroservices 101: opportunities, dilemmas and problems
Microservices 101: opportunities, dilemmas and problems
Łukasz Sowa1.8K views
The Cloud-natives are RESTless @ JavaOne by Konrad Malawski
The Cloud-natives are RESTless @ JavaOneThe Cloud-natives are RESTless @ JavaOne
The Cloud-natives are RESTless @ JavaOne
Konrad Malawski1K views
Reactive microservices with play and akka by scalaconfjp
Reactive microservices with play and akkaReactive microservices with play and akka
Reactive microservices with play and akka
scalaconfjp2K views
Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac... by Jonas Bonér
Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...
Akka: Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Ac...
Jonas Bonér54.3K views
Building Reactive Systems with Akka (in Java 8 or Scala) by Jonas Bonér
Building Reactive Systems with Akka (in Java 8 or Scala)Building Reactive Systems with Akka (in Java 8 or Scala)
Building Reactive Systems with Akka (in Java 8 or Scala)
Jonas Bonér28.1K views
Akka persistence == event sourcing in 30 minutes by Konrad Malawski
Akka persistence == event sourcing in 30 minutesAkka persistence == event sourcing in 30 minutes
Akka persistence == event sourcing in 30 minutes
Konrad Malawski40.1K views
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And... by Lightbend
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lessons Learned From PayPal: Implementing Back-Pressure With Akka Streams And...
Lightbend23K views
Microservices in Scala: Spray by Łukasz Sowa
Microservices in Scala: SprayMicroservices in Scala: Spray
Microservices in Scala: Spray
Łukasz Sowa1.9K views

Similar to Practical Akka HTTP - introduction

Reactive database access with Slick3 by
Reactive database access with Slick3Reactive database access with Slick3
Reactive database access with Slick3takezoe
5.2K views32 slides
Building REST API using Akka HTTP with Scala by
Building REST API using Akka HTTP with ScalaBuilding REST API using Akka HTTP with Scala
Building REST API using Akka HTTP with ScalaKnoldus Inc.
1.1K views26 slides
Build reliable, traceable, distributed systems with ZeroMQ by
Build reliable, traceable, distributed systems with ZeroMQBuild reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQRobin Xiao
4.1K views34 slides
Scala services in action by
Scala services in actionScala services in action
Scala services in actionUnderscore
411 views51 slides
Node.js streams talk by
Node.js streams talkNode.js streams talk
Node.js streams talkzladuric
1.6K views25 slides
From nothing to Prometheus : one year after by
From nothing to Prometheus : one year afterFrom nothing to Prometheus : one year after
From nothing to Prometheus : one year afterAntoine Leroyer
800 views38 slides

Similar to Practical Akka HTTP - introduction(20)

Reactive database access with Slick3 by takezoe
Reactive database access with Slick3Reactive database access with Slick3
Reactive database access with Slick3
takezoe5.2K views
Building REST API using Akka HTTP with Scala by Knoldus Inc.
Building REST API using Akka HTTP with ScalaBuilding REST API using Akka HTTP with Scala
Building REST API using Akka HTTP with Scala
Knoldus Inc.1.1K views
Build reliable, traceable, distributed systems with ZeroMQ by Robin Xiao
Build reliable, traceable, distributed systems with ZeroMQBuild reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQ
Robin Xiao4.1K views
Scala services in action by Underscore
Scala services in actionScala services in action
Scala services in action
Underscore411 views
Node.js streams talk by zladuric
Node.js streams talkNode.js streams talk
Node.js streams talk
zladuric1.6K views
From nothing to Prometheus : one year after by Antoine Leroyer
From nothing to Prometheus : one year afterFrom nothing to Prometheus : one year after
From nothing to Prometheus : one year after
Antoine Leroyer800 views
OUGN 2016: Experiences with REST support on OSB/SOA Suite by Jon Petter Hjulstad
OUGN 2016: Experiences with REST support on OSB/SOA SuiteOUGN 2016: Experiences with REST support on OSB/SOA Suite
OUGN 2016: Experiences with REST support on OSB/SOA Suite
Jon Petter Hjulstad2.6K views
4Developers 2015: Mikroserwisy - szanse, dylematy i problemy - Łukasz Sowa by PROIDEA
4Developers 2015: Mikroserwisy - szanse, dylematy i problemy - Łukasz Sowa4Developers 2015: Mikroserwisy - szanse, dylematy i problemy - Łukasz Sowa
4Developers 2015: Mikroserwisy - szanse, dylematy i problemy - Łukasz Sowa
PROIDEA324 views
Apache Thrift, a brief introduction by Randy Abernethy
Apache Thrift, a brief introductionApache Thrift, a brief introduction
Apache Thrift, a brief introduction
Randy Abernethy3.7K views
JAX London 2019 "Cloud Native Communication: Using an API Gateway and Service... by Daniel Bryant
JAX London 2019 "Cloud Native Communication: Using an API Gateway and Service...JAX London 2019 "Cloud Native Communication: Using an API Gateway and Service...
JAX London 2019 "Cloud Native Communication: Using an API Gateway and Service...
Daniel Bryant387 views
Characterizing and Contrasting Kuhn-tey-ner Awr-kuh-streyt-ors by Sonatype
Characterizing and Contrasting Kuhn-tey-ner Awr-kuh-streyt-orsCharacterizing and Contrasting Kuhn-tey-ner Awr-kuh-streyt-ors
Characterizing and Contrasting Kuhn-tey-ner Awr-kuh-streyt-ors
Sonatype 441 views
apachecamelk-april2019-190409093034.pdf by ssuserbb9f511
apachecamelk-april2019-190409093034.pdfapachecamelk-april2019-190409093034.pdf
apachecamelk-april2019-190409093034.pdf
ssuserbb9f5118 views
REST Enabling your Oracle Database (2018 Update) by Jeff Smith
REST Enabling your Oracle Database (2018 Update)REST Enabling your Oracle Database (2018 Update)
REST Enabling your Oracle Database (2018 Update)
Jeff Smith3.5K views

Recently uploaded

360 graden fabriek by
360 graden fabriek360 graden fabriek
360 graden fabriekinfo33492
24 views25 slides
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...Deltares
9 views24 slides
DevsRank by
DevsRankDevsRank
DevsRankdevsrank786
11 views1 slide
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut... by
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...Deltares
6 views28 slides
Generic or specific? Making sensible software design decisions by
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
6 views60 slides
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI... by
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...Marc Müller
36 views83 slides

Recently uploaded(20)

360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info3349224 views
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 views
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut... by Deltares
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
Deltares6 views
Generic or specific? Making sensible software design decisions by Bert Jan Schrijver
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI... by Marc Müller
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Marc Müller36 views
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t... by Deltares
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...
Deltares9 views
Software testing company in India.pptx by SakshiPatel82
Software testing company in India.pptxSoftware testing company in India.pptx
Software testing company in India.pptx
SakshiPatel827 views
Software evolution understanding: Automatic extraction of software identifier... by Ra'Fat Al-Msie'deen
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
MariaDB stored procedures and why they should be improved by Federico Razzoli
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy8 views
Advanced API Mocking Techniques by Dimpy Adhikary
Advanced API Mocking TechniquesAdvanced API Mocking Techniques
Advanced API Mocking Techniques
Dimpy Adhikary19 views
Consulting for Data Monetization Maximizing the Profit Potential of Your Data... by Flexsin
Consulting for Data Monetization Maximizing the Profit Potential of Your Data...Consulting for Data Monetization Maximizing the Profit Potential of Your Data...
Consulting for Data Monetization Maximizing the Profit Potential of Your Data...
Flexsin 15 views
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols by Deltares
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - DolsDSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
Deltares7 views
A first look at MariaDB 11.x features and ideas on how to use them by Federico Razzoli
A first look at MariaDB 11.x features and ideas on how to use themA first look at MariaDB 11.x features and ideas on how to use them
A first look at MariaDB 11.x features and ideas on how to use them
Federico Razzoli45 views
Headless JS UG Presentation.pptx by Jack Spektor
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptx
Jack Spektor7 views
DSD-INT 2023 The Danube Hazardous Substances Model - Kovacs by Deltares
DSD-INT 2023 The Danube Hazardous Substances Model - KovacsDSD-INT 2023 The Danube Hazardous Substances Model - Kovacs
DSD-INT 2023 The Danube Hazardous Substances Model - Kovacs
Deltares7 views
Fleet Management Software in India by Fleetable
Fleet Management Software in India Fleet Management Software in India
Fleet Management Software in India
Fleetable11 views

Practical Akka HTTP - introduction

  • 1. ITERATORSI T E R AT O R S @luksow Practical Akka HTTP introduction Łukasz Sowa, XI Tricity Scala User Group
  • 2. I T E R AT O RITERATORSI T E R AT O R S @luksow Hi, I'm Łukasz ● Co-founder, dev @ Iterators (https://iterato.rs) ● Highly concurrent & distributed systems (MSA) ● ScalaWAW organizer (please attend!) ● Pizza, beer & football lover ● http://luksow.com ● contact@luksow.com ● @luksow
  • 3. ITERATORSI T E R AT O R S @luksow What's in it for you? ● Learn – What Akka HTTP is – Key architectural concepts – When & how to use it – How does it fit in the Scala ecosystem ● Takeaways – Beginners: ability to start using Akka HTTP – Advanced: solid foundations for more exploring
  • 4. I T E R AT O RITERATORSI T E R AT O R S @luksow Akka HTTP basics boring facts
  • 5. ITERATORSI T E R AT O R S @luksow Akka HTTP – in definition ● Part of Akka project ● HTTP toolkit (server- and client-side) ● On top of akka-actor & akka-stream ● Streams, reactive, backpressure, blah! ● Not a framework – focus: integration layers ● Multiple levels of abstraction (open API) ● Scalable; max throughput, acceptable latency ● Both for Java & Scala
  • 6. ITERATORSI T E R AT O R S @luksow Akka HTTP – in implementation ● Fully asynchronous & non-blocking ● Actor-friendly but focused on higher-level API ● Lightweight ● Modular, testable ● Based on mature spray.io
  • 7. ITERATORSI T E R AT O R S @luksow Akka HTTP – in dates ● Spray.io (2011-03 – 2015-06 ✝?) – Bad chunked-requests support – Missing features (ex. websockets) – Hard to develop & debug ● Spray.io acquired by Typesafe (2013-10) ● First preview - 0.4 (2014-06) – Very experimental, practically unusable ● 'Good enough for evaluation and production' – 1.0 (2015-07) – No, not really, still very experimental and lacking ● Five months later - 2.0 (2015-12) – Oh sorry, we changed some interfaces, have a nice migration! ● Akka 2.4.2-RC3 (2016-02) – Akka HTTP included in main Akka project, only 'half experimental'
  • 8. ITERATORSI T E R AT O R S @luksow Akka HTTP – in parts ● akka-http-core – Low-level server & client-side HTTP implementation ● akka-http – High-level API: (un)marshalling, (de)compression, DSL ● akka-http-testkit – Utilities for testing server-side implementation ● akka-http-spray-json – Glue-code for (de)serialization from/to JSON with spray-json ● akka-http-xml – Glue-code for (de)serialization from/to XML with scala-xml
  • 9. I T E R AT O RITERATORSI T E R AT O R S @luksow Inside Akka HTTP HTTP model, server, DSL, client, (un)marshalling, testkit
  • 10. I T E R AT O RITERATORSI T E R AT O R S @luksow HTTP model ● Fully immutable, case-class-based ● Abstraction for most HTTP things (types!) ● Little logic inside ● Lots of predefined types – common media type, status codes, encodings etc. ● Open for extensions ● Still very efficient parsing & rendering ● Drawback? Your clients and connected services have to obey the standard (and bugs happen too!)
  • 11. I T E R AT O RITERATORSI T E R AT O R S @luksow HTTP model – examples (1)
  • 12. I T E R AT O RITERATORSI T E R AT O R S @luksow HTTP model - examples (2)
  • 13. I T E R AT O RITERATORSI T E R AT O R S @luksow HTTP model - examples (3)
  • 14. I T E R AT O RITERATORSI T E R AT O R S @luksow Serving – the streams way
  • 15. I T E R AT O RITERATORSI T E R AT O R S @luksow Serving – the functional way
  • 16. I T E R AT O RITERATORSI T E R AT O R S @luksow Serving – the hard way Flow[ByteString, ByteString, _]
  • 17. I T E R AT O RITERATORSI T E R AT O R S @luksow Serving – example Full example: https://github.com/theiterators/cors-buster
  • 18. I T E R AT O RITERATORSI T E R AT O R S @luksow Serving – the nice way
  • 19. I T E R AT O RITERATORSI T E R AT O R S @luksow Routing DSL ● Internal domain specific language for routing ● How most services are actually written ● Layer to the application (business logic) ● Type safe but flexible ● Not just routing – behaviour definition ● Very composable ● Fun, powerful and looks sexy!
  • 20. I T E R AT O RITERATORSI T E R AT O R S @luksow Routing DSL - example
  • 21. I T E R AT O RITERATORSI T E R AT O R S @luksow Routing DSL – directives (1) ● type Route = RequestContext ⇒ Future[RouteResult] ● abstract class Directive[L] ● def complete(m: ⇒ ToResponseMarshallable): StandardRoute ● def reject(rejections: Rejection*): StandardRoute ● def fail(error: Throwable): Future[RouteResult]
  • 22. I T E R AT O RITERATORSI T E R AT O R S @luksow Routing DSL – directives (2) ● ~136 predefined directives ● Roles: extracting, filtering, transforming request or response, side-effecting ● Create your own by composing (| and & operators) or writing from scratch ● Beaware: it takes time to fully understand how directives work
  • 23. I T E R AT O RITERATORSI T E R AT O R S @luksow Routing DSL – custom directive
  • 24. I T E R AT O RITERATORSI T E R AT O R S @luksow Routing DSL – rejections ● Rejections – Are produced by directives – Travel down the routing structure – Can be cancelled – Must be handled if no route completes request – Can be extended – trait RejectionHandler extends (immutable.Seq[Rejection] ⇒ Option[Route]) – Handling rejection = transforming it into response (probably with some error code etc.) – Ex. MethodRejection, AuthorizationFailedRejection, MissingCookieRejection, MissingQueryParamRejection
  • 25. I T E R AT O RITERATORSI T E R AT O R S @luksow Routing DSL – failures ● Failures (or exceptions) – Are triggered by exceptions or fail(...) – Travel up the routing structre – Can be handled by handleExceptions directive or top-level ExceptionHandler – trait ExceptionHandler extends PartialFunction[Throwable, Route] – Can be used to simplify flow (not very nice) – ex. for validation
  • 26. I T E R AT O RITERATORSI T E R AT O R S @luksow Client ● Connection-level client – Full control over connection and request/response scheduling ● Host-level client – Akka manages connection pool to one specific host ● Request-level – Akka performs all connection management
  • 27. I T E R AT O RITERATORSI T E R AT O R S @luksow Connection-level client ● Stream-based ● Enables – HTTP persistent connection – HTTP pipelining
  • 28. I T E R AT O RITERATORSI T E R AT O R S @luksow Host-level client ● Stream-based ● Some complexities around pool configuration and materialization
  • 29. I T E R AT O RITERATORSI T E R AT O R S @luksow Request-level client ● Super easy to use ● What you usually need ● Notice request-building DSL!
  • 30. I T E R AT O RITERATORSI T E R AT O R S @luksow (Un)marshalling (1) ● Marshalling – high level → low (wire) level – ToEntityMarshaller[T] – ToHeadersAndEntityMarshaller[T] – ToResponseMarshaller[T] – ToRequestMarshaller[T] ● Unmarshalling – low (wire) level → high level – FromEntityUnmarshaller[T] – FromMessageUnmarshaller[T] – FromResponseUnmarshaller[T] – FromRequestUnmarshaller[T] – FromStringUnmarshaller[T] – FromStrictFormFieldUnmarshaller[T]
  • 31. I T E R AT O RITERATORSI T E R AT O R S @luksow (Un)marshalling (2) ● Many predefined (un)marshallers (also for generic types) ● Extensible ● JSON and XML support in additional packages ● Type-class approach – implicit resolution ● Cryptic error messages
  • 32. I T E R AT O RITERATORSI T E R AT O R S @luksow Testkit ● Very simple and straightforward ● Allows to assert responses returned for given requests ● Integrates well with specs2, ScalaTest
  • 33. I T E R AT O RITERATORSI T E R AT O R S @luksow Everyday Akka HTTP some additional remarks
  • 34. I T E R AT O RITERATORSI T E R AT O R S @luksow Akka HTTP in Scala world (1) ● Again: it's not a framework ● Not a competition for Play – Akka HTTP is going to be Play's (2.5.X?) backend ● Competition: Spray.io, Finagle, Scalatra, Unfiltered? ● Backed by Lightbend (Typesafe)
  • 35. I T E R AT O RITERATORSI T E R AT O R S @luksow Akka HTTP in Scala world (2) ● Where it excels – Integration layers – Microservices – Pure REST APIs ● Where it falls short – Fully-fledged web applications (with server- side template generation) – IMO still lacks maturity
  • 36. I T E R AT O RITERATORSI T E R AT O R S @luksow Notable community projects ● https://github.com/softwaremill/akka-http-session ● https://github.com/hseeberger/akka-sse ● https://github.com/hseeberger/akka-http-json ● Not too many ● So roll up your sleeves! :) ● But integrates seamlessly with almost anything ● Lots of educational projects exist
  • 37. I T E R AT O RITERATORSI T E R AT O R S @luksow Performance ● Spray.io's performance was impressive – 750K req/sec via Twitter – More benchmarks by TechEmpower ● Akka HTTP 1.0 was roughly 10x slower ● Akka HTTP 2.4.2-RC2 75% of Spray's perf – “this is not the end of the performance work, we have only just begun” ● But those benchmarks, you know…
  • 38. I T E R AT O RITERATORSI T E R AT O R S @luksow Conclusions tldr;
  • 39. I T E R AT O RITERATORSI T E R AT O R S @luksow What was skipped? ● Streams stuff (persistent connections, pipelining, backpressure etc.) ● Low-level internals ● SSL/TLS support ● Very nice Websocket support ● Probably bunch of other important things
  • 40. I T E R AT O RITERATORSI T E R AT O R S @luksow What to remember? ● Akka HTTP is (probably) the hottest Scala HTTP toolkit ● Built on very solid foundations ● Features all the building blocks needed for well- designed HTTP services ● Provides both low- and high-level (DSLs!) interfaces for almost everything ● Reactive streams included ● Still lacks maturity ● Easy to start with but hard to master ● Fun to use!
  • 41. I T E R AT O RITERATORSI T E R AT O R S @luksow How to start? ● Start coding right away ● Have fun ● Discover best practices along the way ● http://doc.akka.io/docs/akka-stream-and-http-experimental/current/scala/http/index.html ● http://doc.akka.io/api/akka-stream-and-http-experimental/2.0.3/ ● https://github.com/theiterators/akka-http-microservice + tutorial ● https://github.com/theiterators/reactive-microservices + tutorial ● Lightbend Activator – search akka-http ● https://groups.google.com/forum/#!forum/akka-user ● https://gitter.im/akka/akka ● Sign up for a newsletter on http://luksow.com to get notified about big tutorial I'm preparing
  • 42. I T E R AT O RITERATORSI T E R AT O R S @luksow Thanks! ● Łukasz Sowa ● https://iterato.rs ● http://luksow.com ● contact@luksow.com ● @luksow Questions?